Introduction: Telegram on Raspberry Pi

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.
Step 1: 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
To send a photo type
send_photo Name_Lastname /folder/photo.jpg
Other supported commands are:
Messaging
msg Text - sends message to this peer
fwd - forward message to user. You can see message numbers starting client with -N
chat_with_peer starts one on one chat session with this peer. /exit or /quit to end this mode.
add_contact - tries to add contact to contact-list by phone
rename_contact - tries to rename contact. If you have another device it will be a fight
mark_read - mark read all received messages with peer
delete_msg - deletes message (not completly, though)
restore_msg - restores delete message. Impossible for secret chats. Only possible short time (one hour, I think) after deletion
Multimedia
send_photo - sends photo to peer
send_video - sends video to peer
send_text - sends text file as plain messages
load_photo/load_video/load_video_thumb/load_audio/load_document/load_document_thumb - loads photo/video/audio/document to download dir
view_photo/view_video/view_video_thumb/view_audio/view_document/view_document_thumb - loads photo/video to download dir and starts system default viewer
fwd_media 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 - sets userpic. Photo should be square, or server will cut biggest central square part
Group chat options
chat_info - prints info about chat
chat_add_user - add user to chat
chat_del_user - remove user from chat
rename_chat
create_group_chat ... - creates a groupchat with users, use chat_add_user to add more users
chat_set_photo - sets group chat photo. Same limits as for profile photos.
Search
search pattern - searches pattern in messages with peer
global_search pattern - searches pattern in all messages
Secret chat
create_secret_chat - creates secret chat with this user
visualize_key - prints visualization of encryption key. You should compare it to your partner's one
set_ttl - sets ttl to secret chat. Though client does ignore it, client on other end can make use of it
accept_secret_chat - manually accept secret chat (only useful when starting with -E key)
Stats and various info
user_info - prints info about user
history [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 - 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
Step 2: 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 -Wsave 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"
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 -Wsave 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
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.











hi, im doing a Owncloud using Raspberry Pi. How to send the notifications to Telegram when the files that in my Owncloud has been edited or created ?so far Owncloud just send notification to email only. anyone can help please?
Makefile:48: recipe for target 'objs/interface.o' failed
while make command this error occures. can anyone help
All the steps worked but:
Then I get:
I use a Raspberry Pi 3. Any ideas on how to solve this.
Ps I initially had an issue with libjannson, but on
https://www.domoticz.com/wiki/Installing_Telegram_... I found how to install the libjansson.
I found a solution on https://github.com/vysheng/tg/issues/1070
So what I did:
static unsigned long long BN2ull (TGLC_bn *b) {
if (sizeof (unsigned long) == 8) {
return TGLC_bn_get_word (b);
} else if (sizeof (unsigned long long) == 8) {
//assert (0); // As long as nobody ever uses this code, assume it is broken.
unsigned long long tmp;
/* Here be dragons, but it should be okay due to be64toh */
TGLC_bn_bn2bin (b, (unsigned char *) &tmp);
return be64toh (tmp);
} else {
assert (0);
}
}
static void ull2BN (TGLC_bn *b, unsigned long long val) {
if (sizeof (unsigned long) == 8 || val < (1ll << 32)) {
TGLC_bn_set_word (b, val);
} else if (sizeof (unsigned long long) == 8) {
// assert (0); // As long as nobody ever uses this code, assume it is broken.
htobe64(val);
/* Here be dragons, but it should be okay due to htobe64 */
TGLC_bn_bin2bn ((unsigned char *) &val, 8, b);
} else {
assert (0);
}
}
hello, i have this problem too, but you dont say to run make again ....:) thanks
so after that we have to run :
cd ..
make
thanks it works
It is a very nice instruction, the only problem is that I cannot run python programs using telegram cli, which is unfortunate because I cannot grasp lua syntax.
Hi man, first congratulation for your tutorial, but i have a problem.
When I write bin/telegram-cli -k tg-server.pub -W
the program don't ask me for telephone, what can i do??
Ciao, in primis complimenti per il tutorial, ma ho un problema.
Quando scrivo bin/telegram-cli -k tg-server.pub -W
il programma non mi chiede il numero di telefono, cosa posso fare??
Hi all
I am new to this when i tried to use the make command i got the following error
No targets specified and no makefile found. Stop
Any help would be greatl appreciated thanks
Hi, I followed the instructions, but when I run the 'make' command, I get this error. Any suggestions?
pi@raspberrypi:~/tg $ make
make: *** No targets specified and no makefile found. Stop.
Simple instruction how to run Telegram on rpi:
https://eltechs.com/run-telegram-on-raspberry-pi/
I'M Stuckkkk!!!!!!!
Everyything works fine till 1St step i.e message is sent manually with commands
but while sending the message automatically I'm getting an error which says:
FAIL: 38: can not parse arg #1
> safe_quit
> > All done. Exit
any soln to this??
thank u
Hello, you may try like that...
#!/bin/bash
to=$1
msg=$2
tgpath=/home/pi/tg
cd ${tgpath}
${tgpath}/bin/telegram-cli -k tg-server.pub -W -e "msg $to $msg"
It worked! Starting a Lua script (all through SSH) so I can message my Raspberry Pi in the middle of the day and receive an intelligent response. To run a Lua script asynchronously, with Telegram, replace `bin/telegram-cli -k tg-server.pub -W` with `bin/telegram-cli -k tg-server.pub -W -s script_name.lua`
hey how did u manage to send message automatically??can u help me out??
Hi there, I was able to do all the above steps. However, I am trying to send a picture from a mobile to the raspberry pi. Any idea how I will be able to see the photo? Which folder will the photo be saved in. Please help! Thanks !
Thank you this is awesome. Had some problems with access, but used "sudo" and then after some do-overs it worked - thank you
BTW - I am running this on the QENU - emulator
great
very good.
I can't even get beyond installation because I get an error:
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 python... enabled
checking for python build information...
checking for python3.4... no
checking for python3.3... no
checking for python3.2... python3.2
checking for main in -lpython3.2... no
checking for main in -lpython3.2m... no
checking for main in -lpython3.2mu... no
checking for python3.1... no
checking for python3... python3
checking for main in -lpython3... no
checking for main in -lpython3m... no
checking for main in -lpython3mu... no
checking for python2.7... python2.7
checking for main in -lpython2.7... yes
results of the Python check:
Binary: python2.7
Library: python2.7
Include Dir: /usr/include/python2.7
checking for libjansson... enabled
checking for json_array_set_new in -ljansson... no
configure: error: No libjansson found. Try --disable-json
Can you offer any help?
you can try..
./configure --disable-python
and then..
./configure --disable-python --disable-json
and the last, type 'make'
is it work?
Hai!
it's great. and i success to install telegram on my raspi. but, i still wondering. my raspi has a GPS Module. and i wanna send my location from my raspi to other friends. but, i don't know what is the command for send the current location? any one can help? thanks :)
Hi!
Great stuff! I managed to send myself a message with this. However, when I go to step 2 something weird happens. It seems like it cannot parse the argument anymore for some reason. I also tried to use to user number (see second image), but this didnt work either and I got a AUTH_KEY_UNREGISTERED error. So I assume something went wrong with authorization of my number. But it does work when I try to send a message the "regular" (step1) way. Does anyone have the same issue or does someone know what is causing this and how I can solve it?
Great!
Thanks
Just a heads up if you're here planning to mess around with this: the TG conveniently opens up a TCP server, with the command line option -P. Thank you for this great instructable!
hi
i got:
pi@raspberrypi ~/tg $ make
make: *** No targets specified and no makefile found. Stop.
pls help
The problem is you don't have compiler so configure script does not generate the Makefile properly.
Try
sudo apt-get install gcc build-essential
now its:
~/tg $ -bash: bin/telegram-cli: No such file or directory
-bash: -bash:: command not found
tnx
Check that bin/telegram-cli exists
Hi, in my case it does not exist... what to do then?
Love to hear from you, and thanks in advance.
Kind regards
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
Nice instructable! But when i run the command bin/telegram-cli -k tg-server.pub -W it givee me an error saying auth_key_id should be null
If you want run ANY command safely by Telegram in RaspberryPi or any Linux system I have just published 'Obedience'
Test it!
https://github.com/GuillermoPena/obedience
When I run MAKE i get the following error messages, can someone give me some hints on what may be wrong and why??
/usr/src/tg/tgl/tgl-timers.c:43: undefined reference to `event_new'
collect2: ld returned 1 exit status
Makefile:52: recipe for target 'bin/telegram-cli' failed
make: *** [bin/telegram-cli] Error 1
Please Help , after i "Make" it just stop here . and no more run .
gcc -I. -I. -g -O2 -I/usr/local/include -I/usr/include -I/usr/include -I/usr/in lude/lua5.2 -DHAVE_CONFIG_H -Wall -Wextra -Werror -Wno-deprecated-declarations -fno-strict-aliasing -fno-omit-frame-pointer -ggdb -Wno-unused-parameter -fPIC - quote ./tgl -c -MP -MD -MF dep/auto/auto.d -MQ objs/auto/auto.o -o objs/auto/aut .o auto/auto.c
It takes time around 15-20 mins or sometimes more than that.. Try running "make clean" and then running "make" again.
I try on Galileo as it is not debian so that i am facing problem in installing libraries
sudo apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev make
for this any alternative things please tell me
opkg install lebreadline-dev i found but other are unknown packages for Intel IOT so please tell me can it externaly add is it possible
Great! but the automatic script wont work :(
Hi thanks for this! Is there any way to logout? there is no command in the "help"..
Thanks for the great tutorial, everything is working fine, except when i try to send a message with the script
The message is prompted to early i guess.
I: config dir=[/home/pi/.telegram-cli]
> msg Test_User TestMessage
> safe_quit
User Test User updated photo
User Telegram updated photo
It's above all the user lines and when I see your example it's underneath and then quiting.
Any idea how to delay prompting the message?
Rgds
Bart
Thanks for the guide! trying to follow it i'm stuck at step 2. My output looks a bit different than yours and the message just isn't sent. Here how it looks like.
Any help would be appreciated!
me too are locked there, the program doesn't ask for the telephone number to register.
I have tested the tutorial with 2015-01-31-raspbian.img and telegram asked me the number. Try to wait a few seconds without typing anything after the command bin/telegram-cli -k tg-server.pub -W
tested but still nothing:
bin/telegram-cli -k tg-server.pub -W
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=[/home/pi/.telegram-cli]
[/home/pi/.telegram-cli] created
[/home/pi/.telegram-cli/downloads] created
>
and here it does nothing
SOLVED MY PROBLEM!
if the program doesn't ask for your telephone number I suggest to run the program with: telegram -k /etc/telegram/tg.pub -W -vvvv, so you are able to see thee dirty work....
In my case I had problem connecting to the server, but when I changed from one internet provider to another, the normal behavior returned.
after I was able to register the telephone number with the alternative connection the program works on my normal connection too.
Hi, I suggest to pass by and try the lua script (see https://www.instructables.com/id/Raspberry-remote-control-with-Telegram/) . It worked for me!
but you didn't specify any telephone number?