How Can Get CallbackData in current when InlineKeyboardCallbackButton clicked in telegram Bot in C#

2.5k views Asked by At

How can I get InlineKeyboardCallbackButton clicked in telegram bot?

Here is my code : I Edited The Code

public async System.Threading.Tasks.Task<ActionResult> GetMsgAsync()
     {
     var req = Request.InputStream; 
     var responsString = new StreamReader(req).ReadToEnd(); 
     var update = JsonConvert.DeserializeObject<Update>(responsString);
     var message = update.Message;  
     var chat = message.Chat;  


  InlineKeyboardMarkup categoryInlineMarkup = new 
      InlineKeyboardMarkup(
         new InlineKeyboardButton[][]
                        {
                        new InlineKeyboardButton[]
                            {
                  new InlineKeyboardCallbackButton("button1","callbackData")
                            }
                        }
                           );

                await api.SendTextMessageAsync(update.Message.Chat.Id, "Please click the button", replyMarkup: categoryInlineMarkup);

                if (update.Type == Telegram.Bot.Types.Enums.UpdateType.MessageUpdate)
                {
                    // all codes just run in this block
                }



                if (update.Type == Telegram.Bot.Types.Enums.UpdateType.CallbackQueryUpdate)
                {
                    // I can't get clicked button here 
                    if (update.CallbackQuery.Data.Contains("callbackData"))
                    {
                        await api.AnswerCallbackQueryAsync(update.CallbackQuery.Id, update.CallbackQuery.Data);
                    }
                }


   }

How can I get this button clicked also in webhook method, not in console program?

1

There are 1 answers

0
Yaser On BEST ANSWER

Finally i find out the answer, the problem is this line of code :

  var message = update.Message; 

this line of code is the main problem because this line is not in try catch block and i can't understand which line is the problem .

After this Problem, i have a suggestion for you
You can debugging your telegram bot in your personal computer without uploading any code on any host, for this purpose You can run 3 below steps ::

1- Accessing an IIS Express site from a remote computer

2- Download NGROK

3- after download ngrok you can forward telegram webhook requests to your personal computer .