Esempio n. 1
0
        public async Task Send(SentMessage message, ReceivedMessage receivedMessage)
        {
            if (message.ChatId.Messenger != MessengerId)
            {
                return;
            }

            var associatedData = (ReceivedMessageAssociatedData)receivedMessage?.MessengerSpecificData;

            LastMessageSent = DateTime.Now;

            InlineKeyboardMarkup inlineKeyboardMarkup  = null;
            ReplyKeyboardMarkup  buttonsKeyboardMarkup = null;

            if (message.Buttons == null)
            {
                // Should not be here
                Logger.Error("Sending message without buttons. This is bad.");
            }
            else
            {
                var inlineButtons = new InlineKeyboardButton[message.Buttons.Length][];
                var simpleButtons = new KeyboardButton[message.Buttons.Length][];
                var keyboard      = false;
                for (var i = 0; i < message.Buttons.Length; i++)
                {
                    var buttons = message.Buttons[i];
                    inlineButtons[i] = new InlineKeyboardButton[buttons.Length];
                    simpleButtons[i] = new KeyboardButton[buttons.Length];
                    for (var j = 0; j < buttons.Length; j++)
                    {
                        inlineButtons[i][j] = new InlineKeyboardButton
                        {
                            Text         = buttons[j],
                            CallbackData = buttons[j],
                        };
                        simpleButtons[i][j] = new KeyboardButton
                        {
                            Text = buttons[j],
                        };
                        keyboard = true;
                    }
                }

                if (keyboard)
                {
                    inlineKeyboardMarkup  = new InlineKeyboardMarkup(inlineButtons);
                    buttonsKeyboardMarkup = new ReplyKeyboardMarkup(simpleButtons);
                }
            }

            const bool enableInline = false;
            var        parseMode    = message.Formatted ? ParseMode.Html : ParseMode.Default;

            var text = message.Text;

            if (associatedData?.ReplyMessageId != null)
            {
                text += $"\n<a href=\"tg://user?id={associatedData.ReplyMessageId}\">@</a>";
            }

            try
            {
                if (enableInline)
                {
                    var inlineId = associatedData?.InlineMessageId;
                    if (inlineId != null && message.PreferToUpdate == true)
                    {
                        var(chatId, messageId) = inlineId.Value;
                        await _bot.EditMessageTextAsync(
                            chatId,
                            messageId,
                            text,
                            parseMode,
                            replyMarkup : inlineKeyboardMarkup
                            );
                    }
                    else
                    {
                        if (inlineId != null)
                        {
                            // PreferToUpdate is null, but inlineId is not null, so remove old buttons
                            // Also we do not want to wait until they are removed.
#pragma warning disable 4014
                            var(chatId, messageId) = inlineId.Value;
                            _bot.EditMessageReplyMarkupAsync(chatId, messageId,
                                                             new InlineKeyboardMarkup(new InlineKeyboardButton[0]));
#pragma warning restore 4014
                        }

                        await _bot.SendTextMessageAsync(
                            message.ChatId.Id,
                            text,
                            parseMode,
                            replyMarkup : inlineKeyboardMarkup
                            );
                    }
                }
                else
                {
                    await _bot.SendTextMessageAsync(
                        message.ChatId.Id,
                        text,
                        parseMode,
                        replyMarkup : buttonsKeyboardMarkup
                        );
                }
            }
            catch (Exception e)
            {
                if (e is ForbiddenException)
                {
                    Logger.Info(e, "Bot is stopped by user {0}", message.ChatId.Id);
                }
                else
                {
                    Logger.Error(e, "Error while sending message");
                }
            }
        }
Esempio n. 2
0
        public async Task Send(SentMessage message, RecivedMessage recivedMessage)
        {
            if (message.ChatId.Messenger != MessengerId)
            {
                return;
            }

            LastMessageSent = DateTime.Now;

            IReplyMarkup replyMarkup;

            if (message.Buttons == null)
            {
                // Leave previous keyboard
                replyMarkup = null;
            }
            else
            {
                var markup   = new KeyboardButton[message.Buttons.Length][];
                var keyboard = false;
                for (var i = 0; i < message.Buttons.Length; i++)
                {
                    var buttons = message.Buttons[i];
                    markup[i] = new KeyboardButton[buttons.Length];
                    for (var j = 0; j < buttons.Length; j++)
                    {
                        markup[i][j] = new KeyboardButton(buttons[j]);
                        keyboard     = true;
                    }
                }

                if (keyboard)
                {
                    replyMarkup = new ReplyKeyboardMarkup(markup)
                    {
                        Selective = true
                    };
                }
                else
                {
                    replyMarkup = new ReplyKeyboardRemove {
                        Selective = true
                    };
                }
            }

            var parseMode = message.Formatted ? ParseMode.Markdown : ParseMode.Default;

            var text = message.Text;

            if (recivedMessage?.ReplyUserId != null)
            {
                text += $"\n[@](tg://user?id={recivedMessage.ReplyUserId})";
            }

            try
            {
                await _bot.SendTextMessageAsync(
                    message.ChatId.Id,
                    text,
                    parseMode,
                    replyMarkup : replyMarkup
                    //replyToMessageId: recivedMessage?.ReplyId ?? 0  // Not working, because MessageId is different for each bot
                    );
            }
            catch (Exception e)
            {
                if (e is ForbiddenException)
                {
                    // User stopped this bot.
                }
                else
                {
                    YandexMetrica.ReportError("Error while sending message", e);
                    Logger.Error(e, "Error while sending message");
                }
            }
        }