Raspberry Pi Telegram CLI

In this Raspberry Pi Telegram CLI tutorial, we will be showing you how to setup the popular messaging service Telegram on your Raspberry Pi. We will be doing this by making use of the Telegram CLI (Command Line Interface) written by a user on GitHub.

Basically, this will allow you to send and receive messages over the Telegram messaging service on your Raspberry Pi, even going as far as even 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:

Raspberry Pi 2 or 3

Micro SD Card

Ethernet Network Connection

Mobile Phone

Optional:

Raspberry Pi Case

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-get update
sudo apt-get 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-get install -y libreadline-dev libconfig-dev libssl-dev 
sudo apt-get install -y lua5.2 liblua5.2-dev
sudo apt-get install -y libevent-dev libjansson-dev libpython-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 also clone the remote repository’s 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/LucentW/tg.git

4. To get the Telegram CLI to compile on our Raspberry Pi we will first have to make a modification to 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) like 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 3 commands on our Raspberry Pi, the first changes us into 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 was 04XXXXXXXX it would become +614XXXXXXXX when we type it in. A authentication code will be sent to this phone, 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 onwards. Immediately afterwards 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. Replacing <fistname> 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<msg>

2. You can also use the Telegram CLI to send photos as well, 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 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 basically 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 will basically 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}
(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 need to also give it execution permissions so it can actually start up Telegram, run the following command on your Raspberry Pi to change its permissions.

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

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}
(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 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.sh <firstname>_<lastname> /home/pi/photo.jpg

If you would like to know what sort of commands you can utilise 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 are able to successfully send text messages and images over Telegram.

Don't forget to check out our numerous other projects on the website and in our Raspberry Pi project book. 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.

The Raspberry Pi Crash Course

The Raspberry Pi Crash Course

Enter your email address below to get the

Raspberry Pi crash course delivered straight to your inbox

Please check your inbox for a confirmation email!