简体   繁体   English

C# WTelegramClient Telegram-API 如何以编程方式单击来自 Telegram-Bot 的消息中的按钮

[英]C# WTelegramClient Telegram-API How to programmatically click a button in a message from Telegram-Bot

The program communicates with Telegram-Bot.该程序与 Telegram-Bot 通信。 A text message is sent to the bot.一条短信被发送到机器人。 The bot responds to the message.机器人响应消息。 The answer contains 3 buttons.答案包含 3 个按钮。 Question: How to click on a button programmatically?问题:如何以编程方式单击按钮?

Create a Telegram client:创建电报客户端:

_client = new WTelegram.Client(Config):
_user = await _client.LoginUserIfNeeded();

Finding a bot:寻找机器人:

var contacts = await _client.Contacts_Search("@Search_bot", 20);
contacts.users.TryGetValue(1911124859, out peerBot));

Sending a request (message) to the Bot:向 Bot 发送请求(消息):

var message = await _client.SendMessageAsync(peerBot, "REQUEST");

We get an answer:我们得到一个答案:

var differenceMessages = await _client.Updates_GetDifference(state.pts, state.date, state.qts);

We understand the answer.我们明白答案。 We find a button.我们找到一个按钮。 How to send a message to the Bot that we clicked on the button?如何向我们点击按钮的 Bot 发送消息?

TL.Message tLMessage = null;
if (differenceMessages != null)
{
    if(differenceMessages.NewMessages != null)
    {
        foreach (var difference in differenceMessages.NewMessages)
        {
            tLMessage = difference as TL.Message;
            if(tLMessage != null && tLMessage.peer_id.ID == peerBot.ID )
            {
                if (!(tLMessage.reply_markup is ReplyMarkup replyMarkup)) continue;
                TL.ReplyInlineMarkup replyInlineMarkup = (ReplyInlineMarkup)replyMarkup;
                if (replyInlineMarkup.rows[2].buttons[0].Text == "Check text on Button")
                {
                    ***//TODO We want to click on this button!***
                }
            }
        }
    }
}

An inline button can be one of several types .内联按钮可以是多种类型之一
Let's assume you're talking about a Callback button (that sends its callback to the bot)..假设您正在谈论一个回调按钮(将其回调发送到机器人)..

Searching through the full list of API methods available , you would quickly find that the right method to proceed to clicking on that button is messages.getBotCallbackAnswer (so Messages_GetBotCallbackAnswer in WTelegramClient naming)搜索可用 API 方法的完整列表,您会很快发现继续单击该按钮的正确方法是messages.getBotCallbackAnswer (因此在 WTelegramClient 命名中为Messages_GetBotCallbackAnswer

Therefore, in your case, you would write something like this:因此,在您的情况下,您将编写如下内容:

if (replyInlineMarkup.rows[2].buttons[0] is KeyboardButtonCallback btnCallback)
{
    var answer = await client.Messages_GetBotCallbackAnswer(peerBot, tLMessage.id, data: btnCallback.data);
}

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

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