Using Telegram CLI on the Raspberry Pi

In this Raspberry Pi Telegram CLI tutorial, we will be showing you how to set up the popular messaging service Telegram on your Raspberry Pi.

Raspberry Pi Telegram CLI

We will be setting up Telegram by making use of the Telegram CLI (Command Line Interface) written by a user on GitHub.

This project will allow you to send and receive messages over the Telegram messaging service on your Raspberry Pi, even going as far as also being able to send images. All you require to do this is a mobile phone, Raspberry Pi, and a mobile phone number.

Unlike WhatsApp, you can have multiple devices running off the same phone number, so you do not have to worry about having a spare phone number to be able to run it. You can extend this tutorial further by utilizing Telegram to control your Raspberry Pi.

Equipment List

Below are all the bits and pieces that I used for this Raspberry Pi Telegram tutorial, you will need an internet connection to be able to complete this tutorial.

Recommended

Optional

Setting up Telegram CLI on the Raspberry Pi

This tutorial was completed on a clean install of Raspbian but will also work on other Linux based operating systems. You will need to make a few changes to the tutorial but will work.

If you need to install Raspbian, then you can learn how to install a clean copy of Raspbian here.

1. Before we begin the process of installing the Telegram CLI (Command Line Interface) to our Raspberry Pi, we must first run an update and upgrade to ensure our Raspberry Pi is running the latest versions. Run the following two commands to update the Raspberry Pi.

sudo apt update
sudo apt upgrade

2. With our Raspberry Pi now up to date, we can now install all the libraries we need to utilize the Telegram CLI. Run the following three commands on your Raspberry Pi to install all the required packages.

sudo apt install -y libreadline-dev libconfig-dev libssl-dev 
sudo apt install -y lua5.2 liblua5.2-dev
sudo apt install -y libevent-dev libjansson-dev libpython-dev libssl1.0-dev make git

3. Now that we have all the libraries that we need to run the Telegram CLI we can proceed to clone the repository from GitHub. We need to use –recursive as we require Git to clone the remote repositories that the Telegram CLI GitHub references.

Run the following two commands on your Raspberry Pi to begin the cloning process.

cd ~
git clone --recursive https://github.com/kenorb-contrib/tg.git

4. To get the Telegram CLI to compile on our Raspberry Pi, we will first have to modify one of the source code files. Run the following command on your Raspberry Pi to begin editing the file.

nano ~/tg/tgl/mtproto-utils.c

5. Within this file we want to press CTRL + W then type in BN2ull and then press ENTER. That should take you to the following block of code.

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);
  }
}

6. Within this block of code, we want to find and replace two occurrences of assert(0) as shown below.

Find both

assert(0);

Replace with

//assert(0);

You can now save the file by pressing CTRL + X then Y and then hitting ENTER.

7. Now that we have cloned the Telegram CLI to our Raspberry Pi and made changes to the source code, we will now need to run the configuration script and compile it.

To do this we can simply run the following three commands on our Raspberry Pi, the first changes the directory to the Telegram directory, the second configures the Telegram CLI for compiling, and finally, the make command compiles it.

cd ~/tg
./configure
make

Registering with Telegram CLI on the Raspberry Pi

1. Now that we have downloaded and compiled the Telegram CLI, we can now actually start to make use of it. To proceed further in this tutorial, you will need a mobile and a mobile phone number handy. You will also need to know your country code for your mobile phone number, for example, the country code for Australia is +61.

2. Now that we have a mobile phone number with a country code ready, we can proceed with running the Telegram CLI. To do this, we need to run the following two commands on our Raspberry Pi.

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

3. You will now be asked to enter your phone number, make sure that you type in your phone number with your country code in front of it.

For example, if our phone number were 04XXXXXXXX, it would become +614XXXXXXXX when we type it in. An authentication code will be sent to this phone, and we will need it after the next step.

4. After entering your phone number, you will be asked if you want to Register, type in Y and press ENTER to continue onward.

Immediately afterward you will be asked to enter both your first name and your last name, fill out both details. Remember what you set for these.

5. Now that you have filled in your information you will now be asked to enter in the code that you received on your phone. Type in the code you received and press ENTER to finalize the registration.

You should now have access to the Telegram command line interface.

Utilizing the Telegram CLI on the Raspberry Pi

1. Since we have now successfully registered ourselves with the Telegram CLI was can now actually start to receive and send messages. You will notice that if anyone messages you, the message will pop up in the command line interface.

You can message people, or yourself by utilizing the following command within the Telegram CLI.

Replace <firstname> with the first name of the contact you want to message and <lastname> with the last name of the person you want to contact. Finally, you can replace <msg> with the message you want to send this person.

You can also use this command to message yourself by using the first name and last name that you specified with registering.

msg <firstname>_<lastname> <msg>

As an example of what a filled-out version of the command, we have an example below, using PiMy as the first name and LifeUp as the last name.

msg PiMy_LifeUp This is a test message from pimylifeup.com

2. You can also use the Telegram CLI to send photos as well, and this command is very similar to the sending a text-based message command. But instead of <msg> you point to the location of a photo on the Raspberry Pi that you want to send. You can extend this to send pictures from the Pi camera or a webcam.

msg_photo <firstname>_<lastname> <imagelocation>

As an example of what a filled-out version of the msg_photo command, we have an example below, using PiMy as the first name and LifeUp as the last name.

msg_photo PiMy_LifeUp /home/pi/photo.jpg

Automating the Telegram CLI on the Raspberry Pi

1. To simplify sending messages through the Raspberry Pi Telegram CLI we are going to write two scripts, one for handling sending text messages and one for handling photo images. These will remove the need to launch up the command line interface of Telegram every time you want to utilize it.

Let’s run the following command to begin writing our first Telegram script.

sudo nano /home/pi/tg.sh

2. Within this file, you will want to write the following lines of script. This script will read in two arguments as variables and then pass them through to the Telegram CLI to send the message.

#!/bin/bash
tgpath=/home/pi/tg
cd ${tgpath}
(sleep 3; echo "msg $1 $2"; echo "safe_quit") | ${tgpath}/bin/telegram-cli -k tg-server.pub -W

You can now save the file by pressing CTRL + X then Y and then hitting ENTER.

3. With our new script now written, we also need to give it execution permissions so it can start up Telegram.

Run the following command on your Raspberry Pi to change its permissions.

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

Learn how to understand and utilize the chmod command in our tutorial.

4. We can test our new script by running the following command, of course replacing <firstname> and <lastname> with the relevant data.

/home/pi/tg.sh <firstname>_<lastname> "Your test message from pimylifeup"

5. Once you have confirmed that the script successfully works, we can do the same for images. Run the following command to begin writing our second script.

sudo nano /home/pi/tg_photo.sh

6. Now in this file, we will want to write the following lines of script, this is very similar to our previous script but instead sends the send_photo command to the Telegram CLI.

#!/bin/bash
tgpath=/home/pi/tg
cd ${tgpath}
(sleep 3; echo "send_photo $1 $2"; echo "safe_quit") | ${tgpath}/bin/telegram-cli -k tg-server.pub -W

You can now save the file by pressing CTRL + X then Y and then hitting ENTER.

7. Like the tg.sh script we will also need to make this new tg_photo.sh script executable as well.

Run the following command to give our new tg_photo.sh script the correct permissions.

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

8. Now we can test this final script by running the following command, of course replacing <firstname> and <lastname> with the relevant data. We also need to make sure we point to a real image.

/home/pi/tg_photo.sh <firstname>_<lastname> /home/pi/photo.jpg

If you would like to know what sort of commands you can utilize with the Telegram CLI, you can go to the official documentation that lists all the possible commands. You can find that by going to the Telegram CLI GitHub page.

Hopefully, by now you have successfully set up the Telegram CLI on your Raspberry Pi and can successfully send text messages and images over Telegram.

Also be sure to drop a comment below if this Raspberry Pi Telegram CLI tutorial helped you out or whether you run into any issues.

9 Comments

  1. Avatar for Rodolfo
    Rodolfo on

    Worked straight away, thanks a lot!

  2. Avatar for Pietman Heyns
    Pietman Heyns on

    Good day,
    Thanks for a great tutorial: I am having difficulty after running ‘make’: I am using a Raspberry PI 3 and have followed your instructions to the letter, the error I get is:

    tgl/tl-parser/tl-parser.c: In function ‘tl_parse_args134’:
    tgl/tl-parser/tl-parser.c:1907:26: error: ‘sprintf’ may write a terminating nul past the end of the destination [-Werror=format-overflow=]

    Could you please help me to get it working?
    Kind regards,
    Pietman

    1. Avatar for Pietman Heyns
      Pietman Heyns on

      Hi everyone, to anyone who aslo hits this issue: there is a file in the ‘tg’ folder called makefile, in there, delete the text “-Werror”, that will allow warnings during compilation instead of treating a warning as an error: you will need to install two additional packages:
      sudo apt-get install libgcrypt20-dev
      sudo apt-get install libssl-dev

      Regards,
      Pietman

  3. Avatar for Frank
    Frank on

    Hey, thanks for the project. I am going to be doing this with some high-school students next week. Question for you…

    Basically, how do I “log out” of the Telegram CLI? For example, when I am in the Telegram CLI, I hit “clear” and it takes me back to the regular “pi@raspberrypi:~/tg $” prompt. Makes sense. But when I rerun the “bin/telegram-cli -k tg-server.pub -W” command, I am still logged in, meaning, it doesn’t ask me to enter a phone number.

    I can forecast some of the students will be concerned that this is a security concern. “The next student will be logged in to my account!”

    After doing some extensive research, my current work-around is to run “rm -rf ~/.telegram-cli”, then “rm -rf ~/tg”, and rerun “git clone –recursive https://github.com/LucentW/tg.git” and go from there. It requires me to recompile via the “./configure” and “make” commands (step 7 = ~10-12 minutes) and it’s annoying that I have to do this every time I want a new student to try the project.

    Any way I can “log out” of the CLI?

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Frank,

      I am glad to hear that the project has been useful for you and I do hope that it goes well for your students.

      From memory the Telegram CLI program should store its config files within the current users home directory in a special folder.

      Can you try running something like rm -r -f ~/.telegram-cli, and let us know whether that clears the data or not?

      Recompiling the project from scratch every time would quickly become incredibly tedious.

      Will look into it further if that doesn’t work.

      Cheers,
      Emmet

  4. Avatar for Eric
    Eric on

    There are some omissions at the end that you need to do before running part 7:

    1. You need to make tg_photo.sh executable as in the first example
    2. You need to run:
    /home/pi/tg_photo.sh _ /home/pi/photo.jpg
    not
    /home/pi/tg.sh _ /home/pi/photo.jpg

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Eric,

      Thank you for pointing out those two mistakes.

      I have now corrected the tutorial.

      Thank you for your contribution!

      Cheers,
      Emmet

  5. Avatar for Romie Searle
    Romie Searle on

    This is what I get after I DO THIS

    Find both
    assert(0);
    Replace with
    #assert(0);
    You can now save the file by pressing Ctrl + X then Y and then hitting Enter.
    [SNIP]
    tgl/mtproto-utils.c:120:13: error: predicate must be an identifier
    #assert (0);
    ^

    1. Avatar for Gus
      Gus on
      Editor

      Hey,

      There was a slight mistake in the web version of this tutorial, # should of been //
      Changing the #’s to // should correct the issue, the web version has now been corrected.

      Cheers

Leave a Reply

Your email address will not be published. Required fields are marked *