TL;DR This post is kind of a protocol of the initial setup of our Telegram Planets bot. In about 10 minutes, you can send notifications to a chat with curl.
Do not waste time. Get started!
Q: How do I create a bot? Start a chat with ... the Botfather
Creating a new bot is as easy as /newbot
and answering some questions.
You'll get a URL like t.me/<your_bot_name>_bot
and a token for the HTTP API
.
And a hint where to go next: "For a description of the Bot API, see this page: https://core.telegram.org/bots/api"
To give the bot a bit more personality, the Botfather offers some more commands:
command | description |
---|---|
/setname |
change a bot's name |
/setdescription |
change bot description |
/setabouttext |
change bot about info |
/setuserpic |
change bot profile photo |
/setcommands |
change the list of commands |
Let's look around in the Telegram Bot API...
All requests have the following format: https://api.telegram.org/bot<token>/METHOD_NAME
Calling getMe
reveals basic information about our bot:
$ curl -X GET https://api.telegram.org/bot123456:ABCDEF/getMe
{
"ok": true,
"result": {
"id": 123456,
"is_bot": true,
"first_name": "PlanetsFather",
"username": "planets_father_bot"
}
}
That was easy...maybe we should getUpdates
now?
{
"ok": true,
"result": []
}
As promised by the docs another JSON document "which always has a Boolean field ‘ok’". No updates - We have to start a chat with our fresh bot:
hello
and try again getUpdates
{
"ok": true,
"result": [
{
"update_id": 630248673,
"message": {
"message_id": 7,
"from": {
"id": 1234,
"is_bot": false,
"first_name": "planets",
"language_code": "en-US"
},
"chat": {
"id": 5678,
"first_name": "planets",
"type": "private"
},
"date": 1504891788,
"text": "Hello"
}
}
]
}
Now extract the chat id 5678
:
$ TELEGRAM_BOT_TOKEN=$(<bot-token.txt)
$ curl -s https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/getUpdates | jq -r '.result[0].message.chat.id'
5678
This chatId is where our notifications should go as well.
We are good to go for push notifications to the chat. Let's sendmessage to the chat
$ curl -X POST https://api.telegram.org/bot123456:ABCDEF/sendmessage \
-H 'content-type: application/json' \
-d '{ "chat_id": 171765575, "text": "Hello from Planets bot" }'
Nice.
The initial contact with our bot is there. Stay tuned...