3

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");
    }
2
  • You'd need to use result = await DoSomethingAsync() (as I indicated in my now deleted answer), but that's not possible in the Main() method. See stackoverflow.com/questions/9208921/… Commented Feb 10, 2016 at 21:47
  • I have a same problem.Can you solve it? Commented Jul 6, 2017 at 7:17

3 Answers 3

3

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!

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

1 Comment

Please instead link , put your example here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.