Skip to content

Commit 5f7a3a6

Browse files
committed
Adding reply keyboard support and examples
1 parent bcaca72 commit 5f7a3a6

File tree

15 files changed

+370
-60
lines changed

15 files changed

+370
-60
lines changed

README.md

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
#Arduino IDE for ESP8266 - Telegram BOT Library
1+
#Universal Telegram Bot Library
22

3-
An Arduino IDE library around the Telegram Bot API to be used with ESP8266.
3+
An Arduino IDE library for using Telegram Bot API. It's designed to be used with multiple Arduino architectures.
44

5-
Application written by [Giancarlo Bacchio](https://github.com/Gianbacchio)
5+
Forked from [ESP8266-TelegramBot](https://github.com/Gianbacchio/ESP8266-TelegramBot) & inspired by [TelegramBot-Library](https://github.com/CasaJasmina/TelegramBot-Library)
66

77

88
## Introduction
99

10-
This library provides an interface for [Telegram Bot API](https://core.telegram.org/bots/api). It is written using Arduino IDE and works using ESP8266.
10+
This library provides an interface for [Telegram Bot API](https://core.telegram.org/bots/api).
1111

12-
It is possible to define your personal Bot, make it able to read and write messges, recieve orders and report data collected from the field.
13-
14-
15-
## Getting the code
16-
17-
You can install Arduino IDE-telegram-bot for your ESP8266 downloading the code that is hosted here.
18-
You can check out the latest development version and being informed about updateds, selecting "Watch" box in the GitHub repository.
12+
It is possible to define your personal Bot, make it able to read and write messages, receive orders and report data collected from the field.
1913

2014

2115
## Installing
2216

23-
The downloaded code can be included as a new library into the IDE selecting the menue:
17+
The downloaded code can be included as a new library into the IDE selecting the menu:
2418

25-
Sketch / include Library / Add .Zip library
19+
Sketch / include Library / Add .Zip library
20+
21+
You also have to install the ArduinoJson library written by [Benoît Blanchon](https://github.com/bblanchon).
22+
Available [here](https://github.com/bblanchon/ArduinoJson).
2623

2724

2825
## Getting started
@@ -40,14 +37,14 @@ Here are listed some examples to help you to build your own Bot:
4037

4138
- EchoBot : replies echoing your messages.
4239

43-
- FlashLedBot : Reacts to your command switching ON/OFF a GPIO.
44-
40+
- FlashLedBot : Reacts to your command switching ON/OFF a GPIO.
4541

42+
- EchoBotWithPost : same as EchoBot but using post to commincate with Telegram.
4643

47-
## License
48-
49-
You may copy, distribute and modify the software provided that modifications are described and licensed for free under [LGPL-3](http://www.gnu.org/licenses/lgpl-3.0.html). Derivatives works (including modifications or anything statically linked to the library) can only be redistributed under [LGPL-3](http://www.gnu.org/licenses/lgpl-3.0.html), but applications that use the library don't have to be.
44+
- CustomKeyboard : Same as FlashLedBot but also uses a replyKeyboard
5045

5146

5247

48+
## License
5349

50+
You may copy, distribute and modify the software provided that modifications are described and licensed for free under [LGPL-3](http://www.gnu.org/licenses/lgpl-3.0.html). Derivatives works (including modifications or anything statically linked to the library) can only be redistributed under [LGPL-3](http://www.gnu.org/licenses/lgpl-3.0.html), but applications that use the library don't have to be.
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*******************************************************************
2+
* An example of how to use a custom reply keyboard using *
3+
* ESP8266TelegramBot. *
4+
* *
5+
* written by Brian Lough *
6+
*******************************************************************/
7+
8+
#include <ESP8266WiFi.h>
9+
#include <WiFiClientSecure.h>
10+
#include <ESP8266TelegramBOT.h>
11+
12+
13+
// Initialize Wifi connection to the router
14+
char ssid[] = "xxxxxxxxxxxxxxxxxxxxxx"; // your network SSID (name)
15+
char pass[] = "yyyyyyyy"; // your network key
16+
17+
18+
19+
const int ledPin = 13;
20+
21+
// Initialize Telegram BOT
22+
#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get off Botfather)
23+
24+
ESP8266TelegramBOT bot(BOTtoken);
25+
26+
int Bot_mtbs = 1000; //mean time between scan messages
27+
long Bot_lasttime; //last time messages' scan has been done
28+
int ledStatus = 0;
29+
30+
void handleNewMessages(int numNewMessages) {
31+
Serial.println("handleNewMessages");
32+
Serial.println(String(numNewMessages));
33+
for(int i=0; i<numNewMessages; i++) {
34+
String chat_id = String(bot.messages[i].chat_id);
35+
String text = bot.messages[i].text;
36+
if (text == "\/ledon") {
37+
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
38+
ledStatus = 1;
39+
bot.sendMessage(chat_id, "Led is ON", "");
40+
}
41+
if (text == "\/ledoff") {
42+
ledStatus = 0;
43+
digitalWrite(ledPin, LOW); // turn the LED off (LOW is the voltage level)
44+
bot.sendMessage(chat_id, "Led is OFF", "");
45+
}
46+
if (text == "\/status") {
47+
if(ledStatus){
48+
bot.sendMessage(chat_id, "Led is ON", "");
49+
} else {
50+
bot.sendMessage(chat_id, "Led is OFF", "");
51+
}
52+
}
53+
if (text == "\/options") {
54+
StaticJsonBuffer<500> jsonBuffer;
55+
JsonObject& payload = jsonBuffer.createObject();
56+
payload["chat_id"] = chat_id;
57+
payload["text"] = "Choose from one of the following options";
58+
JsonObject& replyMarkup = payload.createNestedObject("reply_markup");
59+
60+
// Reply keyboard is an array of arrays.
61+
// Outer array represents rows
62+
// Inner arrays represents columns
63+
// This example "ledon" and "ledoff" are two buttons on the top row
64+
// and "status is a single button on the next row"
65+
StaticJsonBuffer<200> keyboardBuffer;
66+
char keyboardJson[] = "[[\"/ledon\", \"/ledoff\"],[\"/status\"]]";
67+
replyMarkup["keyboard"] = keyboardBuffer.parseArray(keyboardJson);
68+
replyMarkup["resize_keyboard"] = true;
69+
70+
bot.sendPostMessage(payload);
71+
}
72+
73+
if (text == "\/start") {
74+
String wellcome = "The custom keyboard example for ESP8266TelegramBot";
75+
String wellcome1 = "/ledon : to switch the Led ON";
76+
String wellcome2 = "/ledoff : to switch the Led OFF";
77+
String wellcome3 = "/status : Returns current status of LED";
78+
String wellcome4 = "/options : returns the custom keyboard";
79+
bot.sendMessage(chat_id, wellcome, "");
80+
bot.sendMessage(chat_id, wellcome1, "");
81+
bot.sendMessage(chat_id, wellcome2, "");
82+
bot.sendMessage(chat_id, wellcome3, "");
83+
bot.sendMessage(chat_id, wellcome4, "");
84+
}
85+
}
86+
}
87+
88+
89+
void setup() {
90+
Serial.begin(115200);
91+
delay(3000);
92+
93+
// attempt to connect to Wifi network:
94+
Serial.print("Connecting Wifi: ");
95+
Serial.println(ssid);
96+
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
97+
Serial.print(".");
98+
delay(500);
99+
}
100+
Serial.println("");
101+
Serial.println("WiFi connected");
102+
Serial.println("IP address: ");
103+
IPAddress ip = WiFi.localIP();
104+
Serial.println(ip);
105+
bot.begin(); // launch Bot functionalities
106+
pinMode(ledPin, OUTPUT); // initialize digital ledPin as an output.
107+
delay(10);
108+
digitalWrite(ledPin, HIGH); //initilase pin as off
109+
110+
}
111+
112+
113+
114+
void loop() {
115+
if (millis() > Bot_lasttime + Bot_mtbs) {
116+
117+
int numNewMessages = bot.getUpdates(bot.last_message_recived + 1);
118+
if(numNewMessages) {
119+
Serial.println("got response");
120+
handleNewMessages(numNewMessages);
121+
}
122+
Bot_lasttime = millis();
123+
}
124+
}
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
#ESP8266 - Echo Bot
1+
#ESP8266 - Custom Keyboard bot
22

3-
Echo Bot receives messages and reply like a parrot.
3+
This is an example of how to use custom reply keyboards using the ESP8266TelegramBOT
44

5-
This Example shows how to use a Bot from Telegram. You will learn how to write a message to your Bot from telegram and receive a reply with an ESP8266 board.
5+
The application will turn on and off an led based on commands received via telegram
66

7-
Application written by [Giancarlo Bacchio]([email protected])
7+
NOTE: You will need to enter your SSID, password and bot Token for the example to work.
8+
9+
Application written by [Brian Lough](https://github.com/witnessmenow)
810

911

1012

1113
## License
1214

1315
You may copy, distribute and modify the software provided that modifications are described and licensed for free under [LGPL-3](http://www.gnu.org/licenses/lgpl-3.0.html). Derivatives works (including modifications or anything statically linked to the library) can only be redistributed under [LGPL-3](http://www.gnu.org/licenses/lgpl-3.0.html), but applications that use the library don't have to be.
14-
15-
16-
17-

examples/EchoBot/EchoBot.ino renamed to examples/ESP8266/EchoBot/EchoBot.ino

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
/*******************************************************************
2-
* this is a basic example how to program a Telegram Bot *
3-
* using TelegramBOT library on ESP8266 *
2+
* An example of bot that echos back any messages received *
3+
* using ESP8266TelegramBot. *
44
* *
5-
* Open a conversation with the bot, it will echo your messages *
6-
* https://web.telegram.org/#/im?p=@EchoBot_bot *
7-
* *
8-
* written by Giacarlo Bacchio *
5+
* written by Giacarlo Bacchio (Gianbacchio on Github) *
6+
* adapted by Brian Lough *
97
*******************************************************************/
108

119

@@ -21,7 +19,7 @@ char password[] = "yyyyyyyyy"; // your network key
2119

2220

2321
// Initialize Telegram BOT
24-
#define BOTtoken "77330665:AAEIHv4RJxPnygoKD8nZqLnlpmd4hq7iR7s" //token of TestBOT
22+
#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get off Botfather)
2523
ESP8266TelegramBOT bot(BOTtoken);
2624

2725
int Bot_mtbs = 1000; //mean time between scan messages

examples/ESP8266/EchoBot/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ESP8266 - Echo Bot
2+
3+
This is a basic example of how to use ESP8266TelegramBOT.
4+
5+
The application will echo an message it received back to the user.
6+
7+
NOTE: You will need to enter your SSID, password and bot Token for the example to work.
8+
9+
Application originally written by [Giancarlo Bacchio]([email protected]) for [ESP8266-TelegramBot library](https://github.com/Gianbacchio/ESP8266-TelegramBot)
10+
11+
Adapted by [Brian Lough](https://github.com/witnessmenow)
12+
13+
## License
14+
15+
You may copy, distribute and modify the software provided that modifications are described and licensed for free under [LGPL-3](http://www.gnu.org/licenses/lgpl-3.0.html). Derivatives works (including modifications or anything statically linked to the library) can only be redistributed under [LGPL-3](http://www.gnu.org/licenses/lgpl-3.0.html), but applications that use the library don't have to be.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*******************************************************************
2+
* An example of bot that echos back any messages received *
3+
* using ESP8266TelegramBot. *
4+
* *
5+
* written by Brian Lough *
6+
*******************************************************************/
7+
8+
9+
#include <ESP8266WiFi.h>
10+
#include <WiFiClientSecure.h>
11+
#include <ESP8266TelegramBOT.h>
12+
13+
14+
// Initialize Wifi connection to the router
15+
char ssid[] = "xxxxxxxxxxxxxxxxxxxxxxxx"; // your network SSID (name)
16+
char password[] = "yyyyyyyyy"; // your network key
17+
18+
19+
// Initialize Telegram BOT
20+
#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get off Botfather)
21+
ESP8266TelegramBOT bot(BOTtoken);
22+
23+
int Bot_mtbs = 1000; //mean time between scan messages
24+
long Bot_lasttime; //last time messages' scan has been done
25+
26+
27+
/********************************************
28+
* EchoMessages - function to Echo messages *
29+
********************************************/
30+
void Bot_EchoMessages(int numNewMessages) {
31+
for(int i=0; i<numNewMessages; i++) {
32+
StaticJsonBuffer<500> jsonBuffer;
33+
JsonObject& payload = jsonBuffer.createObject();
34+
payload["chat_id"] = bot.messages[i].chat_id;
35+
payload["text"] = bot.messages[i].text;
36+
bot.sendPostMessage(payload);
37+
}
38+
}
39+
40+
41+
void setup() {
42+
43+
Serial.begin(115200);
44+
delay(3000);
45+
46+
// attempt to connect to Wifi network:
47+
Serial.print("Connecting Wifi: ");
48+
Serial.println(ssid);
49+
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
50+
Serial.print(".");
51+
delay(500);
52+
}
53+
Serial.println("");
54+
Serial.println("WiFi connected");
55+
Serial.println("IP address: ");
56+
IPAddress ip = WiFi.localIP();
57+
Serial.println(ip);
58+
59+
bot.begin(); // launch Bot functionalities
60+
}
61+
62+
63+
64+
void loop() {
65+
66+
if (millis() > Bot_lasttime + Bot_mtbs) {
67+
int numNewMessages = bot.getUpdates(bot.last_message_recived + 1); // launch API GetUpdates up to xxx message
68+
if(numNewMessages) {
69+
Serial.println("got response");
70+
Bot_EchoMessages(numNewMessages); // reply to message with Echo
71+
}
72+
Bot_lasttime = millis();
73+
}
74+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ESP8266 - Echo Bot with post
2+
3+
This is a basic example of how to use sendPostMessage functionality of ESP8266TelegramBOT.
4+
5+
sendPostMessage allows the more advanced options of the Telegram Api to be used (see CustomKeyboard example for how to use keyboards).
6+
7+
The application will echo an message it received back to the user.
8+
9+
NOTE: You will need to enter your SSID, password and bot Token for the example to work.
10+
11+
Adapted by [Brian Lough](https://github.com/witnessmenow)
12+
13+
## License
14+
15+
You may copy, distribute and modify the software provided that modifications are described and licensed for free under [LGPL-3](http://www.gnu.org/licenses/lgpl-3.0.html). Derivatives works (including modifications or anything statically linked to the library) can only be redistributed under [LGPL-3](http://www.gnu.org/licenses/lgpl-3.0.html), but applications that use the library don't have to be.

examples/FlashledBot/FlashledBot.ino renamed to examples/ESP8266/FlashledBot/FlashledBot.ino

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ char pass[] = "yyyyyyyy"; // your network key
2323
const int ledPin = 13;
2424

2525
// Initialize Telegram BOT
26-
27-
#define BOTtoken "134745667:AAETzUWRQdb9xbMX_s-q_50U6ffgXcW3ldg" //token of FlashledBOT
26+
#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get off Botfather)
2827

2928
ESP8266TelegramBOT bot(BOTtoken);
3029

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ESP8266 - Flashled Bot
2+
3+
This is a basic example of how to receive commands using ESP8266TelegramBOT.
4+
5+
The application will turn on and off an led based on commands received via telegram
6+
7+
Application originally written by [Giancarlo Bacchio]([email protected]) for [ESP8266-TelegramBot library](https://github.com/Gianbacchio/ESP8266-TelegramBot)
8+
9+
Adapted by [Brian Lough](https://github.com/witnessmenow)
10+
11+
NOTE: You will need to enter your SSID, password and bot Token for the example to work.
12+
13+
## License
14+
15+
You may copy, distribute and modify the software provided that modifications are described and licensed for free under [LGPL-3](http://www.gnu.org/licenses/lgpl-3.0.html). Derivatives works (including modifications or anything statically linked to the library) can only be redistributed under [LGPL-3](http://www.gnu.org/licenses/lgpl-3.0.html), but applications that use the library don't have to be.

examples/FlashledBot/README.md

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)