Mobile instant messaging from the Pi

Programming

Messaging, chatting, and sending files is all very well, but what makes the command-line version of Telegram really useful (or at least much more fun) is the ability to inject scripts and execute actions when certain events fire.

Scripts are written in Lua [3], a simple programming language similar to Python, and you inject them into the command-line Telegram client by running it with the -s parameter:

$ ./telegram -s yourscript.lua

Listing 1 is an example of a minimal skeleton of a Telegram Lua script that sets one global variable and then lists a series of events wrapped in functions that can trigger your code. Everything listed in Listing 1 is necessary and, if you take anything out, the script crashes.

Listing 1

skeleton.lua

01 our_id = 0
02
03 function on_msg_receive (msg)
04 end
05
06 function on_our_id (id)
07    our_id = id
08 end
09
10 function on_secret_chat_created (user)
11 end
12
13 function on_user_update (user)
14 end
15
16 function on_chat_update (user)
17 end
18
19 function on_get_difference_end ()
20 end
21
22 function on_binlog_replay_end ()
23 end

Listing 2 shows an example of a working script. This script sets up an "answering machine" that texts your friends while you're busy. To do this, you use the on_msg_receive Telegram event (lines 3-15).

Listing 2

busy.lua

01 our_id = 0
02
03 function on_msg_receive (msg)
04    if msg.out then
05       return
06    end
07    if msg.text == nil then
08       return
09    end
10    if msg.unread == 0 then
11       return
12    end
13
14    send_msg(msg.from.print_name, "I'm busy right now. Text you back later.")
15 end
16
17 function on_our_id (id)
18    our_id = id
19 end
20
21 function on_secret_chat_created (user)
22 end
23
24 function on_user_update (user)
25 end
26
27 function on_chat_update (user)
28 end
29
30 function on_get_difference_end ()
31 end
32
33 function on_binlog_replay_end ()
34 end

Because Telegram also shows the messages you send, you must check that the message you are responding to is not one of your own outgoing messages (line 4). This security feature is a very important, because responding to an outgoing message with another message would lead to again answering an outgoing message with a message, thus replying to an outgoing message with another message  … and so on, creating an infinite loop that would flood your friend's chat session.

In line 7, you check to see whether the text you are sent is empty – in which case you would assume it was sent by mistake and do nothing (line  9)  – and whether it is unread or not (line  10).

If the message is not an outgoing text, not empty, and not read, you use the Telegram send_msg() function in line 14 to send an automatic text to your friend. This function takes two parameters: the name of the person you're texting (which, in this case, you grab from the telegram msg.from.print_name variable that contains the username of the previous message) and the text you want to send enclosed in quotes.

You can see what an exchange would look like in Figure 4.

Figure 4: Autoresponse script from Raspberry Pi.

Conclusion

Using an instant messaging system designed for smartphones on the Rasp Pi's command line might seem counterintuitive, until you see what you can do with it. Apart from the Telegram answering machine solution, some versions of Telegram can be accessed from the web or pop up notifications on your desktop every time you get a message. This is much more than WhatsApp can do!

Although the example shown in this article is a bit silly, this technology has some serious practical applications. For example, you could use Telegram to start and end processes on your Rasp Pi and even control external devices plugged in to the GPIO pins. The sky's the limit!

Infos

  1. Telegram: https://telegram.org/
  2. Telegram for the Linux CLI: https://github.com/vysheng/tg
  3. The Lua programming language: http://www.lua.org/

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Host a secret chat using Mesh

    With Mesh, an unlimited number of Scratch projects can interact by using variables and broadcasts over a local network. We show how to communicate across Mesh by building a multiuser chat program.

  • Waking the Raspberry Pi with a wireless command

    If you want to power your Pi solely with batteries, then it makes sense for the Pi to only be turned on when you need it. The Pi can boot when called remotely via radio signal sent over the European 868 MHz band.

  • Tracking airplanes in real time with ADS-B

    Airplanes continuously broadcast signals that identify the aircraft and its current flight path. With a moderately priced receiver and a Raspberry Pi, users can receive ADS-B transponder data in real time.

  • Working with the Raspberry Pi camera module

    The amazing Raspberry Pi camera module opens into a whole new world of useful projects. We'll show you how to use the Pi camera for time-lapse photography, and we'll even help you set up a motion-detecting surveillance camera.

  • DIY body cam with a Raspberry Pi

    Make your own body cam with a Raspberry Pi, a cannibalized webcam, a WiFi module, and some Python.