Raspberry Pi Telegram BotIn this project, we will be building upon our Telegram tutorial by showing you how to utilize Telegram to control your Raspberry Pi. We will be doing this by utilizing the LUA interpreter that is built into the Telegram CLI which opens it up to quite a wide variety of functionality.

Basically, the LUA interpreter allows us to program certain functions that trigger when something occurs on the Telegram CLI, such as it receiving a message.

This allows us to do certain actions based on the received message, the two simple tasks we will be showing you how to do is a simple receive message and reply, and also how to setup the Telegram CLI so we can trigger an outside bash script and upload a photo automatically.

Before proceeding with the tutorial, ensure that you have completed our Telegram CLI tutorial as that sets up the basis for this guide.

Equipment List

Below are all the bits and pieces that I used for this guide on controlling your Raspberry Pi with Telegram CLI.

Recommended:

Raspberry Pi 2 or 3

Micro SD Card

Ethernet Network Connection or Wifi dongle (The Pi 3 has WiFi inbuilt)

Optional:

Raspberry Pi Case

Writing your first script for Telegram CLI

Before you begin this tutorial please make sure that you have followed our “setting up NGINX” guide and installed PHP. We will be using that as a base for our Raspberry Pi eBook server, due to its speed and memory efficiency.

1. Before you tackle this tutorial, you will first have had to complete our base Telegram tutorial as this takes you through all the steps of setting up Telegram on the Raspberry Pi.

Once you have finished setting up Telegram you can continue with this tutorial. With this tutorial will be showing you how to utilize the Telegram CL’s LUA interpreter so we can actively monitor the received messages and make actions based on what we have received. Basically setting up our own Raspberry Pi telegram bot that will respond to commands that are sent to it.

For those who don’t know LUA is a very powerful, fast, lightweight and embeddable scripting language. Many applications use LUA to be allow people to easily modify and extend their application easily.

In the Lua Script we will be writing there will be 7 different functions we must implement, even though we will only be using one of these we need to have all the functions in place to stop continuous warnings.

This LUA script will be basic, with it replying to every message that says “ping” with “pong”, while simple it will give you an idea on how to handle text messages.

Let’s begin writing our LUA script for Telegram by running the following two commands on our Raspberry Pi.

cd ~/tg
nano actions.lua

2. With our actions.lua file opened we will need to write the following lines of code into it. We will explain each section as we write them into the file. Remember to retain the tabs as LUA is whitespace sensitive.

function on_msg_receive (msg)
      if msg.out then
            return
      end
      if (msg.text=='ping') then
            send_msg (msg.from.print_name, 'pong', ok_cb, false)
      end
end

This is the main function that we will be using in this tutorial, this function is called every time that the Telegram CLI receives a new message. We first make sure that this isn’t a message that we just sent by checking if msg.out is true.

If it is false we then proceed to check the content of the message. If the message is equal to the string “ping” we then proceed to send a message back in reply

function on_our_id (id)
end

When this function is called it provides the id of the currently logged in user. We do not need to implement anything for this as we are focusing on receiving and replying to messages.

function on_secret_chat_update (user, what_changed)
end
function on_user_update (user, what_changed)
end
function on_chat_update (user, what_changed)
end

These three functions are called when there is updated information on a user, the variables provided back is the user that was updated and an array of what changed.

function on_get_difference_end ()
end

This function is called after the first get_difference call. This means that all updates after last client execute have been received.

function on_binlog_replay_end ()
end

This function is called after the replay of old events has ended.

3. Once you have finished typing in the code you should end up with something like what is displayed below. Check over your code and ensure you haven’t made any mistakes.

function on_msg_receive (msg)
      if msg.out then
          return
      end
      if (msg.text=='ping') then
            send_msg (msg.from.print_name, 'pong', ok_cb, false)
      end
end

function on_our_id (our_id)
end

function on_secret_chat_update (user, what_changed)
end

function on_user_update (user, what_changed)
end

function on_chat_update (user, what_changed)
end

function on_get_difference_end ()
end

function on_binlog_replay_end ()
end

Once you are happy with the code that you have written press Ctrl + X then Y then Enter to save the file.

4. With our LUA script now written we can finally test to see if it is working successfully with Telegram. To do this we need to simply run the following two commands on our Raspberry Pi. These commands will launch Telegram CLI and send it out script to utilize.

cd ~/tg
bin/telegram-cli -k tg-server.pub -W -s actions.lua

5. 5. Now that the Telegram CLI (Command line interface) is now up and running with our script, we need to send a message to our Telegram account. The message you need to send is "ping". Immediately upon sending the message the Raspberry Pi should receive it, check whether the message matches to "ping" then reply with the message "Pong".

If you receive "Pong" on your device, you have successfully written your first LUA script that lets your Raspberry Pi Telegram bot reply automatically to messages.

Over the next section we will be diving into this concept even further, this time showing how you can use the LUA script to run a outside batch script that will take a photo and send it over Telegram to the receiver.

This will give you an idea on how you could use Telegram to do all sorts of things on your Raspberry Pi, and not be bound by LUA's limitations. Meaning you could use it to control devices over your GPIO pins if you wanted.

Automatically sending photos through Telegram

1. To proceed with this section of the tutorial you will need to of installed and setup the Raspberry Pi camera as we will be using that to automatically take photos and send them when a message is received by our LUA script.

To get started we will make a directory where we will temporary store the images we take and the bash script we will need to write for our LUA script to call.

Run the following command on our Raspberry Pi to create our new folder.

sudo mkdir ~/camera

2. With the folder created lets write our bash script, this script will basically make a call to the raspistill application so the Raspberry Pi will take a photo and save it into our new /home/pi/camera/ directory.

nano ~/camera/camera.sh

3. Within this file write the following lines of code:

#!/bin/bash
raspistill -w 800 -h 600 -o /home/pi/camera/photo.jpg

Once you have written in these two lines, save the file by pressing Ctrl + X then Y and then finally hit Enter.

4. Now before we go ahead and modify our actions.lua script we will need to make sure we can execute our new bash script from our lua script.

Run the following command on the Raspberry Pi to set the correct permissions.

sudo chmod 755 ~/camera/camera.sh

5. Finally, we can move onto modifying our actions.lua script that we wrote earlier in the tutorial. Run the following command to begin editing the file on your Raspberry Pi.

nano /home/pi/tg/actions.lua

6. In this file we need to add the following code:

Find

      if msg.out then
          return
      end
      if (msg.text=='ping') then
            send_msg (msg.from.print_name, 'pong', ok_cb, false)
      end

Add Below

      if (msg.text=='photo') then
            os.execute('/home/pi/camera/camera.sh')
            send_photo (msg.from.print_name, '/home/pi/camera/photo.jpg', ok_cb, false)
      end

This new bit of code works by checking for any message that is "photo" upon interpreting the correct message we then execute the camera.sh bash script that we wrote earlier in this tutorial.

After the script has been executed we can then utilize the Telegram CLI's send_photo function to send the photo we captured with our bash script.

Simply put this takes a photo from your Raspberry Pi camera then sends it back to the user that sent the message photo to your Telegram account.

7. Once you have written these new lines into our actions.lua script you should end up with a file that looks like what we have shown below.

function on_msg_receive (msg)
      if msg.out then
          return
      end
      if (msg.text=='ping') then
            send_msg (msg.from.print_name, 'pong', ok_cb, false)
      end
     if (msg.text=='photo') then
            os.execute('/home/pi/camera/camera.sh')
            send_photo (msg.from.print_name, '/home/pi/camera/photo.jpg', ok_cb, false)
      end
end

function on_our_id (our_id)
end

function on_secret_chat_update (user, what_changed)
end

function on_user_update (user, what_changed)
end

function on_chat_update (user, what_changed)
end

function on_get_difference_end ()
end

function on_binlog_replay_end ()
end

Once you are sure you have entered the new lines correctly you can save and quit out of the file by pressing Ctrl + X then hit Y and then Enter.

8. Now finally we can test our new code, simply run the same telegram-cli command that we ran earlier in the tutorial by running the following two commands on your Raspberry Pi.

cd ~/tg
bin/telegram-cli -k tg-server.pub -W -s actions.lua

9. With the Telegram CLI now running with your actions.lua script you can now send yourself a message that says "photo". Your Raspberry Pi should pause temporary as it takes the photo then you should be able to see the upload progress as it sends the photo.

Hopefully by now you have successfully setup your Raspberry Pi with Telegram and now have a good understanding on how to use the LUA script to allow Telegram to control your Raspberry Pi. You can obviously extend this further by setting it up to control even other devices through the GPIO pins.

You can find more documentation on the functions that you can utilize within the LUA script that Telegram CLI provides by going to the developers LUA documentation page:

We hope by the time you have gotten to this point you have learnt how to control your Raspberry Pi by using Telegram, we should of by now given you the basic understanding of how it works and how you can utilize it to do numerous things with your Raspberry Pi.

Feel free to drop a comment below if you enjoyed this Raspberry Pi Telegram Bot tutorial or need some help getting it up and running.

The Raspberry Pi Crash Course

The Raspberry Pi Crash Course

Enter your email address below to get the

Raspberry Pi crash course delivered straight to your inbox

Please check your inbox for a confirmation email!