Control Raspberry Pi via Telegram Messenger

This tutorial demonstrates how to automatically run scripts for text commands via Telegram after the installation on the Raspberry was covered in the previous tutorial. This allows you to, for example, easily query the status of your Pi’s on the go or simply perform actions remotely via your messenger.

Preparations

First I create a script that performs my actions, for example, I want to shut down my Pi, reboot and see the uptime. I prefer Python, but of course, any other script is possible, too (e.g., bash).
I name my script tg_actions.py.

sudo nano /home/pi/tg/tg_actions.py

In order to be able to execute different actions with a script, I use command line arguments, so that e.g. sudo python /home/pi/tg/tg_actions.py argument is recognized.

The List can be expanded as desired, for example, the GPIO pins could also be read out

Set up Telegram

So that Telegram can react automatically to various Interactions (receiving Texts / Images / etc.), One can create a Lua Script. An example script is available in the Telegram directory under test.lua. I will edit this file as it already has ready-made functions for receiving etc.

sudo nano /home/pi/tg/test.lua

Since I want to respond only to text messages, for the time being, is the function on_msg_receive (msg) to be edited:

In lines 15, 22, 30, 36 and 39, I call another script. This is not absolutely necessary; I wrote the alternative command in line 14. Anyone who still wants to send an answer via an additional script can create one:

sudo nano ~/tg/send_script

The content is:

So that both scripts have corresponding rights, we assign them:

sudo chmod +x ~/tg/send_script
sudo chmod +x ~/tg/tg_actions.py

If this script does not work for someone, tazzie has come up with an alternative.

Attention: It is important that the number(s) of the sender has been stored by means of add_contact, otherwise no automatic Reply can be made.

Some Users report that the lua Script shows the following error: lua: /usr/share/lua/5.2/lgi/namespace.lua:149: Typelib file for namespace 'Notify' (any version) not found
If that is the case, the inner part of

-- Notification code {{{
...
-- }}}

should be deleted from the test.lua.

 

Start Telegram

The created Script must now be started with the messenger so that it can react to messages. So that it runs in the background you can start it like this:

screen -dmS TelegramCLI ~/tg/bin/telegram-cli -s ~/tg/test.lua -k ~/tg/tg-server.pub

If an error occurs, you still have to install screen, if not already done:

sudo apt-get install screen

Here is a process with my Raspberry Pi.

 

To start it automatically, follow this tutorial or use crontab:

crontab -e

At the end of the file, write the following line:

@reboot sleep 60 && screen -dmS TelegramCLI /home/pi/tg/bin/telegram-cli -s /home/pi/tg/test.lua -k /home/pi/tg/tg-server.pub

 

For those who want to do everything in the Lua script, there is a nice Lua GPIO Extension.

One Response

  1. how to make my friends in telegram group can use the shutdown etc. command?

    Reply

Leave a Comment

Your email address will not be published.

Subscribe to Raspberry Pi Tutorials and don't miss any new Tutorial!