What do all hack projects need? A geeky avatar of course. It may be a dirty hack, but it’s going to be a well-presented one ;-)

To get our bot to work we need to start it first, so click on its link from Botfather (or just start a Telegram chat with it directly). This will send it the /start command:

Getting the recipient’s Chat ID

We need to get the id of the recipient of messages that our bot is going to send. This can either be a direct message to you, or you can set up a group (which other real people can be members of an see the same message from the bot). If you want to use a group then make sure you start the bot ( /start per above) and then invite it to the group. Using a group is also more convenient because you could create multiple alert routes with a single bot, instead of having to create a new bot for each purpose.

Having started the bot, and optionally added it to a group and sent a message to the group, now invoke the getUpdates API:

curl -s https://api.telegram.org/bot<bot access token>/getUpdates

Replace <bot access token> with (you guessed it!) the bot access token that the Botfather gave you above. The API is a bit funky here - note that the bot is hardcoded part of the URL and should not be changed - you append your bot access token to this. So if Botfather gave you an access token of 99999:XXXXX you would invoke:

curl -s https://api.telegram.org/bot99999:XXXXX/getUpdates

From this you’ll get one, or more, messages that the bot has received. This might just be the single /start that you invoked, or it could also be group messages if you’ve added it to one. Regardless, identify the message instance corresponding to the recipient that you want for the bot and make a note of the chat.id value. Here it’s -468250841 :

{ "message_id" : 3 , "from" : { "id" : 218419044 , "is_bot" : false , "first_name" : "Robin" , "last_name" : "Moffatt" , "username" : "rmoff" , "language_code" : "en" }, "chat" : { "id" : - 468250841 , "title" : "pcap ingest monitoring" , "type" : "group" , "all_members_are_administrators" : true }, "date" : 1586894082 , "group_chat_created" : true }

You can use jq to return just the chat ID and associated recipient information too. Here it shows the group chat message quoted above, plus the DM that I sent the bot previously ( /start ).

$ curl -s https://api.telegram.org/bot99999:XXXXX/getUpdates | jq -c '.result[].message.chat | [.id , .title, .username]' [ 218419044,null, "rmoff" ] [ -468250841, "pcap ingest monitoring" ,null ]

However you do it, you should now have: