Beispiel #1
0
        static void Main()
        {
            Console.WriteLine("Start!");

            var bot = new BotClient("<your bot token>");

            bot.SetMyCommands(new BotCommand("callback", "new callback"));

            // Long Polling
            var updates = bot.GetUpdates();

            while (true)
            {
                if (updates.Length > 0)
                {
                    foreach (var update in updates)
                    {
                        switch (update.Type)
                        {
                        case UpdateType.Message:
                            var message = update.Message;
                            if (message.Text.Contains("/callback"))
                            {
                                var replyMarkup = new InlineKeyboardMarkup
                                {
                                    InlineKeyboard = new InlineKeyboardButton[][] {
                                        new InlineKeyboardButton[] {
                                            InlineKeyboardButton.SetCallbackData("Callback", "callback_data")
                                        }
                                    }
                                };
                                bot.SendMessage(message.Chat.Id, "Message with callback data", replyMarkup: replyMarkup);
                            }
                            break;

                        case UpdateType.CallbackQuery:
                            var query = update.CallbackQuery;
                            bot.AnswerCallbackQuery(query.Id, "HELLO");
                            bot.EditMessageText(new EditMessageTextArgs
                            {
                                ChatId    = query.Message.Chat.Id,
                                MessageId = query.Message.MessageId,
                                Text      = $"Click!\n\n{query.Data}"
                            });
                            break;
                        }
                    }
                    updates = updates = bot.GetUpdates(offset: updates.Max(u => u.UpdateId) + 1);
                }
                else
                {
                    updates = bot.GetUpdates();
                }
            }
        }
        protected override void OnCallbackQuery(CallbackQuery query)
        {
            var cbargs = query?.Data.Split(' ');

            switch (cbargs[0])
            {
            case "month":
                var month     = new Month((MonthName)Enum.Parse(typeof(MonthName), cbargs[2]), uint.Parse(cbargs[1]));
                var mkeyboard = new InlineKeyboardMarkup
                {
                    InlineKeyboard = Calendar.New(month)
                };
                Api.EditMessageReplyMarkup <Message>(new EditMessageReplyMarkup
                {
                    ChatId      = query.Message.Chat.Id,
                    MessageId   = query.Message.MessageId,
                    ReplyMarkup = mkeyboard
                });
                break;

            case "year":
                var ykeyboard = new InlineKeyboardMarkup
                {
                    InlineKeyboard = Calendar.New(uint.Parse(cbargs[1]))
                };
                Api.EditMessageReplyMarkup <Message>(new EditMessageReplyMarkup
                {
                    ChatId      = query.Message.Chat.Id,
                    MessageId   = query.Message.MessageId,
                    ReplyMarkup = ykeyboard
                });
                break;

            default:
                Api.AnswerCallbackQuery(new AnswerCallbackQueryArgs
                {
                    CallbackQueryId = query.Id,
                    Text            = query.Data,
                    ShowAlert       = true
                });
                break;
            }
        }