示例#1
0
        private static void RecieveUpdates(object stateInfo)
        {
            GetUpdatesRequest updateOpts = new GetUpdatesRequest();
            updateOpts.Offset = Properties.Settings.Default.LastReadMessage;
            GetUpdatesResponse response = ApiAccess.GetUpdates(updateOpts);
            List<UpdateResponse> updates = response.Result;

            if (response.Ok)
            {
                if (updates.Count > 0)
                {
                    foreach (UpdateResponse update in updates)
                    {
                        if (update.Message.Text != "" && update.Message.Text != null)
                        {
                            if (Properties.Settings.Default.Verbose)
                            {
                                string date = "";
                                if (update.Message.Date.HasValue)
                                {
                                    date = update.Message.Date.Value.ToShortTimeString() + " ";
                                }
                                Console.WriteLine(date +
                                    update.Message.From.FirstName + ":" +
                                    update.Message.Text);
                            }
                            if (update.Message.Text.StartsWith("/") == false)
                            {
                                if (ChatKeys.Contains(update.Message.Chat.Id))
                                {
                                    Chats[update.Message.Chat.Id].Chain.Feed(update.Message.Text);
                                }
                            }
                            else
                            {
                                if (update.Message.Text.ToLower().StartsWith("/subscribe"))
                                {
                                    if (ChatKeys.Contains(update.Message.Chat.Id) == false)
                                    {
                                        Chat subscriber = new Chat(update.Message.Chat.Id);
                                        try
                                        {
                                            subscriber.Load();
                                        }
                                        catch (Exception)
                                        {
                                            subscriber.Chain = new Markov();
                                        }
                                        Chats.Add(subscriber.ChatID, subscriber);
                                        ChatKeys.Add(subscriber.ChatID);

                                        SendTgMessage(update.Message.Chat.Id, "Congrats! You are now " +
                                            "subscribed to my wisdom!");
                                    }
                                    else
                                    {
                                        SendTgMessage(update.Message.Chat.Id, "Chat already subscribed.");
                                    }
                                }
                                else if (update.Message.Text.ToLower().StartsWith("/unsubscribe"))
                                {
                                    if (ChatKeys.Contains(update.Message.Chat.Id))
                                    {
                                        ChatKeys.Remove(update.Message.Chat.Id);
                                        Chats.Remove(update.Message.Chat.Id);
                                        SendTgMessage(update.Message.Chat.Id, "This chat has successfuly been unsubscribed.");
                                    }
                                    else
                                    {
                                        SendTgMessage(update.Message.Chat.Id, "Chat is not subsrcibed to begin with!\n" +
                                            "...How dare you!");
                                    }
                                }
                            }
                        }
                    }
                    Properties.Settings.Default.LastReadMessage =
                    updates[updates.Count - 1].UpdateId + 1;
                    Properties.Settings.Default.Save();
                }
            }
        }
示例#2
0
 private static void ReadChats()
 {
     string[] chatIds;
     chatIds = System.IO.File.ReadAllLines("chat.list");
     if (chatIds.Length > 0)
     {
         foreach (string line in chatIds)
         {
             try
             {
                 Chat readChat = new Chat(int.Parse(line));
                 ChatKeys.Add(readChat.ChatID);
                 try
                 {
                     readChat.Load();
                 }
                 catch (Exception)
                 {
                     readChat.Chain = new Markov();
                 }
                 Chats.Add(readChat.ChatID, readChat);
             }
             catch (Exception)
             {
                 /* ~~~ */
             }
         }
     }
 }