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.
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
- Telegram: https://telegram.org/
- Telegram for the Linux CLI: https://github.com/vysheng/tg
- The Lua programming language: http://www.lua.org/
« Previous 1 2 3 Next »
Buy this article as PDF
Pages: 4
(incl. VAT)