如何使用sendtextmessageasync方法?

时间:2018-01-21 21:34:28

标签: c# telegram telegram-bot

我想使用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");
   }

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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