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.


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

sudo nano /home/pi/tg/

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/ 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/

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/

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/


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

7 Responses

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

    • is the tutorial working for you? i did the same thing but its not working for me
      if you can help this is my mail “” please mail me.

  2. i did exactly as you did. i can interact and send msgs to other mobiles through pi’s telegram-cli, but am not able to run those scripts. how do pi knows to which no. it should respond?
    why did u use lua script especially ? if you can do scripting in python itself.
    how to know if my script is actually working, where to place the script file, what if i dont give the chmod +x permissions… too many question i have. if you reply, please reply in english

  3. Great tutorial!
    Worked for me, but telegram-cli deamon stops responding after sending message, even it’s via other screen daemon. Anyone has an idea why?

  4. Thank you very much for this post, really useful.

    Do you think is possible to pass parameters via Telegram ? Like “reboot 5” for instance to reboot in 5 min… but with that number being a real parameter.

    I’m looking for a way to pass parameters via telegram to once of my scripts in a dynamic way.


Leave a Comment

Your email address will not be published.

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