27 Dec 2014

Telegram on Raspberry Pi

Submitted by Max

In one of our last tutorials we have installed WhatsApp on Raspberry.
Whatsapp at the moment is definitely the most famous app for messaging services, but there are also many alternatives although little known. One of these is Telegram.

"Telegram is a cloud-based mobile and desktop messaging app with a focus on security and speed. Unlike WhatsApp, Telegram is cloud-based and heavily encrypted. As a result, you can access your messages from several devices at once, including tablets and computers, and share an unlimited number of photos, videos and files (doc, zip, mp3, etc) of up to 1,5 GB. Thanks to our multi-data center infrastructure and encryption, Telegram is also faster and way more secure. On top of that, Telegram is free and will stay free — no ads, no subscription fees, forever." From https://telegram.org/

Well, then we try to install it on Raspberry

You need:

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

Installation:

Update the packages with

sudo apt-get update

sudo apt-get upgrade

 

Install libs: readline or libedit, openssl and (if you want to use config) libconfig and liblua. If you do not want to use them pass options --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 Repository

git clone --recursive https://github.com/vysheng/tg.git && cd tg

./configure

make

Running this command takes a little time and after install is done.

Utilization:

navigate to the folder tg

cd tg

then

bin/telegram-cli -k tg-server.pub -W

The first time we start telegram we must enter the phone number, including land code (for Italy is +39).

You should receive on your phone a sms message with a code, enter it and hit "Enter"

Now we are ready to use telegram, if you send a message from your smartphone you can see it on terminal.

To send a message type

msg Name_Lastname My message from Raspberry

Telegram on Raspberry first message

To send a photo type

send_photo Name_Lastname /folder/photo.jpg

Telegram on Raspberry send photo

Send message automatically

To send 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 exit, give it execution permissions

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

test it with

/home/pi/tg.sh Name_lastname "your message"

Raspberry Pi Telegram automatic message

Raspberry Pi Telegram alert

to send an image create a file

sudo nano /home/pi/tg_photo.sh

with this content

#!/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 exit, give it execution permissions

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

test it with

/home/pi/tg_photo.sh Name_Lastname /folder/photo.png

Other supported commands are:
Messaging

    msg <peer> Text - sends message to this peer
    fwd <user> <msg-seqno> - forward message to user. You can see message numbers starting client with -N
    chat_with_peer <peer> starts one on one chat session with this peer. /exit or /quit to end this mode.
    add_contact <phone-number> <first-name> <last-name> - tries to add contact to contact-list by phone
    rename_contact <user> <first-name> <last-name> - tries to rename contact. If you have another device it will be a fight
    mark_read <peer> - mark read all received messages with peer
    delete_msg <msg-seqno> - deletes message (not completly, though)
    restore_msg <msg-seqno> - restores delete message. Impossible for secret chats. Only possible short time (one hour, I think) after deletion

Multimedia

    send_photo <peer> <photo-file-name> - sends photo to peer
    send_video <peer> <video-file-name> - sends video to peer
    send_text <peer> <text-file-name> - sends text file as plain messages
    load_photo/load_video/load_video_thumb/load_audio/load_document/load_document_thumb <msg-seqno> - loads photo/video/audio/document to download dir
    view_photo/view_video/view_video_thumb/view_audio/view_document/view_document_thumb <msg-seqno> - loads photo/video to download dir and starts system default viewer
    fwd_media <msg-seqno> send media in your message. Use this to prevent sharing info about author of media (though, it is possible to determine user_id from media itself, it is not possible get access_hash of this user)
    set_profile_photo <photo-file-name> - sets userpic. Photo should be square, or server will cut biggest central square part

Group chat options

    chat_info <chat> - prints info about chat
    chat_add_user <chat> <user> - add user to chat
    chat_del_user <chat> <user> - remove user from chat
    rename_chat <chat> <new-name>
    create_group_chat <chat topic> <user1> <user2> <user3> ... - creates a groupchat with users, use chat_add_user to add more users
    chat_set_photo <chat> <photo-file-name> - sets group chat photo. Same limits as for profile photos.

Search

    search <peer> pattern - searches pattern in messages with peer
    global_search pattern - searches pattern in all messages

Secret chat

    create_secret_chat <user> - creates secret chat with this user
    visualize_key <secret_chat> - prints visualization of encryption key. You should compare it to your partner's one
    set_ttl <secret_chat> <ttl> - sets ttl to secret chat. Though client does ignore it, client on other end can make use of it
    accept_secret_chat <secret_chat> - manually accept secret chat (only useful when starting with -E key)

Stats and various info

    user_info <user> - prints info about user
    history <peer> [limit] - prints history (and marks it as read). Default limit = 40
    dialog_list - prints info about your dialogs
    contact_list - prints info about users in your contact list
    suggested_contacts - print info about contacts, you have max common friends
    stats - just for debugging
    show_license - prints contents of GPLv2
    help - prints this help

Card

    export_card - print your 'card' that anyone can later use to import your contact
    import_card <card> - gets user by card. You can write messages to him after that.

Other

    quit - quit
    safe_quit - wait for all queries to end then quit

The opportunities that telegram offers are very interesting and in the next tutorials we will see how to use them, for example, Raspberry could send us a photo after an alarm or ask Raspberry to do an action following a particular message, for example "turn on heating."

Follow us on social to stay informed.

 

Comments

Have followed the instructions on installation.

Tried bin/telegram-cli -k tg-server.pub -W

It asks for phone number, I enter one and hit enter. It moves to a new line but does not say anything or exit. The phone does not receive the text. I have tried a Australian number (+61) and a UK one (+44) no message arrives.

Is there any firewall setting or router routing that is needed ?

Ian

Raspberry Pi B+

ok, the code arrived 6 hours later !

Now completed the steps and send a message to myself from my phone - nothing arrived on pi, and sent a message from my pi No messages received by the phone.

Maybe another 6 hrs ???

Hi, after registering Telegram, run the script

bin/telegram-cli -k tg-server.pub -W

Raspberry must be in listen, then send a message from your smartphone to Raspberry. If all goes well you should see the exact name of the sender (eg Name_Lastname). You have to use that name written in the same way to send the message from Raspberry to smartphone

HI there

This is a great article.
I can send OK manually, but if I use the script to send a message, then it doesn't allow enough time for the system to initialise before sending the message text, so it is lost.

How can I insert a delay between starting the telegram connection and sending the message.
This is the line that causes the problem.
(echo "msg $to $msg"; echo "safe_quit") | ${tgpath}/bin/telegram-cli -k

I need a delay before the mesage command is given, allowing telegraph connection to be set up

You can use a file lua to launch Telegram and let it listen, here's an example http://www.emmeshop.eu/blog/?q=node/45

hi

====

root@ubuntu:/usr/local/bin/telegram# sh tg.sh anonyopnet "ok"
change_user_group: can't find the user telegramd to switch to
Telegram-cli version 1.2.0, Copyright (C) 2013-2015 Vitaly Valtman
Telegram-cli comes with ABSOLUTELY NO WARRANTY; for details type `show_license'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show_license' for details.
Telegram-cli uses libtgl version 1.2.0
I: config dir=[/root/.telegram-cli]
> msg anonyopnet ok
> safe_quit
> All done. Exit
halt

===

do not sending message!!!!!!!

but send message is manual method...

step1 : command ./telegram-cli -k
step2 : msg anonyopnet ok

is ok and send message from anonyopnet

====

please help

what problem :( ?

Hello, i don't understand if i have to use a phone number i actually own (for example my iphone number) or if i have to use another one.

In the case i would like to receive notification from my Pi to my phone, i have to get a new phone number (a new sim) ?

Hello, you can use your phone number, or at least a real number, because you will receive a confirmation of registration. Don't you need a new sim

 

But if i use the phone number on both the phone and RPi, isn't it confusing?

It is obviously that the number of sender should not be the same of recipient.

so i have to use another REAL phone number, right?

Yes

what i have to do if i would send a picture from an attached webcam?

You must save the photo and then create a script that sends it. Here's an example http://www.emmeshop.eu/blog/?q=node/45

When I send MP4 video from cli, I receive an attached "file" in my smartphone, named as "VIDEO/MP4", but no way to open like a video, only open like a document... why?

Elia

Probably does not open it because there is no extension .mp4

Hallo,

I have got a new phone numer and want to tell "telegram-cli" on my Raspberry Pi this number. How can I do this?

Andreas

Hello,

the very first time I start Telegram (after make) with "bin/telegram-cli -k tg-server.pub -W" I only obtain "Killed" and nothing else. What I have to do?

thanks in advance

hey guys

i have a problem in install telegram... make command doesnt work for me....i have some problem with configure command too...i use rpi2 with lastest version noobs...
this is my  /.configure log:
 
pi@raspberrypi ~/tg $ ./configure && make
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for sqrt in -lm... yes
checking for library containing clock_gettime... -lrt
checking for library containing backtrace... none required
checking for event_base_new in -levent... yes
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking event2/event.h usability... yes
checking event2/event.h presence... yes
checking for event2/event.h... yes
checking for pkg-config... /usr/bin/pkg-config
checking whether compiling and linking against OpenSSL works... yes
checking if zlib is wanted... yes
checking for inflateEnd in -lz... yes
checking zlib.h usability... yes
checking zlib.h presence... yes
checking for zlib.h... yes
checking for rl_save_prompt in -lreadline... yes
checking for libconfig... enabled
checking for config_init in -lconfig... yes
checking for liblua... enabled
checking for lua... /usr/bin/lua
checking if /usr/bin/lua is a Lua interpreter... yes
checking for lua version... 5.2
checking for lua platform... unknown
checking for lua script directory... ${prefix}/share/lua/5.2
checking for lua module directory... ${exec_prefix}/lib/lua/5.2
checking if LUA_VERSION is defined... yes
checking lua.h usability... no
checking lua.h presence... no
checking for lua.h... no
checking lualib.h usability... no
checking lualib.h presence... no
checking for lualib.h... no
checking lauxlib.h usability... no
checking lauxlib.h presence... no
checking for lauxlib.h... no
checking luaconf.h usability... no
checking luaconf.h presence... no
checking for luaconf.h... no
checking for Lua headers in... /usr/include/lua5.2
checking lua.h usability... yes
checking lua.h presence... yes
checking for lua.h... yes
checking lualib.h usability... yes
checking lualib.h presence... yes
checking for lualib.h... yes
checking lauxlib.h usability... yes
checking lauxlib.h presence... yes
checking for lauxlib.h... yes
checking luaconf.h usability... yes
checking luaconf.h presence... yes
checking for luaconf.h... yes
checking for Lua header version... 5.2
checking if Lua header version matches 5.2... yes
checking if LUA_VERSION is defined... yes
checking for library containing exp... none required
checking for library containing dlopen... -ldl
checking for library containing lua_load... -llua5.2
checking for libjansson... enabled
checking for json_array_set_new in -ljansson... yes
checking progname... default
checking execinfo.h usability... yes
checking execinfo.h presence... yes
checking for execinfo.h... yes
checking fcntl.h usability... yes
checking fcntl.h presence... yes
checking for fcntl.h... yes
checking malloc.h usability... yes
checking malloc.h presence... yes
checking for malloc.h... yes
checking netdb.h usability... yes
checking netdb.h presence... yes
checking for netdb.h... yes
checking for stdlib.h... (cached) yes
checking for string.h... (cached) yes
checking for unistd.h... (cached) yes
checking arpa/inet.h usability... yes
checking arpa/inet.h presence... yes
checking for arpa/inet.h... yes
checking mach/mach.h usability... no
checking mach/mach.h presence... no
checking for mach/mach.h... no
checking netinet/in.h usability... yes
checking netinet/in.h presence... yes
checking for netinet/in.h... yes
checking sys/file.h usability... yes
checking sys/file.h presence... yes
checking for sys/file.h... yes
checking sys/socket.h usability... yes
checking sys/socket.h presence... yes
checking for sys/socket.h... yes
checking termios.h usability... yes
checking termios.h presence... yes
checking for termios.h... yes
checking for size_t... yes
checking for uid_t in sys/types.h... yes
checking for inline... inline
checking for stdlib.h... (cached) yes
checking for GNU libc compatible malloc... yes
checking for stdlib.h... (cached) yes
checking for GNU libc compatible realloc... yes
checking for alarm... yes
checking for endpwent... yes
checking for memset... yes
checking for memmove... yes
checking for mkdir... yes
checking for select... yes
checking for socket... yes
checking for strdup... yes
checking for strndup... yes
checking for uname... yes
checking for __builtin_bswap32... yes
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: config.h is unchanged
gcc -I. -I. -I./tgl -g -O2  -I/usr/local/include -I/usr/include -I/usr/include -I/usr/include/lua5.2   -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC -c -MP -MD -MF dep/loop.d -MQ objs/loop.o -o objs/loop.o loop.c
loop.c: In function ‘write_dc’:
loop.c:350:3: error: ‘struct tgl_dc’ has no member named ‘port’
loop.c:351:21: error: ‘struct tgl_dc’ has no member named ‘ip’
loop.c:353:3: error: ‘struct tgl_dc’ has no member named ‘ip’
Makefile:49: recipe for target 'objs/loop.o' failed
make: *** [objs/loop.o] Error 1