0

I'm trying to implement simple sending messages via telegram bot. Here is my code:

using System;
using Telegram.Bot;
using Telegram.Bot.Types;

namespace ConsoleApp2
{

  class Program
  {
    public static string ApiKey { get; set; } = "MyApiKey";
    static void Main(string[] args)
    {
        TelegramBotClient client = new TelegramBotClient(ApiKey);
        client.SendTextMessageAsync(MyChatId, "Hello World");          
    }
  }
}

But this code dosen't send any message. Any ideas?

Ivan_47
  • 463
  • 1
  • 5
  • 18

1 Answers1

2

SendTextMessageAsync method returns a Task. Try awaiting it.

static async Task Main(string[] args)
{
    TelegramBotClient client = new TelegramBotClient(ApiKey);
    await client.SendTextMessageAsync(MyChatId, "Hello World");
}
Pavel Shastov
  • 2,657
  • 1
  • 18
  • 26
  • It works, thank you. How do you think, is it possible to use "SendTextMessage" without "await"? – Ivan_47 Feb 08 '20 at 18:13
  • It seems there is no synchronous alternative in the TelegramBotClient. If you have to, you can synchronize it. For example like this client.SendTextMessageAsync(MyChatId, "Hello World").GetAwaiter.GetResult() But I would avoid it if possible because there are caveats with deadlocks and exceptions handling. More details: https://stackoverflow.com/questions/17284517/is-task-result-the-same-as-getawaiter-getresult – Pavel Shastov Feb 08 '20 at 18:28