In previous blog posts we have already shown the sending of MQTT packages and Gmails with the ESP8266. Sending and receiving MQTT messages is usually only interesting for smart home users and Gmail is only limited to push notification, so I want to show the sending of telegram messages with the ESP in the following two posts.
Today we are doing the preliminary work. We register Telegram, create a bot, install CURL for Windows, and work with the bot over command line.
In the next post I show how the microcontroller can be implemented.
- ESP8266 board
- Smartphone (with mobile phone number for registration)
What is Telegram? Telegram is an instant messenger similar to WhatzApp, but with some significant differences. Unlike WhatzApp, Telegram is open and a (documented) API is available, and so-called "bots" are also supported. What are bots?A (Ro)bot is an automatic service that can send and receive messages without the need for a physical device or to do so by the user.
Register Telegram account:
A telephone number is required to create a telegram account. You can register with a PC or directly a mobile phone. After downloading the app or application, a check of the number is made by entering a security code (comes via SMS)
If you have chosen the desktop program, the following window will open after verification:
There are no contacts left in the address book, which is why only the empty window is displayed.
The Bot's creation is interactive with the "Botfather". To connect to Botfather, click on the Search bar in Telegram and enter "Botfather":
Start a conversation with Botfather via the Start button:
After that we already see the commands that allow us to create/configure/manage our bot. In principle, the chat can be used like a command line. "/newbot" instructs the botfather to create a new bot. Next we are asked about the name of our bot:
After that for the user name, for testing purposes I have chosen "azdemobot" each.
If the bot was created, we will receive a token for the API. In short, we have now summarized the Name of the bot, den User Names and the token.
The token plays a central role and should not be shared with other users as the holder can fully control the bot.
Looking for our bot we find the service we have just created and can start a conversation by clicking on the button "STARTEN":
In our example, the bot serves as a web interface. To check if everything worked out we can use the command line tool "curl". Linux users are well known for the tool and can skip the installation.
To install curl It is necessary to store the path in the console so that when you enter the curl command, Windows will automatically forward it to curl.exe.
Depending on the operating system you need the 64-bit or 32-bit variant.
After downloading and unpacking we move the folder to: "C:\Users\Username\curl"
This path must now be added to the system environment variables, you will find it in the Start menu:
Then select Environment variables...
Mark the "Path" line and click "Edit...", a new window opens:
The New button allows the path THE EXE DATEI in the bin directory! added by curl:
Damit haben wir curl installiert und können es über die Kommandozeile nutzen:
Now we can test if the bot we configured works.
To work with the API we need the following URL:
Here we still have to get our data or tokens, as well as the Method (i.e. instruction) in our case we want to check if the bot has new news for us. So we ask "getUpdates" as a method:
And receive the following edition:
The selected area specifies that no new information is available.
Let's send a telegram message to the bot:
And start the query again:
Wie wir sehen hat sich der "Content" geändert. Curl also offers the possibility to save this directly into a file or to display it in different variants. If we would save the contents into a file, the contents would be as follows:
So we see the message we send, as well as additional information such as Chat and update ID.
Conversely, it works exactly like that.
Let's enter the following command:
curl -k https://api.telegram.org/bot694546623:AAGIPm0_89TvPFDAiI0LVFS0Egg62845nNo/sendMessage -d chat_id=724358638 -d text="Das war der erste Teil"
In the Telegram window, the message sent by us via command line appears:
Of course, you must use your token here, as well as the chat ID displayed at the query.
This enables us to communicate with CURL via the API, in the second part on Monday, I will show how this can be implemented comfortably with an ESP.
Bis morgen :)