Minimal send message demo

This tutorial explains how to send a message using the API. There is another full tutorial available on deploying a bot.

If you are new to Telegram bots first check the introduction to bots and take a general look to the introduction to the bot API. To run this example you will need the Token authenticating your bot (see here). Get it, you will use it below!

If you haven’t installed the package you may call either the automated install with:

Get["https://raw.githubusercontent.com/dih5/TgBot/master/BootstrapInstall.m"]

Or temporary use the online version with:

Get["https://raw.githubusercontent.com/dih5/TgBot/master/TgBot/TgBot.m"]

Regardless of what you did, load the package now with

Needs["TgBot`"]

You can check the package provided functions with ?. The basic function is BotAPICall

?BotAPICall

It takes three arguments: the method to call, the args to us for this call, and a description of your bot in a dictionary style. Full bots are suggested to keep this in a separate file, but it needn’t be so.
As an example we will call the getUpdates method with no arguments. The only relevant part of the bot description is the Token, which you must paste below.

BotAPICall["getUpdates",{},{"Token"->"PasteYourTokenHere"}]

If the call succeeded you will see an object returned by the call. The first item should be “ok” -> True, which means the Telegram servers processed it. If this failed probably the token is wrong or you have network connection issues.
The second argument should be “result” -> {(something)}. If there is nothing in brackets it means your bot has not recently received any message. Otherwise you should see some info there. Try to talk to your bot and check how this changes. Note messages are deleted when they are around 24h old.

You can use the output above to find your user ID (yours, not your bot’s). An example of sending a message to a user follows, where you must replace 100000000 by your user ID as well as the token:

result=Block[{},Pause[1];(*This is a heavy computation*);Pause[1];2+2];
BotAPICall["sendMessage",{"chat_id"->100000000,"text"->"Finished calculations. Result found was:\n"<>ToString[result]},{"Token"->"PasteYourTokenHere"}]