loading

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.

Step 1: Installation

Installation:

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

#!/bin/bash  
   
  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
     os.execute('/home/pi/camera/camera.sh')
     send_photo (msg.from.print_name, '/home/pi/camera/photo.jpg', ok_cb, false)
  end

Raspberry Telegram action.lua

Step 2: Test

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.

www.emmeshop.eu

<p>If i change the lua script, it doesnt take effect - i have to reboot the system and then the change is effective. Why? how can i make script changes take effect without rebooting?</p>
<p>Thanks for this guide. Works well for me.</p>
<p>Hi I get this error when I try to take a still photo with the photo command</p><p>User Abcd online (was online [2016/05/04 14:08:45])</p><p>mmal: main: Error opening output file: /home/pi/camera/photo.jpg~</p><p>No output file will be generated</p><p> [14:03] Abcd &gt;&gt;&gt; photo</p><p> *** 1458323157.993260 lua: attempt to call a nil value</p>
<p>I've same issue. You fix it?</p>
<p>Just published a &quot;Telegram Device/server monitor/commander&quot; on github.</p><p>Feel free to use it and extend it:</p><p>https://github.com/giano/Telegrammer</p>
<p>i need help</p><p>telegram-cli: tgl/structures.c:1973: tglm_message_insert_tree: Assertion `M-&gt;id' failed.</p><p>SIGNAL received</p><p> how i can fix this ?It works, but after a few minutes it appears and exits</p>
<p>i need help</p><p>telegram-cli: tgl/structures.c:1973: tglm_message_insert_tree: Assertion `M-&gt;id' failed.</p><p>SIGNAL received</p><p> how i can fix this ?It works, but after a few minutes it appears and exits</p>
<p>i need help</p><p>telegram-cli: tgl/structures.c:1973: tglm_message_insert_tree: Assertion `M-&gt;id' failed.</p><p>SIGNAL received</p><p> how i can fix this ?It works, but after a few minutes it appears and exits</p>
<p>Good morning I have a question . Where I could find information on the send_message function , the question is I want to print a string instead of a message and can not find the way . Thanks and best regards</p>
<p>hi. I have a android project and i want to connect to tgcli and by this way send photo to telegram. how can i do this?</p>
<p>Hi, great Instructable!</p><p>I made it but..telegram logs in automatically with the first number I signed in and now I would like to change the number.</p><p>Is there any way to logout? In the help there are no instructions on this.. </p><p>I've tried to delate and reinstall everything but keeps on loggin in automatically with the old number..</p>
<p>If you would like to start afresh with telegram-cli you can delete the folder located at /home/[username]/.telegram-cli. This folder is not visible in the file listings, it can be deleted with the command: rm -rf .telegram-cli be sure to navigate to the containing folder before running the command. After deleting the folder the cli should ask for the number when run. If the cli does not ask for the number, you may execute the following command: bin/telegram-cli -q -k tg-server.pub as you would normally, but with the -q flag set.</p><p>Hope it helps.</p>
Try bin/telegram-cli -N
<p>uh, no success, but thanks )</p>
<p>bin/telegram-cli -q</p><p>Just done it myself, and its asking for a phone number again. used 2 numbers on it now</p>
<p>how if i want to run &quot;bin/telegram-cli -k tg-server.pub -W -s action.lua&quot; automatically at boot ?</p>
<p>thanks for your tutorial, I added a statement to action.lua which when user types sample, sends back output of a script which prints 30, in on_msg_receive function, for that if statement I wrote: send_msg(msg.from.print_name, os.execute(./sample), ok_cb, false) but it prints output in RPi instead of send it back to me via telegram.</p><p>How can I solve it?</p>
<p>You're missing the callback function ok_cb which is called after the send_msg and send_photo has sent</p><p>Add this into file to remove error, as it could cause errors later in sequence. this will remove nil value error</p><p>function ok_cb (extra, success, result)</p><p>end</p>
<p>I've add that function, but I still get the nil value error, any clues?</p>
<p>Nice Instructable. I actually modified the test.lua script that comes with the telgram-cli installation. In order to get this to work properly I also had to install lua-lgi and libnotify-dev using</p><p>sudo apt-get install lua-lgi libnotify-dev</p><p>I found that your action.lua script, although working gave an error message<br>*** lua: attempt to call a nil value</p><p>Instead I modified the function on_message_receive in the test.lua file and added extra commands in there within the function on_msg_receive(msg)</p><p>I now have photo taking and turning a led on and off with separate commands all working.</p><p>I will do a separate write up of this on my blog <a href="http://rbnrpi.wordpress.com" rel="nofollow"> http://rbnrpi.wordpress.com </a> (giving you credit for my source) shortly.</p><p>Here is a photo showing messages send and responses. You can see the led changing from on to off in the photos.</p><br>
<p>See my other comment about nil value</p>
<p>How did you fix the '*** lua: attempt to call a nil value' error?<br></p>
<p>Hey guys, we provide REST API for Telegram at <a href="http://jaconda.im" rel="nofollow">http://jaconda.im</a></p><p>It is much easier to use, because we take care of stability and deliverability of your messages.</p><p>Just create an account with Jaconda, and you will be able to send and receive messages over HTTP.</p>
<p>If you want run ANY command safely by Telegram in RaspberryPi or any Linux system I have just published 'Obedience'<br>Test it!</p><p>https://github.com/GuillermoPena/obedience</p>
<p>woww! thanks a lot! i made it!</p><p>can i run telegram in daemon mode? </p>

About This Instructable

57,561views

102favorites

License:

More by emmeshop:Roast Coffee With Artisan and Phidgets Raspberry datalogger with Mysql Highcharts  Remote control with Raspberry Pi and Phidget WebService 
Add instructable to: