Send messages to Telegram users from your Raspberry Pi

Telegram

If you are a smartphone user, you will surely know that one of the most similar and famous applications of WhatsApp is Telegram, a very versatile messaging client which presents several peculiarities such as the possibility of being able to use the same telephone number from different devices simultaneously. Thanks precisely to this feature, today I want to present you a small tutorial with which you can send text messages and even multimedia files to your contacts from a Raspberry Pi.

As much or more interesting is the fact that we can configure our Raspberry Pi so that, before a certain command received by Telegram, our card can exercise some additional performanceIn other words, let's imagine that we send the word «photo»And this gives us an image of any room in the house,«light»To automatically turn on any light or«open»To automatically open the garage door.

Surely this additional functionality has caught your attention. If so, we get down to work, but not before telling you that we will need a Raspberry Pi B or Raspberry Pi B + to carry out the project as well as an 8 GB Class 10 microSD card with the latest version of Raspbian pre-installed.

Once we have all of the above, we start and from a Port we start with the update and basic configuration. Surely there will be many users who do not need this but we better do everything step by step and at a good pace so as not to skip anything. We start by running and updating the packages with:

sudo apt-get update
sudo apt-get upgrade

We continue with the installation and updating of several essential libraries where the system will find all the libraries we need

sudo apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 li-blua5.2-dev libevent-dev make

Shift of the repository GitHub

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

Telegram

Once we have everything installed, it is time to configure lua, a powerful and fast scripting language. The syntax is very simple, in our Terminal we execute:

sudo nano /home/pi/tg/action.lua

and we add the following content:

function on_msg_receive (msg)
      if msg.out then
          return
      end
      if (msg.text=='ping') then
         send_msg (msg.from.print_name, 'pong', ok_cb, false)
      end
  end
   
  function on_our_id (id)
  end
   
  function on_secret_chat_created (peer)
  end
   
  function on_user_update (user)
  end
   
  function on_chat_update (user)
  end
   
  function on_get_difference_end ()
  end
   
  function on_binlog_replay_end ()
  end

With the above, we would have practically everything configured so that, when we send the text «ping»This will return«pong«.

We move to the tg directory

cd /home/pi/tg

We execute the following order

bin/telegram-cli -k tg-server.pub -W -s action.lua

Now is the time to start testing and submit our «ping»To Telegram, immediately afterwards and as you can see in the image the answer is our expected«pong«. We must take into account if we use capital letters or not since the system is sensitive to their use.

If what we want is that, instead of "pong" our Raspberry Pi returns an image, in the function where we send the response we would only have to tell the system to take a photo using a previously installed camera and send it to us.

Link: instructables


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

3 comments, leave yours

Leave a Comment

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

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Bill said

    Seeing the possibilities of this it occurred to me that perhaps it would be nice to be able to safely send any command to my raspberry (or to any Linux server) from telegram and get the output. Even create command aliases to avoid having to type too much, manage users who can do the same on the same machine so that anyone can not do what they want ... etc

    I have started to do it and today I have published 'Obedience'.
    If anyone wants to mess around and try it, go ahead

    https://github.com/GuillermoPena/obedience

  2.   Juan Luis Arboledas said

    Hi Guillermo,

    I have not had much time to review everything but I have to tell you that it looks very good. If I have time this weekend I will try everything to see how it works.

    Thank you very much for your work!!!

  3.   Jonathan said

    Hello, excellent post, I loved it! I just want to know if there is a way to run the .lua script automatically at startup, greetings!