Do you know Telegram? It’s an amazing app for instant messages, like a better version of WhatsApp (this is only my opinion). Since I’m a developer I love the simple APIs of this app and how easy is, for example, create a bot and use it for send messages.

First of all, you need to create a Telegram Bot, the sender of your messages.

Create the BOT

To create a bot, go in you Telegram app and add in your contacts @BotFather. This is bot used to.. create other bot! 🙂

Once you have added it, you can “chat” with the bot giving him the commands to execute to create your own bot. The available commands are:

/newbot – create a new bot
/token – generate authorization token
/revoke – revoke bot access token
/setname – change a bot’s name
/setdescription – change bot description
/setabouttext – change bot about info
/setuserpic – change bot profile photo
/setcommands – change bot commands list
/setjoingroups – can your bot be added to groups?
/setprivacy – what messages does your bot see in groups?
/deletebot – delete a bot
/cancel – cancel the current operation

To create your own bot:

  1. type /newbot
  2. the bot will ask you the name of your bot. Type it.
  3. the bot will ask you the username of your bot. Type it (remember: it must end with “bot”).
  4. the bot will now give you a token to use when you want to access to the Telegram API. your bot is done and you can use it!

If you want, you can use other commands to complete the profile of the bot. For example, /setuserpic for set its profile image and /setdescription to write a brief description of the bot.

Send Messages

Ok, now the bot is ready!! 🙂

From Visual Studio, download this package (the easiest way is with the Package Manager Console).

The following code is a sample for sending text messages:

//destID: destination ID
//text: text to send
public async Task sendMessage(string destID, string text)
{
    try
    {
         var bot = new Telegram.Bot.TelegramBotClient(YourBotTokenHere);
         await bot.SendTextMessageAsync(destID, text);
    }
    catch (Exception e)
    {
        Console.WriteLine("err");
    }
}

What is the destination ID?

It is the ID of a single user or of a group.

Where can I get these IDs?

You have to go to this url:

https://api.telegram.org/botYourTokerHere/getUpdates

replacing “YourTokenHere” with your own token (keep the prefix “bot”) and you will see data about groups where the bot is invited and single users who have added it. In this data, you will find the IDs you need.

NOTE: the bot send messages only at users who have already added it to their contacts or in the groups where it is already joined.

Last but not least

Another jewel in the Telegram API programming, it’s the Telegram contact @BotSupport: use this contact to chat with the Telegram Bot Platform Support, for questions and feedback from bot developers.

NOTE: I uptated the code, since the previous code is now deprecated.

Advertisements