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