Telegram provides free but extremely useful bot API.
Here are my notes about building a bot on my Raspberry Pi server.
I’m quite into Golang these days, so I made up my mind to build things with it.
So if you want to follow my notes, you need a running Raspberry Pi and Golang installed on it.
1. Create a bot library
I could use other developers’ so many Telegram bot libraries, but I wanted to learn about the API, so I made one.
You can find my library here.
I’ll use this library all through this post, so let’s install it anyway:
2. Create a bot on Telegram
It should be done in (maybe official?) Telegram applications.
Search for botfather and start conversation with it.
Input /newbot or select it from the command menu and follow the instruction from the bot,
then you’ll get a token like this:
You can run a bot with this token from now on.
If you search for your bot, it will be found like this:
OK, good to go on!
3. Create a self-signed certificate for callbacks from Telegram
Telegram demands SSL connection on bots’ webhook callbacks.
Fortunately, it supports self-signed certificates, so I didn’t have to waste my money on it.
I had some difficulties in generating proper certificate and key file.
If I read this page carefully, it wouldn’t have been so hard.
I made a simple shell script that will generate self-signed certificate and key so no one else would suffer from it:
Change DOMAIN to your server’s and run it, then you’ll get your cert.pem and cert.key file.
4. Testing with a simple echo bot
Now let’s start with a simple echo bot.
This bot will echo back received messages:
Save this code to a file named echo_bot.go.
Edit ApiToken, WebhookHost, and WebhookPort to yours.
Make sure that your WebhookPort is one of 80, 88, 443, or 8443.
Now try running it:
Whenever you send messages to this bot, it will send them back to you:
Echo bot is good for testing, but not enough for Raspberry Pi, so let’s add some more features on it.
5. Creating a bot for Raspberry Pi
I want the bot to function as a server-status checker.
For example, it should return the uptime of Raspberry Pi when I input /uptime command and so on.
Let’s add following code to the previous bot code:
and alter several lines in main():
Here is the full code for your convenience:
When starting chat with this bot, you will be asked to input commands:
then it will respond to each of them as you select.
6. Trouble shooting
Is your bot not working as expected?
Check your Raspberry Pi’s network connection.
Is the port(80, 88, 443, or 8443) open to public? If not, open it.
Do you see this kind of message repeated again and again?
2015/11/05 18:59:24 http: TLS handshake error from 184.108.40.206:38869: remote error: unknown certificate
Check your certificate. It could happen if your domain does not match.
Are you behind a firewall?
If you cannot open ports to public, you can setup a bot to crawl updates periodically.
Use this sample code.
With Telegram’s bot API, so many things can be achieved easily.
If you add some more code, you could control not only your Raspberry Pi but all of your home appliances remotely.
It can be a nice entry point to the so-called ‘IoT’.
I love Telegram. It is the best IM platform in the world :-)