Particle Telegram Bot

About the project

Team up your Particle devices with a Telegram Bot

Project info

Difficulty: Easy

Platforms: Particle

Estimated time: 2 hours

License: MIT license (MIT)

Items used in this project

Hardware components

Particle Xenon Particle Xenon x 1
Particle Argon Particle Argon x 1
Particle Boron Particle Boron x 1
Particle Electron Particle Electron x 1
Particle Photon Particle Photon x 1

Software apps and online services

Telegram Telegram

Story

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:

1 / 3

Step 2: save the HTTP API Token

BotFather will give you an HTTPAPI 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<HTTP API token>/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 <Telegram User ID> and <HTTP API token> 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 <HTTP API token> on the URL and <Telegram User ID> 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!

Need help?

If you require professional help with your projects, don't hesitate to write me at gusgonnet@gmail.com. Thank you!

You can check all my projects in my electroMaker profile.

Code

Particle and Telegram repo

git repo, please clone it

Download

Credits

Photo of gusgonnet

gusgonnet

If you need professional help with a project, contact me at gusgonnet@gmail.com

   

Leave your feedback...