How to use Go to send Telegram messages to your phone
A short guide on how to use Go to send messages via Telegram from your server
This guide will present the steps to create a Telegram bot and how to use it to send messages to your phone or tablet. All you need is the Telegram app and basic knowledge of Go.
Audience
Software Engineers, DevOps Engineers, SysAdmins, other people that know or plan to learn Go
Prerequisites
- Computer with Linux or macOS
- Go installed on that computer — instructions
- Git installed on that computer — instructions
- Smartphone or a tablet with a SIM card on it (the phone number will be needed by the Telegram app)
Steps
[ 1 ]. Create a Telegram Bot
Install the Telegram app on your phone or tablet:
- For Apple devices: App Store
- For Android devices: Google Play
Follow the instructions to create an account on this app if you don’t have it already.
Open the app and in the top right corner you should be able to find the search icon. There, search for the "botfather".
Now, use the /newbot command and follow the instructions from the Telegram app as presented in this screenshot:
In your computer open a terminal window, put the token in a variable and export it like in the following command:
# This is just an example, use your own unique tokenexport TOKEN="0123456789:FAEEbVYdi_MyqlL7NTzyvR1PT9JidSLotJ0"
Leave the terminal window open for the next steps.
[ 2 ]. Find out the chat ID
Now you need to find the chat ID of your Telegram username. This is needed because the bot will have to know the destination for the messages that will be sent.
To find it, you have to message your bot, as shown below and to run a GET request after that. So, let’s do that.
In your phone or tablet, open the Telegram app and search for your bot name, press the start button bellow. This will send the first message, which will be “/start”.
Now, we can go in the same terminal window and run the following command:
curl -s https://api.telegram.org/bot${TOKEN}/getUpdates
The output should be similar to:
{"ok":true,"result":[{"update_id":116133001,"message":{"message_id":1,"from":{"id":12345678,"is_bot":false,"first_name": "Marius","username": "<USERNAME_REMOVED>","language_code":"en"},"chat":{"id":90851090,"first_name":"Marius","username":"<USERNAME_REMOVED>","type":"private"},"date":1627153519,"text":"/start","entities":[{"offset":0,"length":6,"type":"bot_command"}]}}]}
I know, that is long and ugly, but bare with me. The info that is needed further is formatted with bold — “from”:{“id”:12345678
In the same terminal window type the following:
export CHAT_ID="12345678"# This is just an example, use your own chat ID
[ 3 ]. Send the message by using Go
On the terminal window, run the following:
git clone https://github.com/marius-lupu/medium.com.git
cd medium.com/Go/send-telegram-message/
go run main.go -message "Hello There"
The output should look like:
INFO[0000] Message 'Hello There' was sent
INFO[0000] Response JSON: {"ok":true,"result":{"message_id":8,"from":{"id":1005622813,"is_bot":true,"first_name":"TutorialFromMediumBot","username":"TutorialFromMediumBot"},"chat":{"id":12345678,"first_name":"Marius","username":"<USERNAME_REMOVED>","type":"private"},"date":1627155181,"text":"Hello There"}}
And now, you should receive a message on your smartphone or tablet:
That’s it!
In the following part of this article, I will explained a bit the magic in the Go program.
I can’t paste the entire source code in this article, so, I will add a link to GitHub and I will paste here only the most important part — github.com
- In the SendMessage function, at line 44, the url variable will look like this:
https://api.telegram.org/bot<TOKEN>/sendMessage - From lines 45–48, we have to add in the JSON payload that will be used in the POST request. There, we have to add the chat ID and the text message that needs to be sent.
- From line 49–53, you can see the actual POST request that will sent the message.
- We have to close the request in line 59 and that’s it.
Advance section
- Documentation for Telegram WebHooks, a way for the bot to perform an action when a certain message is sent in the chat — telegram.org
More info
- Some useful info about the Telegram bots: core.telegram.org
- Documentation for the bot API: core.telegram.org
- Documentation for the http Go package: pkg.go.dev