If you are a smartphone user, you will surely know that one of the most similar and famous applications of WhatsApp is Telegram, a very versatile messaging client which presents several peculiarities such as the possibility of being able to use the same telephone number from different devices simultaneously. Thanks precisely to this feature, today I want to present you a small tutorial with which you can send text messages and even multimedia files to your contacts from a Raspberry Pi.
As much or more interesting is the fact that we can configure our Raspberry Pi so that, before a certain command received by Telegram, our card can exercise some additional performanceIn other words, let's imagine that we send the word «photo»And this gives us an image of any room in the house,«light»To automatically turn on any light or«open»To automatically open the garage door.
Surely this additional functionality has caught your attention. If so, we get down to work, but not before telling you that we will need a Raspberry Pi B or Raspberry Pi B + to carry out the project as well as an 8 GB Class 10 microSD card with the latest version of Raspbian pre-installed.
Once we have all of the above, we start and from a Port we start with the update and basic configuration. Surely there will be many users who do not need this but we better do everything step by step and at a good pace so as not to skip anything. We start by running and updating the packages with:
sudo apt-get update
sudo apt-get upgrade
We continue with the installation and updating of several essential libraries where the system will find all the libraries we need
sudo apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 li-blua5.2-dev libevent-dev make
Shift of the repository GitHub
git clone --recursive https://github.com/vysheng/td.git && cd tg
Once we have everything installed, it is time to configure lua, a powerful and fast scripting language. The syntax is very simple, in our Terminal we execute:
sudo nano /home/pi/tg/action.lua
and we add the following content:
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 (id) end function on_secret_chat_created (peer) end function on_user_update (user) end function on_chat_update (user) end function on_get_difference_end () end function on_binlog_replay_end () end
With the above, we would have practically everything configured so that, when we send the text «ping»This will return«pong«.
We move to the tg directory
We execute the following order
bin/telegram-cli -k tg-server.pub -W -s action.lua
Now is the time to start testing and submit our «ping»To Telegram, immediately afterwards and as you can see in the image the answer is our expected«pong«. We must take into account if we use capital letters or not since the system is sensitive to their use.
If what we want is that, instead of "pong" our Raspberry Pi returns an image, in the function where we send the response we would only have to tell the system to take a photo using a previously installed camera and send it to us.