skip to Main Content

I’m new on telegram Bot programmer and want to write a simple console application to send a message on telegram.

After some research I developed this codes with no error but it doesn’t works and does not send my message.
When i traced my code i found that status on result object is “waiting for activation” , what;s this mean?

I registered my bot and have a API access Token and used it on this codes.

Please guide me 🙂

static void Main(string[] args)
    {

        Task<Message> result;
        result= DoSomethingAsync();

        Console.WriteLine();
    }
    static async Task<Message> DoSomethingAsync()
    {
        var Bot = new Telegram.Bot.Api("my API access Token");
       return await Bot.SendTextMessage("@blablavla", "test message");
    }

3

Answers


  1. You can build you own async main-method like this:

    static void Main(string[] args)
    {
        MainAsync(args).Wait();
    
        Console.ReadKey();
    }
    
    static async Task MainAsync(string[] args)
    {
        Message result;
        Console.WriteLine("Sending Message...");
        result = await DoSomethingAsync();
        Console.WriteLine("Message sent...");
        Console.WrtieLine(result);
    }
    
    static async Task<Message> DoSomethingAsync()
        {
            var Bot = new Telegram.Bot.Api("my API access Token");
            return Bot.SendTextMessage("@blablavla", "test message"); // It's your last call to an async function in an async function, no need to await it here.
        }
    

    this should do what you want. But please note that it is untested!

    Login or Signup to reply.
  2. first create your bot

    Telegram.Bot.Api bot = new Telegram.Bot.Api("my API access Token");
    

    and then write a code like this in any method that you want. when this method runs it whould send your message to the user after each message to be sent. and will notify u in the console.

    int offset = 0;
                while (true)
                {
                    Telegram.Bot.Types.Update[] updates = bot.GetUpdates(offset).Result;
                    foreach (var update in updates)
                    {
                        offset = update.Id + 1;
                        if (update.Message == null)
                            continue;
    
                        Console.WriteLine("Sending Message...");
                        bot.SendTextMessage(update.Message.Chat.Id, "your text");
                        Console.WriteLine("Message sent...");
                        update.Message.Text);
                    }
                }
    

    try it

    Login or Signup to reply.
  3. You Can Use This Example To And Receive Send Message,Location,Inline KeyBoard,In Text KeyBoard ,Photo and…

    In this Example You can See GetUpdate And Webhook Code that Created by Your Library

    this Example Base on Telegram Bot API MrRoundRobin

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search