如何使用sendtextmessageasync方法?

问题描述 投票:0回答:2

我想使用sendtextmessageasync方法,但我不能用适当的值填充第一个和最后一个重载。

private static void Bot_OnMessage(object sender, Telegram.Bot.Args.MessageEventArgs e)
   {
        Telegram.Bot.Types.KeyboardButton button1 = new Telegram.Bot.Types.KeyboardButton("a");
        Telegram.Bot.Types.KeyboardButton button2 = new Telegram.Bot.Types.KeyboardButton("b");
        Telegram.Bot.Types.KeyboardButton button3 = new Telegram.Bot.Types.KeyboardButton("c");
        Telegram.Bot.Types.KeyboardButton button4 = new Telegram.Bot.Types.KeyboardButton("d");

        Telegram.Bot.Types.KeyboardButton[] row1 = { button1, button2 };
        Telegram.Bot.Types.KeyboardButton[] row2 = { button3, button4 };

        Telegram.Bot.Types.KeyboardButton[][] keyboard = { row1, row2 };

        Telegram.Bot.Types.ReplyMarkups.ReplyKeyboardMarkup markup = new Telegram.Bot.Types.ReplyMarkups.ReplyKeyboardMarkup(keyboard);

        bot.SendTextMessageAsync(e.Message.Chat.Id, "Hi", , false, false, 0, markup, );

        Console.WriteLine("A new message has been recieved");
   }
c# telegram telegram-bot
2个回答
0
投票

我认为这个github issue应该给你一个正确的开始。

var reply = "<b>Hello</b>\n"
            + "<a href=\"https://www.google.de\">This is a link</a>\n"
            + "<code>and a little bit code</code>";

await Bot.SendTextMessageAsync(message.Chat.Id, reply, parseMode: ParseMode.Html);

0
投票

如果当前方法和所有调用方法都具有async关键字,则可以使用await命令等待结果,并返回TaskTask<T>,其中T是要返回的类型。 var result = await SendTextMessageAsync()

否则,您可以使用SendTextMessageAsync.ConfigureAwait(false).GetAwaiter().GetResult(),但这并不理想,因为它可能导致死锁,除非调用路径中的所有调用都使用ConfigureAwait(false)。此命令表示结果可以在任何线程上返回。

更多信息可以在这里找到:http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

© www.soinside.com 2019 - 2024. All rights reserved.