justdmitry / NetTelegramBotApi

C# client library for building Telegram bot
MIT License
74 stars 28 forks source link

Call back query #60

Open SecretV opened 7 years ago

SecretV commented 7 years ago

hi can you please write an example of creating an in line keyboard with a call back data and how to receive the call back data?

please answer soon!

XcJoo commented 7 years ago

Hi, here is a very short example in Vb.Net. hopefully it helps:

' Create message and send
Dim sMessage = New SendMessage(ChatID, MessageText) With {.ReplyMarkup = New InlineKeyboardMarkup With {
  .InlineKeyboard = {
    {New InlineKeyboardButton With {.Text = "Button Text", .CallbackData = "Callback Value"}}.ToArray
  }.ToArray
}}
Dim sReturn = teleBot.MakeRequestAsync(sMessage).Result

' ----------------------------------------------
' Callback in a e.g. aspx-file
Dim tBot As New TelegramBot(ACCESS_TOKEN)

Dim strPostRequest As String = ""
Try
  Request.InputStream.Position = 0
  Dim inputStream As New IO.StreamReader(Request.InputStream)
  strPostRequest = inputStream.ReadToEnd()
Catch ex As Exception
End Try

Dim update As Update = tBot.DeserializeUpdate(strPostRequest)

If update IsNot Nothing Then 
  ' The Callback Value is in update.CallbackQuery.Data
End If
justdmitry commented 7 years ago

@XcJoo is right - just put InlineKeyboard with some InlineKeyboardButton into your message.

When button is pressed - you will receive Update with CallbackQuery populated.

rasools13 commented 7 years ago

hi @SecretV , here is a example ` /// while (stopme == true) { try { var Updates = await bot.MakeRequestAsync(new GetUpdates() { Offset = offset });

                    if (Updates != null)
                    {
                        foreach (var UpD in Updates)
                        {
                            if (UpD.Message != null)
                            {
                                offset = UpD.UpdateId + 1;
                                string chatTxt = UpD.Message.Text;
                                if (chatTxt == "/start")
                                {
                                   InlineKeyboardMarkup btnInline = new InlineKeyboardMarkup();
                                    InlineKeyboardButton btn1 = new InlineKeyboardButton { Text = "btn Inline Txt", CallbackData = "callback value" };
                                    btnInline.InlineKeyboard = new[] { new[] { btn1 } };
                                    await bot.MakeRequestAsync(new SendMessage(UpD.Message.Chat.Id, "This is a test message") { ReplyMarkup = btnInline });
                                }
                            }
                            else if (UpD.CallbackQuery != null)
                            {
                                offset = UpD.UpdateId + 1;
                                string t1 = UpD.CallbackQuery.Data.ToString();
                                long chID = UpD.CallbackQuery.Message.Chat.Id;
                                long MId = UpD.CallbackQuery.Message.MessageId;

                                if (t1 == "callback value")
                                {
                                    await bot.MakeRequestAsync(new EditMessageText(chID, MId, "This is callback answer") { ReplyMarkup = btnInline });
                                }
                                AnswerCallbackQuery an = new AnswerCallbackQuery(UpD.CallbackQuery.Id);
                                await bot.MakeRequestAsync(an);
                            }

                        }
                    }
                }
                catch (BotRequestException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }

`