Exemple #1
0
        public override bool chatEvent(message m, chat c = null)
        {
            bool processed = false;

            if (m.text_msg.StartsWith("/help") && c != null && c.muted == false)
            {
                mod_standard_chatdata chatData = c.getPluginData <mod_standard_chatdata>();
                string openingMessage          = "This is chat " + (c.chatTitle == null ? "" : c.chatTitle) + " (" + c.chatID + "). " + "\n\r";
                if (chatData.quietHoursStartTime != TimeSpan.MinValue && chatData.quietHoursEndTime != TimeSpan.MinValue)
                {
                    openingMessage += "Quiet time set between " + chatData.quietHoursStartTime.ToString("c") + " and " + chatData.quietHoursEndTime.ToString("c") + ". \n\r";
                }

                TelegramAPI.SendMessage(m.chatID, openingMessage + getAllMethodDescriptions());
                processed = true;
            }
            else if (m.text_msg.StartsWith("/save"))
            {
                Roboto.Settings.save();
                TelegramAPI.SendMessage(m.chatID, "Saved settings");
                processed = true;
            }
            else if (m.text_msg.StartsWith("/stop") && c != null)
            {
                c.muted = true;
                TelegramAPI.SendMessage(m.chatID, "I am now ignoring all messages in this chat until I get a /start command. ");
                //TODO - make sure we abandon any games

                processed = true;
            }
            else if (m.text_msg.StartsWith("/start") && c != null && c.muted == true)
            {
                c.muted = false;
                TelegramAPI.SendMessage(m.chatID, "I am listening for messages again. Type /help for a list of commands." + "\n\r" + getAllWelcomeDescriptions());
                processed = true;
            }
            else if (m.text_msg.StartsWith("/start"))
            {
                //a default /start message where we arent on pause. Might be in group or private chat.
                TelegramAPI.SendMessage(m.chatID, getAllWelcomeDescriptions());
            }


            else if (m.text_msg.StartsWith("/background"))
            {
                //kick off the background loop.
                Roboto.Settings.backgroundProcessing(true);
            }

            else if (m.text_msg.StartsWith("/setquiethours") && c != null)
            {
                TelegramAPI.GetExpectedReply(m.chatID, m.userID, "Enter the start time for the quiet hours, cancel, or disable. This should be in the format hh:mm:ss (e.g. 23:00:00)", true, this.GetType(), "setQuietHours");
                processed = true;
            }

            else if (m.text_msg.StartsWith("/stats"))
            {
                TimeSpan uptime = DateTime.Now.Subtract(Roboto.startTime);

                String statstxt = "I is *@" + Roboto.Settings.botUserName + "*" + "\n\r" +
                                  "Uptime: " + uptime.Days.ToString() + " days, " + uptime.Hours.ToString() + " hours and " + uptime.Minutes.ToString() + " minutes." + "\n\r" +
                                  "I currently know about " + Roboto.Settings.chatData.Count().ToString() + " chats." + "\n\r" +
                                  "The following plugins are currently loaded:" + "\n\r";

                foreach (RobotoModuleTemplate plugin in settings.plugins)
                {
                    statstxt += "*" + plugin.GetType().ToString() + "*" + "\n\r";
                    statstxt += plugin.getStats() + "\n\r";
                }

                TelegramAPI.SendMessage(m.chatID, statstxt, m.userFullName, true);
                processed = true;
            }
            else if (m.text_msg.StartsWith("/addadmin") && c != null)
            {
                //check if we have privs. This will send a fail if not.
                if (c.checkAdminPrivs(m.userID, c.chatID))
                {
                    //if there is no admin, add player
                    if (!c.chatHasAdmins())
                    {
                        bool added = c.addAdmin(m.userID, m.userID);
                        if (added)
                        {
                            TelegramAPI.SendMessage(m.chatID, "Added " + m.userFullName + " as admin.");
                        }
                        else
                        {
                            TelegramAPI.SendMessage(m.chatID, "Something went wrong! ");
                            log("Error adding user as an admin", logging.loglevel.high);
                        }
                    }
                    else
                    {
                        //create a keyboard with the recent chat members
                        List <string> members = new List <string>();
                        foreach (chatPresence p in c.getRecentChatUsers())
                        {
                            members.Add(p.ToString());
                        }
                        //send keyboard to player requesting admin.
                        TelegramAPI.GetExpectedReply(m.chatID, m.userID, "Who do you want to add as admin?", true, typeof(mod_standard), "ADDADMIN", m.userFullName, -1, false, TelegramAPI.createKeyboard(members, 2));
                    }
                }
                else
                {
                    log("User tried to add admin, but insufficient privs", logging.loglevel.high);
                }
                processed = true;
            }
            else if (m.text_msg.StartsWith("/removeadmin") && c != null)
            {
                //check if we have privs. This will send a fail if not.
                if (c.checkAdminPrivs(m.userID, c.chatID))
                {
                    //if there is no admin, add player
                    if (!c.chatHasAdmins())
                    {
                        TelegramAPI.SendMessage(m.chatID, "Group currently doesnt have any admins!");
                    }
                    else
                    {
                        //create a keyboard with the recent chat members
                        List <string> members = new List <string>();
                        foreach (long userID in c.chatAdmins)
                        {
                            members.Add(userID.ToString());
                        }
                        //send keyboard to player requesting admin.
                        TelegramAPI.GetExpectedReply(m.chatID, m.userID, "Who do you want to remove as admin?", true, typeof(mod_standard), "REMOVEADMIN", m.userFullName, -1, false, TelegramAPI.createKeyboard(members, 2));
                    }
                }
                else
                {
                    log("User tried to remove admin, but insufficient privs", logging.loglevel.high);
                }
                processed = true;
            }



            else if (m.text_msg.StartsWith("/statgraph"))
            {
                string[] argsList = m.text_msg.Split(" ".ToCharArray(), 2);
                Stream   image;
                //Work out args and get our image
                if (argsList.Length > 1)
                {
                    string args = argsList[1];
                    image = Roboto.Settings.stats.generateImage(argsList[1].Split("|"[0]).ToList());
                }
                else
                {
                    image = Roboto.Settings.stats.generateImage(new List <string>());
                }

                //Sending image...
                if (image != null)
                {
                    TelegramAPI.SendPhoto(m.chatID, "Stats", image, "StatsGraph.jpg", "application/octet-stream", m.message_id, false);
                }
                else
                {
                    TelegramAPI.SendMessage(m.chatID, "No statistics were found that matched your input, sorry!");
                }
                processed = true;

                //TODO - keyboard for stats?
            }

            return(processed);
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public string ToTechnologyGroup()
        {
            log4net.ILog logger = log4net.LogManager.GetLogger(typeof(string));  //Declaring Log4Net

            string telegramPublishPath         = System.Configuration.ConfigurationManager.AppSettings["TelegramPublishPath"].ToString();
            string telegramTechnologyGroupID   = System.Configuration.ConfigurationManager.AppSettings["TelegramTechnologyGroupID"].ToString();
            string telegramTechnologyGroupName = System.Configuration.ConfigurationManager.AppSettings["TelegramTechnologyGroupName"].ToString();

            //string telegramLoggerPath = System.Configuration.ConfigurationManager.AppSettings["telegramLoggerPath"].ToString();
            try
            {
                // Send Post Queue To Telegram Technology Channel
                var telegramQueuePath = telegramPublishPath + "TechnologyPostQueue.json";
                var telegramJsonData  = System.IO.File.ReadAllText(telegramQueuePath);
                // Check Telegram Queue is not Empty
                if (telegramJsonData == null || telegramJsonData == "[]")
                {
                    logger.Error("List is Empty.");
                    return("OK");
                }

                var telegramPostList = Newtonsoft.Json.JsonConvert.DeserializeObject <System.Collections.Generic.List <PostModel> >(telegramJsonData) ?? new System.Collections.Generic.List <PostModel>();

                // First Selection Method : Max Article Length
                PostModel post = telegramPostList.OrderByDescending(x => x.ArticleLength).FirstOrDefault();

                // Two Selection Method : High Post Priority
                if (post == null)
                {
                }

                // Three Selection Method : Max Caption White Word Count
                if (post == null)
                {
                }

                // Four Selection Method : Max Article Image Count
                if (post == null)
                {
                }

                // Anyway : First Post in Queue
                if (post == null)
                {
                    post = telegramPostList.FirstOrDefault();
                }

                TelegramAPI telegram = new TelegramAPI();
                // Send Photo Post
                if (post.Content == PostContentType.Photo)
                {
                    var caption = post.Caption + "\n\n" + post.ShortArticle + "\n\n🔗 " + post.ShortLink.Replace("http://", "") + "\n\n" + telegramTechnologyGroupName;
                    telegram.SendPhoto(telegramTechnologyGroupID, post.Photo, caption);
                }

                // Delete Post
                telegramPostList.Remove(post);
                telegramJsonData = Newtonsoft.Json.JsonConvert.SerializeObject(telegramPostList);
                System.IO.File.WriteAllText(telegramQueuePath, telegramJsonData);

                return("OK");
            }
            catch (System.Exception ex)
            {
                logger.Error(ex.ToString());
                return("BAD");
            }
        }