Exemplo n.º 1
0
        async void launchWorkerAsync(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            string           key    = e.Argument as String;

            try
            {
                Bot = new Telegram.Bot.TelegramBotClient(key);
                await Bot.SetWebhookAsync("");

                int offset = 0;
                while (true)
                {
                    var messages = await Bot.GetUpdatesAsync(offset);

                    foreach (var message in messages)
                    {
                        var  msgText = message.Message;
                        User currentUser;
                        if (users.ContainsKey(msgText.Chat.Id))
                        {
                            currentUser = users[msgText.Chat.Id];
                        }
                        else
                        {
                            currentUser = new User("Уважаемый", bot);

                            currentUser.Predicates.addSetting("favouriteanimal", "default");
                            currentUser.Predicates.addSetting("name", "default");

                            users[msgText.Chat.Id] = currentUser;
                        }

                        if (msgText.Type == Telegram.Bot.Types.Enums.MessageType.Text && msgText.Text.Contains("/game"))
                        {
                            string text = msgText.Text;
                            text = text.Replace("/game ", "");
                            string[] clues = text.Split(',');
                            clues = clues.Select(x => x.Trim().ToLower()).ToArray();
                            List <string> games = Program.SetupFacts(clues.ToList(), "");
                            if (games.Count == 0)
                            {
                                await Bot.SendTextMessageAsync(msgText.Chat.Id, "Не знаю таких игра :(");
                            }
                            else
                            {
                                string res = games.Aggregate((i, j) => i + '?' + j);
                                res += '?';
                                await Bot.SendTextMessageAsync(msgText.Chat.Id, "Я что-то такое знаю. " + res);
                            }
                        }
                        else if (msgText.Type == Telegram.Bot.Types.Enums.MessageType.Text)
                        {
                            Console.WriteLine(msgText.Text.ToUpper());
                            Request r   = new Request(msgText.Text, currentUser, bot);
                            Result  res = bot.Chat(r);
                            if (res.Output != "")
                            {
                                if (res.Output.EndsWith(".jpg."))
                                {
                                    await Bot.SendPhotoAsync(msgText.Chat.Id, res.Output.Remove(res.Output.Count() - 1, 1));
                                }
                                else if (res.Output == "time.")
                                {
                                    var time = "У меня " + DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ", сколько у вас - не знаю";
                                    await Bot.SendTextMessageAsync(msgText.Chat.Id, time);
                                }
                                else if (res.Output == "foot.")
                                {
                                    string table = football.getTable();
                                    await Bot.SendTextMessageAsync(msgText.Chat.Id, table);
                                }
                                else if (res.Output == "anec.")
                                {
                                    string anec = Anecdot.anec();
                                    await Bot.SendTextMessageAsync(msgText.Chat.Id, anec);
                                }
                                else if (res.Output == "voice.")
                                {
                                    string anec = Anecdot.anec();

                                    SpeechSynthesizer speaker = new SpeechSynthesizer();

                                    speaker.Rate   = 2;
                                    speaker.Volume = 100;

                                    speaker.Speak(anec);

                                    speaker.SetOutputToWaveFile("soundfile.wav");
                                    speaker.Speak(anec + "\n АХАХАХАХАХАХ");
                                    speaker.SetOutputToDefaultAudioDevice();

                                    var fStream = new FileStream("soundfile.wav", FileMode.OpenOrCreate);
                                    await Bot.SendVoiceAsync(msgText.Chat.Id, new Telegram.Bot.Types.InputFiles.InputOnlineFile(fStream));

                                    fStream.Dispose();
                                }
                                else if (res.Output == "beauty.")
                                {
                                    var fStream = new FileStream("..\\..\\1beauty.jpg", FileMode.OpenOrCreate);
                                    await Bot.SendPhotoAsync(msgText.Chat.Id, new Telegram.Bot.Types.InputFiles.InputOnlineFile(fStream), "Красивее не сыскать");
                                }
                                else if (res.Output.Contains("poetryFlag"))
                                {
                                    var poem = res.Output.Replace("poetryFlag", "");
                                    SpeechSynthesizer speaker = new SpeechSynthesizer();

                                    speaker.Rate   = 2;
                                    speaker.Volume = 100;

                                    speaker.SetOutputToWaveFile("soundfile.wav");
                                    speaker.Speak(poem);
                                    speaker.SetOutputToDefaultAudioDevice();

                                    var fStream = new FileStream("soundfile.wav", FileMode.OpenOrCreate);
                                    await Bot.SendVoiceAsync(msgText.Chat.Id, new Telegram.Bot.Types.InputFiles.InputOnlineFile(fStream));

                                    fStream.Dispose();
                                }
                                else
                                {
                                    await Bot.SendTextMessageAsync(msgText.Chat.Id, res.Output);
                                }
                            }
                        }
                        else if (msgText.Type == Telegram.Bot.Types.Enums.MessageType.Photo)
                        {
                            ProcessImage(msgText.Photo[msgText.Photo.Length - 1].FileId, "", msgText.Chat.Id);
                        }
                        offset = message.Id + 1;
                    }
                }
            }
            catch (Telegram.Bot.Exceptions.ApiRequestException exception)
            {
                Console.WriteLine(exception.Message);
                asyncWorker.RunWorkerAsync("999037946:AAHbd0xIjp5l6iS0aGuVB-jIP2R4a99EUFo");
            }
        }