C#Telegram Bot Inline Mode向用户发送按钮

时间:2018-06-07 19:18:34

标签: c# bots telegram

我正在尝试使用此库创建内联模式机器人:https://github.com/TelegramBots/Telegram.Bot

我在OnInlineQuery事件中添加了一个方法。它被调用,但现在我想将一个按钮发回聊天,该按钮应显示在键盘上方。我该如何做到这一点? 这就是我到目前为止所做的:

BotClient.OnInlineQuery += BotOnInlineQuery;
protected async override void BotOnInlineQuery(object sender, InlineQueryEventArgs inlineQueryEventArgs)
        {
            Log.Info("Inline called " + inlineQueryEventArgs.InlineQuery.From.FirstName);

            InlineQueryResult[] results = {
                new InlineQueryResultVenue()
            };


            await BotClient.SendTextMessageAsync(inlineQueryEventArgs.InlineQuery.Id, "test");

            await BotClient.AnswerInlineQueryAsync(
                inlineQueryEventArgs.InlineQuery.Id,
                results,
                isPersonal: true,
                cacheTime: 0);
        }

1 个答案:

答案 0 :(得分:0)

您的代码中有一些错误:

  1. 您不能将SendTextMessageAsyncInlineQuery.Id一起使用
  2. InlineQueryResultVenue没有参数,没有构造函数
  3. 您无法覆盖OnInlineQuery,因为它不是方法,而是事件!

答案

要将按钮与行内消息一起使用,请对ReplyMarkup中的InlineKeyboardMarkup使用InlineQueryResultBase属性。

并使用聊天链接t.me, 如果我有用户名 @BotFather ,请使用链接:https://t.me/BotFather

因此,在您的聊天链接中使用InlineKeyboardButton.WithUrl("Go to chat", "https://t.me/my_chat_username")

使用此代码:

private async void BotClient_OnInlineQuery(object sender, InlineQueryEventArgs e)
{
    // Some variables
    string link = "https://t.me/chat_username";
    // Your Button that you want
    InlineKeyboardButton button = InlineKeyboardButton.WithUrl("Go to chat", link);

    // Inline Results
    InlineQueryResultBase[] results = new[]  
    {
        new InlineQueryResultVenue(e.InlineQuery.Id, 30, 30, "Venue title", "address")
        //TODO: Add here any of InlineQueryResultVenue properties.
        {
            // HERE IS WHAT DO YOU WANT:
            // Keyboard:
            ReplyMarkup = new InlineKeyboardMarkup(button);
        }
    }
    
    // Answer with results:
    await BotClient.AnswerInlineQueryAsync(e.InlineQuery.Id,
                                           results,
                                           isPersonal: true
                                           cacheTime: 0);
}