
Raspberry and Telegram: prerequisites for creating a smart home
- Transfer

One Italian store groped for new ways to use Telegram (and earlier WhatsApp), installing it on a single-board miniature Raspberry Pi computer. As it turns out, the messenger can be used to remotely communicate with your own technology. Below is a translation of articles ( 1 , 2 ) from Instructables.com. If there are clarifications on the translation, write about it in the comments.
I. Telegram on Raspberry Pi
Step one: installation You will
need: Raspberry Pi B or B +, with the latest version of Raspbian, or an MIcroSD card on 8GB class 10 with the same Raspbian preinstalled.
First update the software packages:
sudo apt-get update
sudo apt-get upgrade
Install the libraries: readline or libedit, openssl and (if you want to use the configuration) libconfig and liblua. If you prefer without them, put --disable-libconfig and --disable-liblua, respectively.
sudo apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev make
Clone GitHub Archive
git clone --recursive https://github.com/vysheng/tg.git && cd tg
./configure
make
The execution of this command will take some time, after which the installation will be completed.
Upd: run the messenger and set up an account by entering the phone number and (or) nickname. See
Step Two: Send Messages
Automatically To create a message automatically, create a file
sudo nano /home/pi/tg.sh
with this content:
#!/bin/bash
to=$1
msg=$2
tgpath=/home/pi/tg
cd ${tgpath}
(echo "msg $to $msg"; echo "safe_quit") | ${tgpath}/bin/telegram-cli -k tg-server.pub -W
Save and close it, giving permission for actions:
sudo chmod -R 0655 /home/pi/tg.sh
Test it with
/home/pi/tg.sh Name_lastname "your message"


To send a photo, create a file
sudo nano /home/pi/tg_photo.sh
and write in it
#!/bin/bash
to=$1
msg=$2
tgpath=/home/pi/tg
cd ${tgpath}
(echo "send_photo $to $msg"; echo "safe_quit") | ${tgpath}/bin/telegram-cli -k tg-server.pub -W
Save and close it by giving permissions:
sudo chmod -R 0655 /home/pi/tg_photo.sh
and test with
/home/pi/tg_photo.sh Name_Lastname /folder/photo.png
II. Remote control of Raspberry Pi via Telegram

Telegram is a very versatile messenger for sending instant messages, in which you can work using one telephone number at the same time on several devices.
In the previous review, we saw how to install it, how to send text and media files. We also made sure that Raspberry can be configured to automatically send messages via Telegram.
This time we will ask Raspberry to perform a specific action as a function of the received message: for example, we could send the word “photo” so that Raspberry sends us a photo of the house, or “lamp”, so that he turns the lamp, or “open” to open the door the garage. Let's get started.
Step One: Installation
We start based on the steps described above. To intercept a new incoming message, we create an action.lua file (I omit the description of the Lua language with a link to the official website, as the habrayusers are obviously familiar with it. - Note per. ):
sudo nano /home/pi/tg/action.lua
with the following contents:
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
Save and close the file. Now that the incoming message is “ping”, the Telegram responds with the message “pong”.
Go to tg
cd /home/pi/tg
and write:
bin/telegram-cli -k tg-server.pub -W -s action.lua
Try to send a message. If everything is correct, Telegram only answers “ping” (not “PING”), and we should see something like this:


Okay, let's try something more entertaining.
Install the Raspberry camera (see this tutorial) and create a new folder where photos from this camera will be saved by default.
sudo mkdir /home/pi/camera
Create a new camera.ch file
sudo nano /home/pi/camera/camera.sh
here with this stuffing
#!/bin/bash
raspistill -w 800 -h 600 -o /home/pi/camera/photo.jpg
Save and close it, giving permission for actions
sudo chmod -R 0655 /home/pi/camera/camera.sh
Edit "action.lua"
sudo nano /home/pi/tg/action.lua
and add these lines to the on_msg_receive function:
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

Step Two: Test


bin/telegram-cli -k tg-server.pub -W -s action.lua
Now, if you send the word "photo" by message, Raspberry will respond with a photograph.
To specify additional commands, simply modify the “action.lua” file by adding a new “if” block. For example, you can activate a switch or request an alarm status.