如何显示HTML格式的内联查询

时间:2019-06-23 11:19:16

标签: .net .net-core telegram-bot

在电报Bot中,我需要在内联查询中显示HTML格式的视图:

private static async void BotOnInlineQueryReceived(
    object sender, 
    InlineQueryEventArgs inlineQueryEventArgs)
{
    Console.WriteLine(
        $"Received inline query from: {inlineQueryEventArgs.InlineQuery.From.Id}");

    InlineQueryResultBase[] results = {
        new InlineQueryResultArticle(
            id: "1",
            title : "<b>Test</b>",
            inputMessageContent :  new InputTextMessageContent("<b>Test</b>"))
    };

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

我尝试了这段代码,但是得到了以下结果:

enter image description here

我需要这样的输出:

enter image description here

2 个答案:

答案 0 :(得分:1)

如果可以使用SendTextMessageAsync,则可以指定解析模式参数以使用markdown或HTML,下面是使用HTML的示例:

private static TelegramBotClient botClient;

static void Main()
{
    botClient = new TelegramBotClient("YOUR_TOKEN");

    var me = botClient.GetMeAsync().Result;
    Console.WriteLine(
      $"Hello, World! I am user {me.Id} and my name is {me.FirstName}."
    );

    botClient.OnMessage += Bot_OnMessage;
    botClient.StartReceiving();
    Thread.Sleep(int.MaxValue);
}

static async void Bot_OnMessage(object sender, MessageEventArgs e)
{
    if (e.Message.Text != null)
    {
        Console.WriteLine($"Received a text message in chat {e.Message.Chat.Id}.");

        await botClient.SendTextMessageAsync(
          chatId: e.Message.Chat,
          text: $"You said: <b>{e.Message.Text}</b>",
          parseMode: Telegram.Bot.Types.Enums.ParseMode.Html
        );
    }
}

此处有更多信息:https://core.telegram.org/bots/api#sendmessage

答案 1 :(得分:0)

我找到了解决方法:

private static async void BotOnInlineQueryReceived(object sender, InlineQueryEventArgs inlineQueryEventArgs)
        {
            Console.WriteLine($"Received inline query from: {inlineQueryEventArgs.InlineQuery.From.Id}");

            InlineQueryResultBase[] results = {
                new InlineQueryResultArticle(
                    id: "1",
                    title : "Received *new data*",
                    inputMessageContent : new  InputTextMessageContent("Received \n Content")
                    )
                {
                    ReplyMarkup = new InlineKeyboardButton {
                        Text = "select",
                        CallbackData = "Some Callback Data",
                    },
                    ThumbUrl = "https://photo.venus.com/im/18062700.jpg?preset=product",
                    Description = "The coolest dress ever seen!",

                }
            };

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

,结果如下: enter image description here