简体   繁体   English

Telegram bot api 从键盘按钮接收响应

[英]Telegram bot api receive response from keboard buttons

i am starting to develop telegram bot application.我开始开发电报机器人应用程序。 i am using custom keyboard while sending messages to the users.我在向用户发送消息时使用自定义键盘。 what i need to know is how can i receive the reply from the keyboard button click and how can i response to the user for that particular button click.我需要知道的是如何从键盘按钮点击接收回复,以及如何响应用户点击特定按钮。

After reading telegram bot api i got the idea that i have to getUpdates from the telegram api which are in json, i am finding it difficult to Deserialize json object received through getUpdate method.在阅读了电报 bot api 后,我想到我必须从 json 中的电报 api 中获取更新,我发现很难反序列化通过 getUpdate 方法接收到的 json 对象。

I want to know that on users keyboard button click i will receive update by json getUpdate method, by deserialising it i have to response that user_id with particular message with help of c# code, Is this is a right flow i am heading to.我想知道在用户键盘按钮上单击时,我将通过 json getUpdate 方法接收更新,通过反序列化它,我必须在 c# 代码的帮助下用特定消息响应 user_id,这是我要使用的正确流程吗? I red the github stuff but i didn't got it.我红了 github 的东西,但我没有得到它。

public async Task sndmsg()
{
    var Bot = new Api("MY_TELEGRAM_TOKEN");
    var rkm = new Telegram.Bot.Types.ReplyMarkups.ReplyKeyboardMarkup();
    //rkm.ResizeKeyboard = true;


    rkm.Keyboard = new Telegram.Bot.Types.KeyboardButton[][]
    {

    new Telegram.Bot.Types.KeyboardButton[]
    {
        new Telegram.Bot.Types.KeyboardButton("button 1")
    },

    new Telegram.Bot.Types.KeyboardButton[]
    {
        new Telegram.Bot.Types.KeyboardButton("button 2")
    },

    new Telegram.Bot.Types.KeyboardButton[]
    {
        new Telegram.Bot.Types.KeyboardButton("button 3")
    },

    new Telegram.Bot.Types.KeyboardButton[]
    {
        new Telegram.Bot.Types.KeyboardButton("button 4")
    }
};

    string cht_id = "201520743";
    string txt = "this is keyboard in reply";
    await Bot.SendTextMessage(cht_id, txt, false, false, 0, rkm);
}

I have this which i want to response when user click particular button.我有这个我想在用户单击特定按钮时响应。

I want to know:我想知道:

  1. whether i am in right direction or not?我的方向是否正确?

  2. How request response flow work while chatting with bot?与机器人聊天时请求响应流如何工作?

  3. How can i receive response of keyboard button click and reply to the user for that button click.我如何接收键盘按钮点击的响应并回复用户点击该按钮。 If using json it will be helpful if someone guide with an example for that in c#?如果使用 json,如果有人用 c# 中的示例进行指导会有所帮助吗?

  4. Is there a way to trigger a callback url for keyboard button click which can be resolved on server and in then particular reply send to user according to that button click?有没有办法触发键盘按钮点击的回调网址,可以在服务器上解析,然后根据该按钮点击向用户发送特定回复?

Can someone guide me in right direction?有人可以指导我正确的方向吗?

Please see this example:请看这个例子:

Telegram.Bot.Example in c# c# 中的 Telegram.Bot.Example

Unfortunately, You can't do this in ReplyKeyboardMarkup like what do you want.不幸的是,您不能像您想要的那样在ReplyKeyboardMarkup执行此操作。

Becuase this type of keyboard on click will send message with same text on clicked buttons.因为这种类型的键盘在点击时会在点击的按钮上发送带有相同文本的消息。

If user click button 1 the telegram will send message with text "button 1".如果用户单击按钮 1 ,电报将发送带有文本“按钮 1”的消息。

But ReplyKeyboardMarkup is useful when you need to request contact, poll or location.但是ReplyKeyboardMarkup在您需要请求联系、投票或位置时很有用。


I Suggest you to use InlineKeyboardMarkup that you will receive data in OnCallbackQuery event我建议您使用InlineKeyboardMarkup ,您将在OnCallbackQuery事件中接收数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM