Particle Telegram Bot

This write-up will show how a Particle device can send Telegram messages.


Create a Telegram bot

Step 1: create your bot

Follow the instructions here.

You can name your bot Particle and the username MyParticleBot.

In my case, I entered: /start, /newbot, Particle, and MyParticleBot:

Step 2: save the HTTP API Token

BotFather will give you an HTTP API token with a pattern like 123456789:AABBCCDD_abcdefghijklmnopqrstuvwxyz. Save it since you need it to send messages.

Step 3: get your Telegram user ID

I- Click on the t.me/MyParticleBot link displayed by FatherBot after creating your bot. This will open the bot chat.

II- Send any message to your new bot (from that bot chat).

III- Open https://api.telegram.org/bot/getUpdates. You got the HTTP API token in step 2.

Example:

https://api.telegram.org/bot123456789:AABBCCDD_abcdefghijklmnopqrstuvwxyz/getUpdates

IV- Look for a field called id, for example, 1234562301, and write it down. This is your Telegram user ID.

Step 4: test it from the command line

curl -X POST \
-H 'Content-Type: application/json' \
-d '{"chat_id": "<Telegram User ID>", "text": "test 123" }' \
https://api.telegram.org/bot<HTTP API token>/sendMessage

Replace and with yours. Example:

curl -X POST \
-H 'Content-Type: application/json' \
-d '{"chat_id": "1234562301", "text": "test 123" }' \
https://api.telegram.org/bot123456789:AABBCCDD_abcdefghijklmnopqrstuvwxyz/sendMessage

You should receive the message in your Telegram Bot:


Create a Particle webhook

Open the Integrations section of your Particle console and create a new Webhook (here’s the link).

Name it telegramWebhook and enter the info so it looks like the picture below.

Replace on the URL and on the custom body with yours (see red boxes):


Code the firmware

The firmware has to trigger the webhook with the message you want to send.

On a Particle device, this can be done as follows:

Particle.publish("telegramWebhook", "the button was clicked!", PRIVATE | WITH_ACK);

You can add that publish command above to any of your projects.

I am providing an example firmware here to test everything works fine. It will send a message every time you tap on the MODE button of your Particle device. You will receive this message on your Bot:

That’s all!

If you have questions, reply below.

You can check all my projects here.

I also published this on Hackster.

4 Likes

This is awesome! :slight_smile:

1 Like

Hi

I followed your tutorial but I get an error

error status 400 from api.telegram.org
{“ok”:false,“error_code”:400,“description”:“Bad Request: message text is empty”}

Has anyone tried the Universal Arduino Telegram Bot Library, written by Brian Lough? It seems pretty useful. Here is a link to his YouTube video –

Hi, I guess that the response to the curl command, correct? Can you post it?

From curl command

C:\Users*>curl -X POST -H ‘Content-Type: application/json’ -d '{“chat_id”: "1*", “text”: “test 123” }’ \https://api.telegram.org/bot1559811426:AAEw0xeviDZ_GcAZ3J3r-tvre_XZfWs3DOY/sendMessage
curl: (6) Could not resolve host: -H
curl: (6) Could not resolve host: 'Content-Type
curl: (52) Empty reply from server
curl: (6) Could not resolve host: -d
curl: (3) [globbing] unmatched brace in column 2
curl: (6) Could not resolve host: 12******,
curl: (52) Empty reply from server
curl: (6) Could not resolve host: test 123
curl: (3) [globbing] unmatched close brace/bracket in column 1
curl: (1) Protocol “\https” not supported or disabled in libcurl

Hi, this gives you a hint:
Protocol “\https” not supported or disabled in libcurl

If the curl command is on the same line, you do no need the slash.
You use the slash to split commands in more than one line

1 Like

Hi, and welcome to the community!

I think one issue with that library could be the fact that it uses WiFiClientSecure and I’m not sure how that library would fit/work in a Particle device due to the nature of SSL or HTTPS connections in general, as they are RAM heavy. But I only read about those issues around the community, I never faced them.

Thanks for the link to the video!
Gustavo.