Telegram Bot Library

Host a Telegram Bot on your Arduino and chat with your brand new IoT device!

  • 21,859 views
  • 14 comments
  • 50 respects

Components and supplies

Apps and online services

About this project

Learn how to use the Telegram Bot library, host a Telegram Bot on your Arduino/Genuino Board, and use the messaging app to interact with your device.

It currently works on Arduino MKR1000, and Arduino Zero with the WiFi shield 101. You can view the last released Telegram API documentation here.

Installing

Step 1 (TelegramBot Library)

Download TelegramBot Library (Download).

To install library, open Arduino, goto menu: Sketch / Include Library / Add .ZIP Library... and select your downloaded .ZIP file.

You can include a new .ZIP library into the Arduino IDE by selecting the menu: Sketch / include Library / Add .Zip library.

Step 2 (ArduinoJson & WiFi101 Library)

Install ArduinoJson & WiFi101 Library from Library Manager.

Getting started

Step 1 (Create a new TelegramBot )

Be sure you have installed Telegram on your phone or your laptop, then, in the search bar, look for @botfather.

Talk to him and use the /newbot command to create a new bot. The BotFather will ask you for a name and username, then generate an authorization token for your new bot.

The Name of your bot will be displayed in contact details and elsewhere.

The Username is a short name, to be used in mentions and telegram.me links. Your bot's username must end in ‘bot’, e.g. ‘tetris_bot’ or ‘TetrisBot’.

Hello World

Now that we have our new bot we can set it to do what we want.

In this example we will make it turn on and off a LED by texting him a simple 'On or Off' message.

#include <WiFi101.h> 
#include <SPI.h>  
#include <TelegramBot.h>  
 
// Initialize Wifi connection to the router  
char ssid[] = "xxxx";             // your network SSID (name)  
char pass[] = "yyyy";            // your network key 
// Initialize Telegram BOT  
const char* BotToken = "xxxx";    // your Bot Teken  
WiFiSSLClient client;  
TelegramBot bot(BotToken,client);  
const int ledPin = 6;  // the number of the LED pin  
void setup() 
{  
 Serial.begin(115200);  
 while (!Serial) {}  //Start running when the serial is open 
 delay(3000);  
 // attempt to connect to Wifi network:  
 Serial.print("Connecting Wifi: ");  
 Serial.println(ssid);  
 while (WiFi.begin(ssid, pass) != WL_CONNECTED) 
       {  
   Serial.print(".");  
   delay(500);  
 }  
 Serial.println("");  
 Serial.println("WiFi connected");  
 bot.begin();  
 pinMode(ledPin, OUTPUT);  
}  
void loop() 
{  
 message m = bot.getUpdates(); // Read new messages  
 if (m.text.equals("On")) 
       {  
   digitalWrite(ledPin, HIGH);  
   Serial.println("message received");  
   bot.sendMessage(m.chat_id, "The Led is now ON");  
 }  
 else if (m.text.equals("Off")) 
       {  
   digitalWrite(ledPin, LOW);  
   Serial.println("message received");  
   bot.sendMessage(m.chat_id, "The Led is now OFF");  
 }  
}  

Let's see in action

Notes about the groups

When the bot is included in a group it will be, by default, in privacy mode. Which means that it will receive only messages starting with "/", the so called commands. In order to disable the privacy mode you have to chat with BotFather and go through this flow:

/mybots
@yourBot
Bot setting
Group privacy
Turn off

You can also create your own list of commands using /setcommands while chatting with the BotFather. This list will appear only in the mobile view, pressing the "/" icon.

Code

Arduino Sketch

Comments

Similar projects you might like

Handheld Infinity Kaleidoscope Group Kit

Project tutorial by Lucas Ainsworth

  • 1,528 views
  • 2 comments
  • 23 respects

Arduino 101 BLE RGB Lamp

Project tutorial by Gustavo Reynaga

  • 361 views
  • 2 comments
  • 6 respects

Arduino Keyless Door Lock System with Keypad and LCD

Project tutorial by DIY Hacking

  • 486 views
  • 3 comments
  • 11 respects

Control LED/Relay Using Arduino 101

Project tutorial by Shivansh Singh

  • 240 views
  • 1 comment
  • 4 respects

I keep my cucumber breeding moist

Project tutorial by Ingo Lohs

  • 583 views
  • 0 comments
  • 9 respects

Easiest Line Follower Robot

Project showcase by Zubayer Al Billal Khan

  • 371 views
  • 3 comments
  • 5 respects
Add projectSign up / Login