8 Jan 2015

Raspberry remote control with Telegram

Submitted by Max

Telegram is a very versatile instant messaging software that can be used with the same phone number on different devices simultaneously.
In this tutorial we saw how to install it, and we tried to send text and media messages.
We have also seen that it is possible to set the Raspberry to send messages automatically.
In this tutorial we will ask Raspberry to take a specific action as a function of the received message, for example, we could send a text message with the word "photo" and Raspberry will sends us a photo of the apartment, or "lamp" to turn a lamp, or "open" to open the garage door.
Well, let's start

You need:

a Raspberry Pi B or B+, with the latest version of Raspbian, or our MIcroSD Card 8GB Class 10 Raspbian preinstalled.


Read this tutorial, we use this configuration as start point.

To intercept a new incoming message we create a file action.lua

"Lua is a powerful, fast, lightweight, embeddable scripting language.

Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping." From http://www.lua.org.

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

with this content

function on_msg_receive (msg)
    if msg.out then
    if (msg.text=='ping') then
       send_msg (msg.from.print_name, 'pong', ok_cb, false)
function on_our_id (id)
function on_secret_chat_created (peer)
function on_user_update (user)
function on_chat_update (user)
function on_get_difference_end ()
function on_binlog_replay_end ()

Save and exit, when incoming text message is "ping", Telegram answers us with a text message containing "pong".

move in tg

cd /home/pi/tg

then type

bin/telegram-cli -k tg-server.pub -W -s action.lua

Try to send a message, if all goes well, Telegram answers only on "ping" and not "PING", we should see something like this

Raspberry Telegram onreceive


Raspberry Telegram onreceive

Ok, let's do something more interesting.

Install the Raspberry Camera, see this tutorial, then create a new folder where we will save the captured photos.

sudo mkdir /home/pi/camera

create a new file camera.sh

sudo nano /home/pi/camera/camera.sh

with this content

raspistill -w 800 -h 600 -o /home/pi/camera/photo.jpg 


save and exit, give it execution permissions

sudo chmod -R 0655 /home/pi/camera/camera.sh

Edit action.lua

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

add this lines in function on_msg_receive

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

Raspberry Telegram action.lua

Test it

bin/telegram-cli -k tg-server.pub -W -s action.lua

Now if you send a text message with "photo", Raspberry answer with a photo

Raspberry telegram send photo

Raspberry photo with Telegram

To enter additional commands simply change the file action.lua inserting a new if block , for example, we could activate a relay or ask the status of a sensor.

In the next tutorials will achieve some other example of use.

Follow us on social to stay informed.


Thank you very much, great article! Your setup using lua is working for me, but it seems to detect only incoming messagges from other telegram accounts. I'd like to use my own telegram account to controll the rpi. It'd be great to use the same account both on the ohone and on the pi.
Is there a way to do this?
Many thanks!

You can use the same account on your phone and raspberry, but you can not send messages to yourself

As the other guy @A_bet said, I've registered with the same number as my phone, as I suspect most people will want to do. When I send messages to myself, I receieve them in the Pi, and the other way around as well. However, the lua script doesn't get activated, probably because these are taken as outbound messages. Any way to work around that?

What's the "if msg.out then" check in the script... does it check direction of the message? Maybe that's something that can take care of this situation?



If it helps, here's a bit of debugging - 

When messages are sent from my phone to my Pi, msg.flags=16 and msg.unread=false

In the other direction, msg.flags=48 and msg.unread=true

Any idea what the flags mean? Maybe this can be used to determine whether to action on the message (and avoid a loop)?


I removed the "if msg.out then" check from the script and I was able to get the automated response back on my "self account".  Which was not occuring with that block of code in there.

Is there better user giude or reference maual documentation on telegram-cli?  For example, looks like "msg" is an object with methods.  How do we find out more about these objects?


There is a way to get information on the weather? for example sending "weather london" and getting forecast info with telegram