简体   繁体   English

C# Telegram Bot 内联模式发送按钮给用户

[英]C# Telegram Bot Inline Mode Send Button to user

I am trying to create an inline bot with: Telegram.Bot我正在尝试创建一个内联机器人: Telegram.Bot

I have added a method to the OnInlineQuery event.我向OnInlineQuery事件添加了一个方法。 It gets called, But now I want to send a button back to the chat, which should be displayed above the keyboard.它被调用,但现在我想向聊天发送一个按钮,该按钮应显示在键盘上方。 How do I accomplish this?我该如何实现?

This is what I have so far:这是我到目前为止:

BotClient.OnInlineQuery += BotOnInlineQuery;

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

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


    await BotClient.SendTextMessageAsync(inlineQueryEventArgs.InlineQuery.Id, "test");
    
    // Answer with results
    await BotClient.AnswerInlineQueryAsync(
                    inlineQueryEventArgs.InlineQuery.Id,
                    results,
                    isPersonal: true,
                    cacheTime: 0);
}

You have some errors in your code:您的代码中有一些错误:

  1. You can't use SendTextMessageAsync with InlineQuery.Id您不能将SendTextMessageAsyncInlineQuery.Id SendTextMessageAsync使用
  2. There is no constructor for InlineQueryResultVenue with no parameters InlineQueryResultVenue没有没有参数的构造函数
  3. You can't override OnInlineQuery because it's not a method, It's an event!你不能覆盖OnInlineQuery因为它不是一个方法,它是一个事件!

Answer回答

To use Buttons with inline message use ReplyMarkup property with InlineKeyboardMarkup in InlineQueryResultBase .要将按钮与内联消息一起使用, InlineKeyboardMarkupInlineQueryResultBase使用ReplyMarkup属性和InlineQueryResultBase

And use chat links t.me, if I have username @BotFather use link: https://t.me/BotFather并使用聊天链接 t.me,如果我有用户名@BotFather使用链接: https : //t.me/BotFather

So use InlineKeyboardButton.WithUrl("Go to chat", "https://t.me/my_chat_username") with your chat link.因此,将InlineKeyboardButton.WithUrl("Go to chat", "https://t.me/my_chat_username")与您的聊天链接一起使用。

Use this code:使用此代码:

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);
}

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

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