Example #1
0
        private async void Bot_OnMessage(object sender, MessageEventArgs e)
        {
            var userId  = e.Message.From.Id;
            var command = e.Message.Text?.GetNetMessage();

            if (e.Message.Chat.Type != ChatType.Private)
            {
                await Bot.SendTextMessageAsync(e.Message.Chat.Id, Localization.InvalidRequest);

                return;
            }

            if (command == Localization.GetMyId.ToLower())
            {
                await Bot.SendTextMessageAsync(userId,
                                               $"{e.Message.From.FirstName} {e.Message.From.LastName}, ID: {userId}");
            }
            else if (UserAuthenticated(e.Message.From, out UserWrapper user)) // CommonReplyKeyboard
            {
                try
                {
                    await user.ConcurrencyController.WaitAsync();

                    user.LastMessageQuery = e.Message;

                    if (command == Localization.Portfolios.ToLower())
                    {
                        await Bot.SendChatActionAsync(e.Message.Chat.Id, ChatAction.Typing);

                        await Bot.SendTextMessageAsync(e.Message.Chat.Id,
                                                       Localization.Portfolios,
                                                       replyMarkup : KeyboardCollection.PortfolioInlineKeyboard(WebsiteManager.Url));
                    }
                    else if (command == Localization.About.ToLower())
                    {
                        await Bot.SendChatActionAsync(e.Message.Chat.Id, ChatAction.Typing);

                        await Bot.SendTextMessageAsync(e.Message.Chat.Id,
                                                       Localization.About + ": \n\r" + (WebsiteManager.About ?? "---"),
                                                       replyMarkup : KeyboardCollection.AboutInlineKeyboard);

                        await Bot.SendTextMessageAsync(e.Message.Chat.Id,
                                                       Localization.Title + ": \n\r" + (WebsiteManager.Title ?? "---"),
                                                       replyMarkup : KeyboardCollection.TitleInlineKeyboard);

                        await Bot.SendTextMessageAsync(e.Message.Chat.Id,
                                                       Localization.ContactEmail + ": \n\r" + (WebsiteManager.ContactEmail ?? "---"),
                                                       replyMarkup : KeyboardCollection.ContactEmailInlineKeyboard);

                        await Bot.SendTextMessageAsync(e.Message.Chat.Id,
                                                       Localization.FeedbackEmail + ": \n\r" + (WebsiteManager.FeedbackEmail ?? "---"),
                                                       replyMarkup : KeyboardCollection.FeedbackEmailInlineKeyboard);

                        await Bot.SendTextMessageAsync(e.Message.Chat.Id,
                                                       Localization.ContactPhone + ": \n\r" + (WebsiteManager.ContactPhone ?? "---"),
                                                       replyMarkup : KeyboardCollection.ContactPhoneInlineKeyboard);
                    }
                    else if (command == Localization.Logo.ToLower())
                    {
                        await Bot.SendChatActionAsync(e.Message.Chat.Id, ChatAction.UploadPhoto);

                        using (var stream = new MemoryStream(WebsiteManager.Logo))
                        {
                            await Bot.SendPhotoAsync(e.Message.Chat.Id,
                                                     photo : new FileToSend("logo", stream),
                                                     caption : Localization.Logo,
                                                     replyMarkup : KeyboardCollection.LogoInlineKeyboard());
                        }
                    }
                    else if (command == Localization.Links.ToLower())
                    {
                        await Bot.SendChatActionAsync(e.Message.Chat.Id, ChatAction.Typing);

                        await Bot.SendTextMessageAsync(e.Message.Chat.Id,
                                                       $"{Emoji.Link + Emoji.Link}           L  I  N  K  S           {Emoji.Link + Emoji.Link}",
                                                       replyMarkup : KeyboardCollection.LinksInlineKeyboard(WebsiteManager));
                    }
                    else if (command == Localization.Inbox.ToLower())
                    {
                        await Bot.SendChatActionAsync(e.Message.Chat.Id, ChatAction.Typing);

                        await Bot.SendTextMessageAsync(e.Message.Chat.Id,
                                                       $"{Emoji.SpeechBalloon + Emoji.SpeechBalloon}   Messages   {Emoji.SpeechBalloon + Emoji.SpeechBalloon}");

                        foreach (var msg in InboxManager.GetMessages())
                        {
                            var replyLink =
                                $"https://mail.google.com/mail/u/0/?view=cm&tf=0&to={msg.Email}&su=feedback+(via+{WebsiteManager.SiteName})&body=%0D%0A--%0D%0Avia+{WebsiteManager.SiteName}&bcc&cc&fs=1";

                            await Bot.SendTextMessageAsync(e.Message.Chat.Id,
                                                           $"{Emoji.WomanArtistLightSkinTone} {msg.Name}:    {msg.Subject}\n\r\n\r{Emoji.SpeechBalloon} {msg.Message}", parseMode : ParseMode.Html,
                                                           replyMarkup : KeyboardCollection.InboxMessageInlineKeyboard(msg.Id, $"Reply {msg.Name}", replyLink));
                        }
                    }
                    else
                    {
                        if (user.WaitingMessageQuery != null)
                        {
                            var t = typeof(BotManager);
                            var m = t.GetMethod(user.WaitingMessageQuery);
                            m?.Invoke(this, new object[] { user });
                        }
                        else
                        {
                            await Bot.SendTextMessageAsync(
                                e.Message.Chat.Id,
                                Localization.PleaseChooseYourOptionDoubleDot,
                                replyMarkup : KeyboardCollection.CommonReplyKeyboard());
                        }
                    }
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp);
                }
                finally
                {
                    user.ConcurrencyController.Release();
                }
            }
            else // RegisterReplyKeyboard
            {
                if (command == Localization.Start.ToLower())
                {
                    await Bot.SendTextMessageAsync(
                        e.Message.Chat.Id,
                        Localization.PleaseChooseYourOptionDoubleDot,
                        replyMarkup : KeyboardCollection.RegisterReplyKeyboard());
                }
                else if (command == Localization.Register.ToLower())
                {
                    Accounts[userId].Password = "";
                    await Bot.SendTextMessageAsync(e.Message.Chat.Id,
                                                   $"{Emoji.Key} {Localization.Password}: ",
                                                   replyMarkup : KeyboardCollection.PasswordInlineKeyboard());
                }
                else
                {
                    await Bot.SendTextMessageAsync(
                        e.Message.Chat.Id,
                        Localization.PleaseChooseYourOptionDoubleDot,
                        replyMarkup : KeyboardCollection.RegisterReplyKeyboard());
                }
            }
        }