Configuring Telegram Bot

You may want to skip this part if you’re not interested in using Telegram to control your power strip.

Botfather First of all you’ll need a Telegram account. If you haven’t got one, create one now. I’ll wait unit your finished. So grab your phone, install the telegram App and sign up as a new member. Telegram uses your phone number to identify you. But don’t worry, unlike the crappy Whatsapp you can use Telegram on any of your devices, either through an app, or through their website.

Ah, you’re back. Now we’re going to create a Telegram bot. The official user guide can be found here, in case you’re interested. However I think it’s best when I walk you through it. For this you might use the App on your phone or go to the Telegram web page.
B.T.W. here’s the site I’ve use to learn how to create a Telegram bot, in case you want to know more.

Once you’re logged in search for "botfather". That is a bot itself, created by Telegram, which will guide you through the process of setting up your own Telegram bot.

Type /help to get a list of all the commands you can send to the BotFather. We’ll obviously need the /newbot command. B.T.W. In case you haven’t noticed all commands you can send to a bot start with a slash.

From now on it is only a matter of answering the Botfather’s questions. The first one is the name of your new bot and that can be anything you like. My power strip is called Carl, one of the well known minions. The username however must be unique in the Telegram network and should end with _bot. Putting your name in front of the username could already make it unique.

Once you’re done you’ll get an HTTP API key, a long string of gibberish. You’ll need that key in a minute when you’re going to configure the power strip driver program.

Editing The Telegram Configuration

There are two ways to go here. Either you enter your API key into the file ~/.telegrampwd (or whatever you’ve called it in the configuration section of the program), or you can enter the API key directly into the program. Personally I prefer editing the ~/.telegrampwd file.
The first line of that file must be the telegram API key. All subsequent lines shall hold whitelisted user numbers. We’ll come to that in a minute.
Perhaps it's a good idea to chmod this file to prevent others from reading your secrets, chmod 600 ~/.telegrampwd .
You may instead add the API key directly into the program, in which case the file ~/.telegrampwd should not exist. Find the line TelegramToken = "yourtelegramtoken" and put your API token in there.

After entering the API token in either way you can (re)start the powerstrip control server. You do that by entering sudo systemctl restart powerstrip.service. After that you can go to your Telegram App on your phone or use the Telegram web interface. Search for your bot and give it its first command. It doesn’t matter what command, because you are not among the whitelisted users yet. The bot should now tell you that it won’t obey to you yet, but as a convenience it shows you your user ID. It outputs "Sorry, I don’t know you. 123456789". Copy that ID to the end of the file ~/.telegrampwd or add it to the Whitelist inside the program. Then restart the powerstrip control server again.

Now your bot should obey to your commands. Let’s try a simple command first /ip. This should return the Raspberry Pi’s IP address.

Just for your reference the file ~/.telegrampwd should look something like this:


Or the Telegram configuration part in the program looks like this, in which case the file ~/.telegrampwd shouldn't exist:

TelegramToken = "568756882:AAOMWCqLzoalw_3nPsc4ojkisSEQV7opaof8"
Whitelist = [123456789]
telegram_auth = ".dontcare"

Next stop we'll configure MQTT.