diff --git a/.travis.yml b/.travis.yml
index 3d93c8c..4c8080d 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -9,6 +9,7 @@ cache:
- "~/.platformio"
env:
+ # ESP8266
- SCRIPT=platformioSingle EXAMPLE_NAME=EchoBot EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
- SCRIPT=platformioSingle EXAMPLE_NAME=ReplyKeyboardMarkup EXAMPLE_FOLDER=/CustomKeyboard/ BOARDTYPE=ESP8266 BOARD=d1_mini
- SCRIPT=platformioSingle EXAMPLE_NAME=InlineKeyboardMarkup EXAMPLE_FOLDER=/CustomKeyboard/ BOARDTYPE=ESP8266 BOARD=d1_mini
@@ -18,6 +19,19 @@ env:
- SCRIPT=platformioSingle EXAMPLE_NAME=PhotoFromSD EXAMPLE_FOLDER=/SendPhoto/ BOARDTYPE=ESP8266 BOARD=d1_mini
- SCRIPT=platformioSingle EXAMPLE_NAME=PhotoFromURL EXAMPLE_FOLDER=/SendPhoto/ BOARDTYPE=ESP8266 BOARD=d1_mini
- SCRIPT=platformioSingle EXAMPLE_NAME=PhotoFromFileID EXAMPLE_FOLDER=/SendPhoto/ BOARDTYPE=ESP8266 BOARD=d1_mini
+ - SCRIPT=platformioSingle EXAMPLE_NAME=Location EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
+ - SCRIPT=platformioSingle EXAMPLE_NAME=ChannelPost EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
+ - SCRIPT=platformioSingle EXAMPLE_NAME=ChatAction EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
+ - SCRIPT=platformioSingle EXAMPLE_NAME=LongPoll EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
+ # ESP32
+ - SCRIPT=platformioSingle EXAMPLE_NAME=EchoBot EXAMPLE_FOLDER=/ BOARDTYPE=ESP32 BOARD=esp32dev
+ - SCRIPT=platformioSingle EXAMPLE_NAME=ReplyKeyboardMarkup EXAMPLE_FOLDER=/CustomKeyboard/ BOARDTYPE=ESP32 BOARD=esp32dev
+ - SCRIPT=platformioSingle EXAMPLE_NAME=InlineKeyboardMarkup EXAMPLE_FOLDER=/CustomKeyboard/ BOARDTYPE=ESP32 BOARD=esp32dev
+ - SCRIPT=platformioSingle EXAMPLE_NAME=FlashLED EXAMPLE_FOLDER=/ BOARDTYPE=ESP32 BOARD=esp32dev
+ - SCRIPT=platformioSingle EXAMPLE_NAME=Location EXAMPLE_FOLDER=/ BOARDTYPE=ESP32 BOARD=esp32dev
+ - SCRIPT=platformioSingle EXAMPLE_NAME=ChannelPost EXAMPLE_FOLDER=/ BOARDTYPE=ESP32 BOARD=esp32dev
+ - SCRIPT=platformioSingle EXAMPLE_NAME=ChatAction EXAMPLE_FOLDER=/ BOARDTYPE=ESP32 BOARD=esp32dev
+ - SCRIPT=platformioSingle EXAMPLE_NAME=LongPoll EXAMPLE_FOLDER=/ BOARDTYPE=ESP32 BOARD=esp32dev
install:
diff --git a/README.md b/README.md
index 3774034..25d4914 100644
--- a/README.md
+++ b/README.md
@@ -53,9 +53,12 @@ Here is a list of features that this library covers. (Note: The examples link to
|*Receiving Messages*|Your bot can read messages that are sent to it. This is useful for sending commands to your arduino such as toggle and LED|`int getUpdates(long offset)`
Gets any pending messages from Telegram and stores them in **bot.messages** . Offset should be set to **bot.last_message_received** + 1. Returns the numbers new messages received.| [FlashLED](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/blob/master/examples/ESP8266/FlashLED/FlashLED.ino) or any other example|
|*Sending messages*|Your bot can send messages to any Telegram or group. This can be useful to get the arduino to notify you of an event e.g. Button pressed etc (Note: bots can only message you if you messaged them first)|`bool sendMessage(String chat_id, String text, String parse_mode = "")`
Sends the message to the chat_id. Returns if the message sent or not.| [EchoBot](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/blob/master/examples/ESP8266/EchoBot/EchoBot.ino#L51) or any other example|
|*Reply Keyboards*|Your bot can send [reply keyboards](https://camo.githubusercontent.com/2116a60fa614bf2348074a9d7148f7d0a7664d36/687474703a2f2f692e696d6775722e636f6d2f325268366c42672e6a70673f32) that can be used as a type of menu.|`bool sendMessageWithReplyKeyboard(String chat_id, String text, String parse_mode, String keyboard, bool resize = false, bool oneTime = false, bool selective = false)`
Send a keyboard to the specified chat_id. parse_mode can be left blank. Will return true if the message sends successfully.| [ReplyKeyboard](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/blob/master/examples/ESP8266/CustomKeyboard/ReplyKeyboardMarkup/ReplyKeyboardMarkup.ino)|
-|*Inline Keyboards*|Your bot can send [inline keyboards](https://camo.githubusercontent.com/55dde972426e5bc77120ea17a9c06bff37856eb6/68747470733a2f2f636f72652e74656c656772616d2e6f72672f66696c652f3831313134303939392f312f324a536f55566c574b61302f346661643265323734336463386564613034).
Note: Only URLS are supported currently|`bool sendMessageWithInlineKeyboard(String chat_id, String text, String parse_mode, String keyboard)`
Send a keyboard to the specified chat_id. parse_mode can be left blank. Will return true if the message sends successfully.| [InlineKeyboard](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/blob/master/examples/ESP8266/CustomKeyboard/InlineKeyboardMarkup/InlineKeyboardMarkup.ino)|
+|*Inline Keyboards*|Your bot can send [inline keyboards](https://camo.githubusercontent.com/55dde972426e5bc77120ea17a9c06bff37856eb6/68747470733a2f2f636f72652e74656c656772616d2e6f72672f66696c652f3831313134303939392f312f324a536f55566c574b61302f346661643265323734336463386564613034).
Note: URLS & callbacks are supported currently|`bool sendMessageWithInlineKeyboard(String chat_id, String text, String parse_mode, String keyboard)`
Send a keyboard to the specified chat_id. parse_mode can be left blank. Will return true if the message sends successfully.| [InlineKeyboard](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/blob/master/examples/ESP8266/CustomKeyboard/InlineKeyboardMarkup/InlineKeyboardMarkup.ino)|
|*Send Photos*|It is possible to send phtos from your bot. You can send images from the web or from the arduino directly (Only sending from an SD card has been tested, but it should be able to send from a camera module)|Check the examples for more info| [From URL](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/blob/master/examples/ESP8266/SendPhoto/PhotoFromURL/PhotoFromURL.ino)
[Binary from SD](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/blob/master/examples/ESP8266/SendPhoto/PhotoFromSD/PhotoFromSD.ino)
[From File Id](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/blob/master/examples/ESP8266/SendPhoto/PhotoFromFileID/PhotoFromFileID.ino)|
-|*Chat Actions*|Your bot can send chat actions, such as *typing* or *sending photo* to let the user know that the bot is doing something. |`bool sendChatAction(String chat_id, String chat_action)`
Send a the chat action to the specified chat_id. There is a set list of chat actions that Telegram support, see the example for details. Will return true if the chat actions sends successfully.| [InlineKeyboard](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/tree/master/examples/ESP8266/ChatAction)|
+|*Chat Actions*|Your bot can send chat actions, such as *typing* or *sending photo* to let the user know that the bot is doing something. |`bool sendChatAction(String chat_id, String chat_action)`
Send a the chat action to the specified chat_id. There is a set list of chat actions that Telegram support, see the example for details. Will return true if the chat actions sends successfully.|
+|*Location*|Your bot can receive location data, either from a single location data point or live location data. |Check the example.| [Location](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/tree/master/examples/ESP8266/Location/Location.ino)|
+|*Channel Post*|Reads posts from channels. |Check the example.| [ChannelPost](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/tree/master/examples/ESP8266/ChannelPost/ChannelPost.ino)|
+|*Long Poll*|Set how long the bot will wait checking for a new message before returning now messages.
This will decrease the amount of requests and data used by the bot, but it will tie up the arduino while it waits for messages |`bot.longPoll = 60;`
Where 60 is the amount of seconds it should wait | [LongPoll](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/tree/master/examples/ESP8266/LongPoll/LongPoll.ino)|
The full Telegram Bot API documentation can be read [here](https://core.telegram.org/bots/api). If there is a feature you would like added to the library please either raise a Github issue or please feel free to raise a Pull Request.
diff --git a/examples/101/FlashledBot/FlashledBot.ino b/examples/101/FlashledBot/FlashledBot.ino
index 29dad39..b5aa01f 100644
--- a/examples/101/FlashledBot/FlashledBot.ino
+++ b/examples/101/FlashledBot/FlashledBot.ino
@@ -23,7 +23,7 @@ const int ledPin = 13;
#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
WiFiSSLClient client;
-UniversalTelegramBot bot(BOTtoken, client);s
+UniversalTelegramBot bot(BOTtoken, client);
int Bot_mtbs = 1000; //mean time between scan messages
long Bot_lasttime; //last time messages' scan has been done
diff --git a/examples/ESP32/ChannelPost/ChannelPost.ino b/examples/ESP32/ChannelPost/ChannelPost.ino
new file mode 100644
index 0000000..4d2f92f
--- /dev/null
+++ b/examples/ESP32/ChannelPost/ChannelPost.ino
@@ -0,0 +1,66 @@
+/*******************************************************************
+* An example of bot that echos back any messages received,
+* including ones from channels
+*
+* written by Brian Lough
+*******************************************************************/
+#include
+#include
+#include
+
+// Initialize Wifi connection to the router
+char ssid[] = "XXXXXX"; // your network SSID (name)
+char password[] = "YYYYYY"; // your network key
+
+// Initialize Telegram BOT
+#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
+
+WiFiClientSecure client;
+UniversalTelegramBot bot(BOTtoken, client);
+
+int Bot_mtbs = 1000; //mean time between scan messages
+long Bot_lasttime; //last time messages' scan has been done
+
+void setup() {
+ Serial.begin(115200);
+
+ // Attempt to connect to Wifi network:
+ Serial.print("Connecting Wifi: ");
+ Serial.println(ssid);
+
+ // Set WiFi to station mode and disconnect from an AP if it was Previously
+ // connected
+ WiFi.mode(WIFI_STA);
+ WiFi.begin(ssid, password);
+
+ while (WiFi.status() != WL_CONNECTED) {
+ Serial.print(".");
+ delay(500);
+ }
+
+ Serial.println("");
+ Serial.println("WiFi connected");
+ Serial.print("IP address: ");
+ Serial.println(WiFi.localIP());
+}
+
+void loop() {
+ if (millis() > Bot_lasttime + Bot_mtbs) {
+ int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
+
+ while(numNewMessages) {
+ Serial.println("got response");
+ for (int i=0; i
+#include
+#include
+
+// Initialize Wifi connection to the router
+char ssid[] = "XXXXXX"; // your network SSID (name)
+char password[] = "YYYYYY"; // your network key
+
+// Initialize Telegram BOT
+#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
+
+WiFiClientSecure client;
+UniversalTelegramBot bot(BOTtoken, client);
+
+int Bot_mtbs = 1000; //mean time between scan messages
+long Bot_lasttime; //last time messages' scan has been done
+bool Start = false;
+
+void handleNewMessages(int numNewMessages) {
+ Serial.println("handleNewMessages");
+ Serial.println(String(numNewMessages));
+
+ for (int i=0; i Bot_lasttime + Bot_mtbs) {
+ int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
+
+ while(numNewMessages) {
+ Serial.println("got response");
+ handleNewMessages(numNewMessages);
+ numNewMessages = bot.getUpdates(bot.last_message_received + 1);
+ }
+
+ Bot_lasttime = millis();
+ }
+}
diff --git a/examples/ESP32/ChatAction/README.md b/examples/ESP32/ChatAction/README.md
new file mode 100644
index 0000000..46e3014
--- /dev/null
+++ b/examples/ESP32/ChatAction/README.md
@@ -0,0 +1,13 @@
+#ESP8266 - Chat Action
+
+This is a basic example of how to use chat action using UniversalTelegramBot for ESP8266 based boards.
+
+Application originally written by [Giancarlo Bacchio](giancarlo.bacchio@gmail.com) for [ESP8266-TelegramBot library](https://github.com/Gianbacchio/ESP8266-TelegramBot)
+
+Adapted by [Brian Lough](https://github.com/witnessmenow)
+
+NOTE: You will need to enter your SSID, password and bot Token for the example to work.
+
+## License
+
+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.
diff --git a/examples/ESP32/CustomKeyboard/InlineKeyboardMarkup/InlineKeyboardMarkup.ino b/examples/ESP32/CustomKeyboard/InlineKeyboardMarkup/InlineKeyboardMarkup.ino
new file mode 100644
index 0000000..26894d4
--- /dev/null
+++ b/examples/ESP32/CustomKeyboard/InlineKeyboardMarkup/InlineKeyboardMarkup.ino
@@ -0,0 +1,93 @@
+/*******************************************************************
+ An example of how to use a custom reply keyboard markup.
+
+
+ written by Vadim Sinitski (modified by Brian Lough)
+ *******************************************************************/
+#include
+#include
+#include
+
+// Initialize Wifi connection to the router
+char ssid[] = "XXXXXX"; // your network SSID (name)
+char password[] = "YYYYYY"; // your network key
+
+// Initialize Telegram BOT
+#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
+
+WiFiClientSecure client;
+UniversalTelegramBot bot(BOTtoken, client);
+
+int Bot_mtbs = 1000; //mean time between scan messages
+long Bot_lasttime; //last time messages' scan has been done
+
+void handleNewMessages(int numNewMessages) {
+
+ for (int i = 0; i < numNewMessages; i++) {
+
+ // Inline buttons with callbacks when pressed will raise a callback_query message
+ if (bot.messages[i].type == "callback_query") {
+ Serial.print("Call back button pressed by: ");
+ Serial.println(bot.messages[i].from_id);
+ Serial.print("Data on the button: ");
+ Serial.println(bot.messages[i].text);
+ bot.sendMessage(bot.messages[i].from_id, bot.messages[i].text, "");
+ } else {
+ String chat_id = String(bot.messages[i].chat_id);
+ String text = bot.messages[i].text;
+
+ String from_name = bot.messages[i].from_name;
+ if (from_name == "") from_name = "Guest";
+
+ if (text == "/options") {
+ String keyboardJson = "[[{ \"text\" : \"Go to Google\", \"url\" : \"https://www.google.com\" }],[{ \"text\" : \"Send\", \"callback_data\" : \"This was sent by inline\" }]]";
+ bot.sendMessageWithInlineKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson);
+ }
+
+ if (text == "/start") {
+ String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
+ welcome += "This is Inline Keyboard Markup example.\n\n";
+ welcome += "/options : returns the inline keyboard\n";
+
+ bot.sendMessage(chat_id, welcome, "Markdown");
+ }
+ }
+ }
+}
+
+void setup() {
+ Serial.begin(115200);
+
+ // Attempt to connect to Wifi network:
+ Serial.print("Connecting Wifi: ");
+ Serial.println(ssid);
+
+ // Set WiFi to station mode and disconnect from an AP if it was Previously
+ // connected
+ WiFi.mode(WIFI_STA);
+ WiFi.begin(ssid, password);
+
+ while (WiFi.status() != WL_CONNECTED) {
+ Serial.print(".");
+ delay(500);
+ }
+
+ Serial.println("");
+ Serial.println("WiFi connected");
+ Serial.print("IP address: ");
+ Serial.println(WiFi.localIP());
+}
+
+void loop() {
+ if (millis() > Bot_lasttime + Bot_mtbs) {
+ int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
+
+ while (numNewMessages) {
+ Serial.println("got response");
+ handleNewMessages(numNewMessages);
+ numNewMessages = bot.getUpdates(bot.last_message_received + 1);
+ }
+
+ Bot_lasttime = millis();
+ }
+}
diff --git a/examples/ESP32/CustomKeyboard/README.md b/examples/ESP32/CustomKeyboard/README.md
new file mode 100644
index 0000000..c8fb2f4
--- /dev/null
+++ b/examples/ESP32/CustomKeyboard/README.md
@@ -0,0 +1,28 @@
+#Reply Keyboard Markup
+
+This is an example of how to use reply keyboard markup on a ESP8266 based board.
+
+
+
+The application will turn on and off an LED based on commands received via telegram.
+
+#Inline Keyboard Markup
+
+This is an example of how to use reply keyboard markup on a ESP8266 based board.
+
+
+
+
+Right now working only URL redirection button. Other features will be added later.
+
+-----------------
+
+NOTE: You will need to enter your SSID, password and bot Token for the example to work.
+
+Application written by [Brian Lough](https://github.com/witnessmenow)
+
+
+
+## License
+
+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.
diff --git a/examples/ESP32/CustomKeyboard/ReplyKeyboardMarkup/ReplyKeyboardMarkup.ino b/examples/ESP32/CustomKeyboard/ReplyKeyboardMarkup/ReplyKeyboardMarkup.ino
new file mode 100644
index 0000000..dd3e08c
--- /dev/null
+++ b/examples/ESP32/CustomKeyboard/ReplyKeyboardMarkup/ReplyKeyboardMarkup.ino
@@ -0,0 +1,114 @@
+/*******************************************************************
+ * An example of how to use a custom reply keyboard markup. *
+ * *
+ * *
+ * written by Brian Lough *
+ *******************************************************************/
+#include
+#include
+#include
+
+// Initialize Wifi connection to the router
+char ssid[] = "XXXXXX"; // your network SSID (name)
+char password[] = "YYYYYY"; // your network key
+
+// Initialize Telegram BOT
+#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
+
+WiFiClientSecure client;
+UniversalTelegramBot bot(BOTtoken, client);
+
+int Bot_mtbs = 1000; //mean time between scan messages
+long Bot_lasttime; //last time messages' scan has been done
+
+const int ledPin = 13;
+int ledStatus = 0;
+
+void handleNewMessages(int numNewMessages) {
+ Serial.println("handleNewMessages");
+ Serial.println(String(numNewMessages));
+
+ for (int i=0; i Bot_lasttime + Bot_mtbs) {
+ int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
+
+ while(numNewMessages) {
+ Serial.println("got response");
+ handleNewMessages(numNewMessages);
+ numNewMessages = bot.getUpdates(bot.last_message_received + 1);
+ }
+
+ Bot_lasttime = millis();
+ }
+}
diff --git a/examples/ESP32/EchoBot/EchoBot.ino b/examples/ESP32/EchoBot/EchoBot.ino
new file mode 100644
index 0000000..041ee56
--- /dev/null
+++ b/examples/ESP32/EchoBot/EchoBot.ino
@@ -0,0 +1,61 @@
+/*******************************************************************
+* An example of bot that echos back any messages received *
+* *
+* written by Giacarlo Bacchio (Gianbacchio on Github) *
+* adapted by Brian Lough *
+*******************************************************************/
+#include
+#include
+#include
+
+// Initialize Wifi connection to the router
+char ssid[] = "XXXXXX"; // your network SSID (name)
+char password[] = "YYYYYY"; // your network key
+
+// Initialize Telegram BOT
+#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
+
+WiFiClientSecure client;
+UniversalTelegramBot bot(BOTtoken, client);
+
+int Bot_mtbs = 1000; //mean time between scan messages
+long Bot_lasttime; //last time messages' scan has been done
+
+void setup() {
+ Serial.begin(115200);
+
+ // Attempt to connect to Wifi network:
+ Serial.print("Connecting Wifi: ");
+ Serial.println(ssid);
+
+ // Set WiFi to station mode and disconnect from an AP if it was Previously
+ // connected
+ WiFi.mode(WIFI_STA);
+ WiFi.begin(ssid, password);
+
+ while (WiFi.status() != WL_CONNECTED) {
+ Serial.print(".");
+ delay(500);
+ }
+
+ Serial.println("");
+ Serial.println("WiFi connected");
+ Serial.print("IP address: ");
+ Serial.println(WiFi.localIP());
+}
+
+void loop() {
+ if (millis() > Bot_lasttime + Bot_mtbs) {
+ int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
+
+ while(numNewMessages) {
+ Serial.println("got response");
+ for (int i=0; i
+#include
+#include
+
+// Initialize Wifi connection to the router
+char ssid[] = "XXXXXX"; // your network SSID (name)
+char password[] = "YYYYYY"; // your network key
+
+// Initialize Telegram BOT
+#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
+
+WiFiClientSecure client;
+UniversalTelegramBot bot(BOTtoken, client);
+
+int Bot_mtbs = 1000; //mean time between scan messages
+long Bot_lasttime; //last time messages' scan has been done
+bool Start = false;
+
+const int ledPin = 13;
+int ledStatus = 0;
+
+void handleNewMessages(int numNewMessages) {
+ Serial.println("handleNewMessages");
+ Serial.println(String(numNewMessages));
+
+ for (int i=0; i Bot_lasttime + Bot_mtbs) {
+ int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
+
+ while(numNewMessages) {
+ Serial.println("got response");
+ handleNewMessages(numNewMessages);
+ numNewMessages = bot.getUpdates(bot.last_message_received + 1);
+ }
+
+ Bot_lasttime = millis();
+ }
+}
diff --git a/examples/ESP32/FlashLED/README.md b/examples/ESP32/FlashLED/README.md
new file mode 100644
index 0000000..a3e6f08
--- /dev/null
+++ b/examples/ESP32/FlashLED/README.md
@@ -0,0 +1,15 @@
+#ESP8266 - Flash LED
+
+This is a basic example of how to receive commands using UniversalTelegramBot for ESP8266 based boards.
+
+The application will turn on and off an LED based on commands received via telegram.
+
+Application originally written by [Giancarlo Bacchio](giancarlo.bacchio@gmail.com) for [ESP8266-TelegramBot library](https://github.com/Gianbacchio/ESP8266-TelegramBot)
+
+Adapted by [Brian Lough](https://github.com/witnessmenow)
+
+NOTE: You will need to enter your SSID, password and bot Token for the example to work.
+
+## License
+
+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.
diff --git a/examples/ESP32/Location/Location.ino b/examples/ESP32/Location/Location.ino
new file mode 100644
index 0000000..3253017
--- /dev/null
+++ b/examples/ESP32/Location/Location.ino
@@ -0,0 +1,87 @@
+/*******************************************************************
+ * An example of recieving location Data
+ *
+ *
+ * By Brian Lough
+ *******************************************************************/
+#include
+#include
+#include
+
+// Initialize Wifi connection to the router
+char ssid[] = "SSID"; // your network SSID (name)
+char password[] = "password"; // your network key
+
+// Initialize Telegram BOT
+#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
+
+WiFiClientSecure client;
+UniversalTelegramBot bot(BOTtoken, client);
+
+int Bot_mtbs = 1000; //mean time between scan messages
+long Bot_lasttime; //last time messages' scan has been done
+
+void handleNewMessages(int numNewMessages) {
+
+ for (int i = 0; i < numNewMessages; i++) {
+
+ String chat_id = String(bot.messages[i].chat_id);
+ String text = bot.messages[i].text;
+
+ String from_name = bot.messages[i].from_name;
+ if (from_name == "") from_name = "Guest";
+
+ if (bot.messages[i].longitude != 0 || bot.messages[i].latitude != 0) {
+ Serial.print("Long: ");
+ Serial.println(String(bot.messages[i].longitude, 6));
+ Serial.print("Lat: ");
+ Serial.println(String(bot.messages[i].latitude, 6));
+
+ String message = "Long: " + String(bot.messages[i].longitude, 6) + "\n";
+ message += "Lat: " + String(bot.messages[i].latitude, 6) + "\n";
+ bot.sendMessage(chat_id, message, "Markdown");
+ } else if (text == "/start") {
+ String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
+ welcome += "Share a location or a live location and the bot will respond with the co-ords\n";
+
+ bot.sendMessage(chat_id, welcome, "Markdown");
+ }
+ }
+}
+
+void setup() {
+ Serial.begin(115200);
+
+ // Attempt to connect to Wifi network:
+ Serial.print("Connecting Wifi: ");
+ Serial.println(ssid);
+
+ // Set WiFi to station mode and disconnect from an AP if it was Previously
+ // connected
+ WiFi.mode(WIFI_STA);
+ WiFi.begin(ssid, password);
+
+ while (WiFi.status() != WL_CONNECTED) {
+ Serial.print(".");
+ delay(500);
+ }
+
+ Serial.println("");
+ Serial.println("WiFi connected");
+ Serial.print("IP address: ");
+ Serial.println(WiFi.localIP());
+}
+
+void loop() {
+ if (millis() > Bot_lasttime + Bot_mtbs) {
+ int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
+
+ while (numNewMessages) {
+ Serial.println("got response");
+ handleNewMessages(numNewMessages);
+ numNewMessages = bot.getUpdates(bot.last_message_received + 1);
+ }
+
+ Bot_lasttime = millis();
+ }
+}
diff --git a/examples/ESP32/LongPoll/LongPoll.ino b/examples/ESP32/LongPoll/LongPoll.ino
new file mode 100644
index 0000000..83b0728
--- /dev/null
+++ b/examples/ESP32/LongPoll/LongPoll.ino
@@ -0,0 +1,66 @@
+/*******************************************************************
+* An example of setting a long poll, this will mean the request
+* for new messages will wait the specified amount of time before
+* returning with no messages
+*
+* This should reduce amount of data used by the bot
+*
+* written by Brian Lough
+*******************************************************************/
+#include
+#include
+#include
+
+// Initialize Wifi connection to the router
+char ssid[] = "XXXXXX"; // your network SSID (name)
+char password[] = "YYYYYY"; // your network key
+
+// Initialize Telegram BOT
+#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
+
+WiFiClientSecure client;
+UniversalTelegramBot bot(BOTtoken, client);
+
+int Bot_mtbs = 1000; //mean time between scan messages
+long Bot_lasttime; //last time messages' scan has been done
+
+void setup() {
+ Serial.begin(115200);
+
+ // Attempt to connect to Wifi network:
+ Serial.print("Connecting Wifi: ");
+ Serial.println(ssid);
+
+ // Set WiFi to station mode and disconnect from an AP if it was Previously
+ // connected
+ WiFi.mode(WIFI_STA);
+ WiFi.begin(ssid, password);
+
+ while (WiFi.status() != WL_CONNECTED) {
+ Serial.print(".");
+ delay(500);
+ }
+
+ bot.longPoll = 60;
+
+ Serial.println("");
+ Serial.println("WiFi connected");
+ Serial.print("IP address: ");
+ Serial.println(WiFi.localIP());
+}
+
+void loop() {
+ if (millis() > Bot_lasttime + Bot_mtbs) {
+ int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
+
+ while(numNewMessages) {
+ Serial.println("got response");
+ for (int i=0; i
+#include
+#include
+
+// Initialize Wifi connection to the router
+char ssid[] = "XXXXXX"; // your network SSID (name)
+char password[] = "YYYYYY"; // your network key
+
+// Initialize Telegram BOT
+#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
+
+WiFiClientSecure client;
+UniversalTelegramBot bot(BOTtoken, client);
+
+int Bot_mtbs = 1000; //mean time between scan messages
+long Bot_lasttime; //last time messages' scan has been done
+
+void setup() {
+ Serial.begin(115200);
+
+ // Set WiFi to station mode and disconnect from an AP if it was Previously
+ // connected
+ WiFi.mode(WIFI_STA);
+ WiFi.disconnect();
+ delay(100);
+
+ // Attempt to connect to Wifi network:
+ Serial.print("Connecting Wifi: ");
+ Serial.println(ssid);
+ WiFi.begin(ssid, password);
+
+ while (WiFi.status() != WL_CONNECTED) {
+ Serial.print(".");
+ delay(500);
+ }
+
+ Serial.println("");
+ Serial.println("WiFi connected");
+ Serial.print("IP address: ");
+ Serial.println(WiFi.localIP());
+}
+
+void loop() {
+ if (millis() > Bot_lasttime + Bot_mtbs) {
+ int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
+
+ while(numNewMessages) {
+ Serial.println("got response");
+ for (int i=0; i
#include
@@ -22,27 +22,35 @@ int Bot_mtbs = 1000; //mean time between scan messages
long Bot_lasttime; //last time messages' scan has been done
void handleNewMessages(int numNewMessages) {
- Serial.println("handleNewMessages");
- Serial.println(String(numNewMessages));
-
- for (int i=0; i Bot_lasttime + Bot_mtbs) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
- while(numNewMessages) {
+ while (numNewMessages) {
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
diff --git a/examples/ESP8266/Location/Location.ino b/examples/ESP8266/Location/Location.ino
new file mode 100644
index 0000000..55ee574
--- /dev/null
+++ b/examples/ESP8266/Location/Location.ino
@@ -0,0 +1,88 @@
+/*******************************************************************
+ * An example of recieving location Data
+ *
+ *
+ * By Brian Lough
+ *******************************************************************/
+#include
+#include
+#include
+
+// Initialize Wifi connection to the router
+char ssid[] = "SSID"; // your network SSID (name)
+char password[] = "password"; // your network key
+
+// Initialize Telegram BOT
+#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
+
+WiFiClientSecure client;
+UniversalTelegramBot bot(BOTtoken, client);
+
+int Bot_mtbs = 1000; //mean time between scan messages
+long Bot_lasttime; //last time messages' scan has been done
+
+void handleNewMessages(int numNewMessages) {
+
+ for (int i = 0; i < numNewMessages; i++) {
+
+ String chat_id = String(bot.messages[i].chat_id);
+ String text = bot.messages[i].text;
+
+ String from_name = bot.messages[i].from_name;
+ if (from_name == "") from_name = "Guest";
+
+ if (bot.messages[i].longitude != 0 || bot.messages[i].latitude != 0) {
+ Serial.print("Long: ");
+ Serial.println(String(bot.messages[i].longitude, 6));
+ Serial.print("Lat: ");
+ Serial.println(String(bot.messages[i].latitude, 6));
+
+ String message = "Long: " + String(bot.messages[i].longitude, 6) + "\n";
+ message += "Lat: " + String(bot.messages[i].latitude, 6) + "\n";
+ bot.sendMessage(chat_id, message, "Markdown");
+ } else if (text == "/start") {
+ String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
+ welcome += "Share a location or a live location and the bot will respond with the co-ords\n";
+
+ bot.sendMessage(chat_id, welcome, "Markdown");
+ }
+ }
+}
+
+void setup() {
+ Serial.begin(115200);
+
+ // Set WiFi to station mode and disconnect from an AP if it was Previously connected
+ WiFi.mode(WIFI_STA);
+ WiFi.disconnect();
+ delay(100);
+
+ // attempt to connect to Wifi network:
+ Serial.print("Connecting Wifi: ");
+ Serial.println(ssid);
+ WiFi.begin(ssid, password);
+
+ while (WiFi.status() != WL_CONNECTED) {
+ Serial.print(".");
+ delay(500);
+ }
+
+ Serial.println("");
+ Serial.println("WiFi connected");
+ Serial.print("IP address: ");
+ Serial.println(WiFi.localIP());
+}
+
+void loop() {
+ if (millis() > Bot_lasttime + Bot_mtbs) {
+ int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
+
+ while (numNewMessages) {
+ Serial.println("got response");
+ handleNewMessages(numNewMessages);
+ numNewMessages = bot.getUpdates(bot.last_message_received + 1);
+ }
+
+ Bot_lasttime = millis();
+ }
+}
diff --git a/examples/ESP8266/LongPoll/LongPoll.ino b/examples/ESP8266/LongPoll/LongPoll.ino
new file mode 100644
index 0000000..9840ba0
--- /dev/null
+++ b/examples/ESP8266/LongPoll/LongPoll.ino
@@ -0,0 +1,68 @@
+/*******************************************************************
+* An example of setting a long poll, this will mean the request
+* for new messages will wait the specified amount of time before
+* returning with no messages
+*
+* This should reduce amount of data used by the bot
+*
+* written by Brian Lough
+*******************************************************************/
+#include
+#include
+#include
+
+// Initialize Wifi connection to the router
+char ssid[] = "XXXXXX"; // your network SSID (name)
+char password[] = "YYYYYY"; // your network key
+
+// Initialize Telegram BOT
+#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
+
+WiFiClientSecure client;
+UniversalTelegramBot bot(BOTtoken, client);
+
+int Bot_mtbs = 1000; //mean time between scan messages
+long Bot_lasttime; //last time messages' scan has been done
+
+void setup() {
+ Serial.begin(115200);
+
+ // Set WiFi to station mode and disconnect from an AP if it was Previously
+ // connected
+ WiFi.mode(WIFI_STA);
+ WiFi.disconnect();
+ delay(100);
+
+ // Attempt to connect to Wifi network:
+ Serial.print("Connecting Wifi: ");
+ Serial.println(ssid);
+ WiFi.begin(ssid, password);
+
+ while (WiFi.status() != WL_CONNECTED) {
+ Serial.print(".");
+ delay(500);
+ }
+
+ bot.longPoll = 60;
+
+ Serial.println("");
+ Serial.println("WiFi connected");
+ Serial.print("IP address: ");
+ Serial.println(WiFi.localIP());
+}
+
+void loop() {
+ if (millis() > Bot_lasttime + Bot_mtbs) {
+ int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
+
+ while(numNewMessages) {
+ Serial.println("got response");
+ for (int i=0; i
sentence=Arduino Telegram Bot library for multiple different architectures.
diff --git a/platformio.ini b/platformio.ini
index 2280717..987c62e 100644
--- a/platformio.ini
+++ b/platformio.ini
@@ -15,3 +15,9 @@ platform = espressif8266
board = d1_mini
framework = arduino
lib_deps = WifiManager, ${common.lib_deps_external}
+
+[env:esp32dev]
+platform = espressif32_stage
+board = esp32dev
+framework = arduino
+lib_deps = ${common.lib_deps_external}
diff --git a/platformioExamples/AdvancedLED/.gitignore b/platformioExamples/AdvancedLED/.gitignore
new file mode 100644
index 0000000..5dac9f5
--- /dev/null
+++ b/platformioExamples/AdvancedLED/.gitignore
@@ -0,0 +1,4 @@
+.pioenvs
+.piolibdeps
+.clang_complete
+.gcc-flags.json
diff --git a/platformioExamples/AdvancedLED/.travis.yml b/platformioExamples/AdvancedLED/.travis.yml
new file mode 100644
index 0000000..52072ef
--- /dev/null
+++ b/platformioExamples/AdvancedLED/.travis.yml
@@ -0,0 +1,55 @@
+# Continuous Integration (CI) is the practice, in software
+# engineering, of merging all developer working copies with a shared mainline
+# several times a day < http://docs.platformio.org/page/ci/index.html >
+#
+# Documentation:
+#
+# * Travis CI Embedded Builds with PlatformIO
+# < https://docs.travis-ci.com/user/integration/platformio/ >
+#
+# * PlatformIO integration with Travis CI
+# < http://docs.platformio.org/page/ci/travis.html >
+#
+# * User Guide for `platformio ci` command
+# < http://docs.platformio.org/page/userguide/cmd_ci.html >
+#
+#
+# Please choice one of the following templates (proposed below) and uncomment
+# it (remove "# " before each line) or use own configuration according to the
+# Travis CI documentation (see above).
+#
+
+
+#
+# Template #1: General project. Test it using existing `platformio.ini`.
+#
+
+# language: python
+# python:
+# - "2.7"
+#
+# install:
+# - pip install -U platformio
+#
+# script:
+# - platformio run
+
+
+#
+# Template #2: The project is intended to by used as a library with examples
+#
+
+# language: python
+# python:
+# - "2.7"
+#
+# env:
+# - PLATFORMIO_CI_SRC=path/to/test/file.c
+# - PLATFORMIO_CI_SRC=examples/file.ino
+# - PLATFORMIO_CI_SRC=path/to/test/directory
+#
+# install:
+# - pip install -U platformio
+#
+# script:
+# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N
diff --git a/platformioExamples/AdvancedLED/README.md b/platformioExamples/AdvancedLED/README.md
new file mode 100644
index 0000000..6f8124e
--- /dev/null
+++ b/platformioExamples/AdvancedLED/README.md
@@ -0,0 +1,39 @@
+## ESP32 - AdvancedLED PlatformIO
+PlatformIO example for controlling an LED from a Telegram Bot.
+This example sets up an ESP32 to be controlled through a Telegram Bot.
+As of January 3rd, 2018, the platform (in platformio.ini) must be set to espressif32_stage.
+
+#### Pre-requisites
+* You must have a Telegram Bot for this example to work. To make one,
+ 1. Open Telegram (on mobile, web, or desktop)
+ 2. Start a chat with BotFather (@BotFather)
+ 3. Send `/start` to BotFather, followed by `/newbot`
+ 4. Send a friendly name for your bot (this isn't the username of bot)
+ 5. Type in and send the username for your bot (ending in bot)
+ 6. Copy the token provided by BotFather and paste it at BOTtoken below
+* Modify `ssid` and `password` to match your WiFi network settings
+* Modify `BOTtoken` with the correct bot token acquired from Botfather
+* Modify `ledPin` to match the GPIO pin on which the LED on your board is connected
+
+#### Available commands for the bot:
+* /start - Displays an intro message in chat and provides a custom keyboard with available commands
+* /options - Displays a custom keyboard with available commands
+* /ledon - Turns on the LED specified by `ledPin`
+* /ledoff - Turns off the LED specified by `ledPin`
+* /blink - Starts blinking the LED specified by `ledPin`
+* /PWM - Provides an inline keyboard for setting a PWM value for the LED specified by `ledPin`
+
+#### Notes
+* Clicking on a button on an inline keyboard sends back a message of type="callback_query".
+* Blink is implemented using low frequency PWM, as fetching message updates can take enough time to cause a noticeable lag in blinking period.
+* LED implementation is based on inverse polarity of LED.
+* Telegram Bot API documentation available at https://core.telegram.org/bots/api
+
+## Credit
+Application originally written by [Giancarlo Bacchio](giancarlo.bacchio@gmail.com) for [ESP8266-TelegramBot library](https://github.com/Gianbacchio/ESP8266-TelegramBot)
+Adapted by [Brian Lough](https://github.com/witnessmenow) for UniversalTelegramBot Library
+Adapted for ESP32, and modified for advance features by [Pranav Sharma](https://github.com/pro2xy)
+
+## License
+
+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.
diff --git a/platformioExamples/AdvancedLED/platformio.ini b/platformioExamples/AdvancedLED/platformio.ini
new file mode 100644
index 0000000..b29152d
--- /dev/null
+++ b/platformioExamples/AdvancedLED/platformio.ini
@@ -0,0 +1,17 @@
+; PlatformIO Project Configuration File
+;
+; Build options: build flags, source filter
+; Upload options: custom upload port, speed and extra flags
+; Library options: dependencies, extra library storages
+; Advanced options: extra scripting
+;
+; Please visit documentation for the other options and examples
+; http://docs.platformio.org/page/projectconf.html
+[common]
+lib_deps_external = ArduinoJson
+
+[env:esp32dev]
+platform = espressif32_stage
+board = esp32dev
+framework = arduino
+lib_deps = ${common.lib_deps_external}
diff --git a/platformioExamples/AdvancedLED/src/main.cpp b/platformioExamples/AdvancedLED/src/main.cpp
new file mode 100644
index 0000000..1e621ba
--- /dev/null
+++ b/platformioExamples/AdvancedLED/src/main.cpp
@@ -0,0 +1,202 @@
+/*******************************************************************
+Example for controlling an LED from a Telegram Bot
+
+You must have a Telegram Bot for this example to work. To make one,
+1. Open Telegram (on mobile, web, or desktop)
+2. Start a chat with BotFather (@BotFather)
+3. Send /start to BotFather, followed by /newbot
+4. Send a friendly name for your bot (this isn't the username of bot)
+5. Type in and send the username for your bot (ending in bot)
+6. Copy the token provided by BotFather and paste it at BOTtoken below
+
+Telegram Bot API documentation available at https://core.telegram.org/bots/api
+
+Note: As of 3rd Jan. 2018, it is necessary to use espressif32_stage
+platform for PlatformIO
+
+written by Giacarlo Bacchio (Gianbacchio on Github)
+adapted by Brian Lough ( witnessmenow ) for UniversalTelegramBot library
+adapted by Pranav Sharma ( PRO2XY ) for ESP32 on PlatformIO
+Library related discussions on https://t.me/arduino_telegram_library
+
+*******************************************************************/
+#include
+#include
+#include
+#include
+
+
+// WiFi parameters
+const char *ssid = "SSID"; // your network SSID (name)
+const char *password = "PASS"; // your network key
+
+// Bot parameters
+#define BOTtoken "xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxx" // your Bot Token (Get from Botfather)
+int Bot_mtbs = 1000; //mean time between scan messages
+long Bot_lasttime; //last time messages' scan has been done
+
+// LED parameters
+const int ledPin = 16; // Internal Red LED on Karyni (2 on NodeMCU-32S)
+enum ledModes {ON = 0, OFF, BLINK, PWM}; // define modes of operation for LED (inverse polarity)
+enum ledModes ledmode = OFF; // Start with off
+
+WiFiClientSecure client;
+UniversalTelegramBot bot(BOTtoken, client);
+
+// Function prototypes (PlatformIO doesn't make these for you automatically)
+void handleNewMessages(int numNewMessages); // parses new messages and sends them to msgInterpretation
+void msgInterpretation(String from_name, String text, String chat_id, String message_type);
+
+
+void setup() {
+ Serial.begin(115200);
+ //bot._debug=true; // uncomment to see debug messages from bot library
+
+ // attempt to connect to Wifi network:
+ Serial.print("Connecting Wifi: ");
+ Serial.println(ssid);
+ // Start WiFi in Station mode
+ WiFi.mode(WIFI_STA);
+ WiFi.begin(ssid, password);
+
+ // Wait for WiFi to connect
+ while (WiFi.status() != WL_CONNECTED) {
+ Serial.print('.');
+ delay(500);
+ }
+
+ Serial.println("\r\nConnected!");
+ Serial.print("IP address: ");
+ Serial.println(WiFi.localIP());
+
+ pinMode(ledPin, OUTPUT); // initialize ledPin as an output.
+ digitalWrite(ledPin, HIGH); // initialize pin as high (LED Off)
+}
+
+void loop() {
+ // Every "Bot_mtbs" the bot checks if any messages have arrived
+ if (millis() > Bot_lasttime + Bot_mtbs) {
+ Bot_lasttime = millis();
+ Serial.print(F("Checking for messages.. "));
+ int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
+ Serial.print(numNewMessages); Serial.println(" new messages");
+
+ if(numNewMessages > 0){
+ handleNewMessages(numNewMessages);
+ }
+ }
+}
+
+// Parse new messages and send them for interpretation
+void handleNewMessages(int numNewMessages) {
+ // Extract info from the message
+ for (int i = 0; i < numNewMessages; i++) {
+ Serial.print(F("Handling message ")); Serial.println(i+1);
+ String chat_id = String(bot.messages[i].chat_id);
+ String text = bot.messages[i].text;
+
+ String from_name = bot.messages[i].from_name;
+ if (from_name == "") from_name = "Guest";
+ // Call the function for understand the message
+ msgInterpretation(from_name, text, chat_id, bot.messages[i].type);
+ }
+}
+
+void msgInterpretation(String from_name, String text, String chat_id, String message_type) {
+ Serial.print(F("Interpreting message: ")); Serial.println(text);
+ Serial.print(F("Type: ")); Serial.println(message_type);
+ Serial.print(F("From: ")); Serial.println(from_name);
+
+ if (text == "/start") { // First interaction of user
+ String welcome = "Hi " + from_name + "!\n";
+ welcome += "I am your Telegram Bot running on ESP32.\n\n";
+ welcome += "Select one of the /options below!\n\n";
+ String keyboardJson = "[[\"/ledon\", \"/ledoff\"],[\"/blink\", \"/PWM\"],[\"/status\"]]";
+ bot.sendMessageWithReplyKeyboard(chat_id, welcome, "Markdown", keyboardJson, true);
+ }
+
+ if (text == "/options") { // List out the custom keyboard
+ String keyboardJson = "[[\"/ledon\", \"/ledoff\"],[\"/blink\", \"/PWM\"],[\"/status\"]]";
+ bot.sendMessageWithReplyKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson, true);
+ }
+
+ if (text == "/status") { // Report present ledmode to user
+ String response = "LED is ";
+ switch (ledmode){
+ case ON:
+ response += "on";
+ break;
+ case OFF:
+ response += "off";
+ break;
+ case BLINK:
+ response += "blinking";
+ break;
+ case PWM:
+ response += "PWMing";
+ break;
+ }
+ bot.sendMessage(chat_id, response, "");
+ }
+
+ if (text == "/ledon") {
+ ledmode = ON;
+ ledcDetachPin(ledPin); // detach pin from ledc
+ digitalWrite(ledPin, LOW); // turn the LED on (drive pin LOW)
+ Serial.println(F("Turning LED on"));
+ bot.sendMessage(chat_id, "Turning LED on", "");
+ }
+
+ if (text == "/ledoff") {
+ ledmode = OFF;
+ ledcDetachPin(ledPin); // detach pin from ledc
+ digitalWrite(ledPin, HIGH); // turn the LED off (drive pin HIGH)
+ Serial.println(F("Turning LED off"));
+ bot.sendMessage(chat_id, "Turning LED off", "");
+ }
+
+ if (text == "/blink") {
+ ledmode = BLINK;
+ // We use ledc for blinking by setting PWM at a low frequency
+ ledcSetup(0, 1, 8); // Channel, Freq., Resolution
+ ledcAttachPin(ledPin, 0); // Pin, Channel
+ ledcWrite(0, 192); // Channel, Duty (stay off for 90% time) (remember inverse polarity!)
+ Serial.println(F("Blink set"));
+ bot.sendMessage(chat_id, "Blink set", "");
+ }
+
+ if (text == "/PWM") { // Send an inline keyboard for seleting PWM values (percentage)
+ // For inline keyboard markup, see https://core.telegram.org/bots/api#inlinekeyboardmarkup
+ String keyboardJson = "[[{ \"text\" : \"0\", \"callback_data\" : \"0\" }],";
+ keyboardJson += "[{ \"text\" : \"10\", \"callback_data\" : \"10\" },";
+ keyboardJson += "{ \"text\" : \"20\", \"callback_data\" : \"20\" },";
+ keyboardJson += "{ \"text\" : \"30\", \"callback_data\" : \"30\" }],";
+ keyboardJson += "[{ \"text\" : \"40\", \"callback_data\" : \"40\" },";
+ keyboardJson += "{ \"text\" : \"50\", \"callback_data\" : \"50\" },";
+ keyboardJson += "{ \"text\" : \"60\", \"callback_data\" : \"60\" }],";
+ keyboardJson += "[{ \"text\" : \"70\", \"callback_data\" : \"70\" },";
+ keyboardJson += "{ \"text\" : \"80\", \"callback_data\" : \"80\" },";
+ keyboardJson += "{ \"text\" : \"90\", \"callback_data\" : \"90\" }],";
+ keyboardJson += "[{ \"text\" : \"100\", \"callback_data\" : \"100\" }]]";
+ Serial.println(F("Sending PWM keyboard"));
+ bot.sendMessageWithInlineKeyboard(chat_id, "Set PWM level", "", keyboardJson);
+ }
+
+ if(message_type=="callback_query") { // Received when user taps a button on inline keyboard
+ // In our case, callback_query is only received for PWM values. In other cases you may
+ // want to append an identifier to the values sent in 'callback_data' (e.g. 'duty=10')
+ // and then check for it here using text.startsWith("duty=") or something similar.
+ ledmode = PWM; // set proper LED mode
+ uint8_t duty = text.toInt(); // Convert value to int
+ duty = duty*2.55; // For duty between 0 - 100%, actual duty would be between 0 - 255
+
+ // We use ledc for PWM
+ ledcSetup(0, 5000, 8); // Channel, Freq., Resolution
+ ledcAttachPin(ledPin, 0); // Pin, Channel
+ ledcWrite(0, 255-duty); // Channel, Duty (255 - x to inverse polarity)
+ Serial.println(F("PWM set"));
+ String message = "PWM set with duty ";
+ message += String(duty);
+ bot.sendMessage(chat_id, message, "");
+ }
+}
diff --git a/scripts/travis/platformio.sh b/scripts/travis/platformio.sh
new file mode 100644
index 0000000..f778850
--- /dev/null
+++ b/scripts/travis/platformio.sh
@@ -0,0 +1,3 @@
+#!/bin/sh -eux
+
+platformio ci $PWD/examples/$BOARDTYPE$EXAMPLE_FOLDER$EXAMPLE_NAME/ -b $BOARD --lib="."
diff --git a/src/UniversalTelegramBot.cpp b/src/UniversalTelegramBot.cpp
index 593e9f7..e68ba77 100644
--- a/src/UniversalTelegramBot.cpp
+++ b/src/UniversalTelegramBot.cpp
@@ -1,130 +1,163 @@
/*
-Copyright (c) 2015 Giancarlo Bacchio. All right reserved.
+ Copyright (c) 2018 Brian Lough. All right reserved.
-TelegramBot - Library to create your own Telegram Bot using
-ESP8266 on Arduino IDE.
-Ref. Library at https:github/esp8266/Arduino
+ UniversalTelegramBot - Library to create your own Telegram Bot using
+ ESP8266 or ESP32 on Arduino IDE.
-This library is free software; you can redistribute it and/or
-modify it under the terms of the GNU Lesser General Public
-License as published by the Free Software Foundation; either
-version 2.1 of the License, or (at your option) any later version.
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
-This library is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-Lesser General Public License for more details.
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
-You should have received a copy of the GNU Lesser General Public
-License along with this library; if not, write to the Free Software
-Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-*/
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/*
+ **** Note Regarding Client Connection Keeping ****
+ Client connection is established in functions that directly involve use of
+ client, i.e sendGetToTelegram, sendPostToTelegram, and
+ sendMultipartFormDataToTelegram. It is closed at the end of
+ sendMultipartFormDataToTelegram, but not at the end of sendGetToTelegram and
+ sendPostToTelegram as these may need to keep the connection alive for respose
+ / response checking. Re-establishing a connection then wastes time which is
+ noticeable in user experience. Due to this, it is important that connection
+ be closed manually after calling sendGetToTelegram or sendPostToTelegram by
+ calling closeClient(); Failure to close connection causes memory leakage and
+ SSL errors
+ */
#include "UniversalTelegramBot.h"
-UniversalTelegramBot::UniversalTelegramBot(String token, Client &client) {
+UniversalTelegramBot::UniversalTelegramBot(String token, Client &client) {
_token = token;
this->client = &client;
}
String UniversalTelegramBot::sendGetToTelegram(String command) {
- String mess = "";
- long now;
- bool avail;
-
- // Connect with api.telegram.org
- if (client->connect(HOST, SSL_PORT)) {
- if (_debug) Serial.println(".... connected to server");
- String a="";
- char c;
- int ch_count=0;
- client->println("GET /"+command);
- now=millis();
- avail=false;
- while (millis() - now<1500) {
- while (client->available()) {
- char c = client->read();
- //Serial.write(c);
- if (ch_count < maxMessageLength) {
- mess=mess+c;
- ch_count++;
- }
- avail=true;
- }
- if (avail) {
+ String mess = "";
+ long now;
+ bool avail;
+
+ // Connect with api.telegram.org if not already connected
+ if (!client->connected()) {
+ if (_debug)
+ Serial.println(F("[BOT]Connecting to server"));
+ if (!client->connect(HOST, SSL_PORT)) {
+ if (_debug)
+ Serial.println(F("[BOT]Conection error"));
+ }
+ }
+ if (client->connected()) {
+
+ if (_debug)
+ Serial.println(F(".... connected to server"));
+
+ String a = "";
+ char c;
+ int ch_count = 0;
+ client->println("GET /" + command);
+ now = millis();
+ avail = false;
+ while (millis() - now < longPoll * 1000 + waitForResponse) {
+ while (client->available()) {
+ char c = client->read();
+ // Serial.write(c);
+ if (ch_count < maxMessageLength) {
+ mess = mess + c;
+ ch_count++;
+ }
+ avail = true;
+ }
+ if (avail) {
if (_debug) {
Serial.println();
Serial.println(mess);
Serial.println();
}
- break;
- }
- }
- }
+ break;
+ }
+ }
+ }
- return mess;
+ return mess;
}
-String UniversalTelegramBot::sendPostToTelegram(String command, JsonObject& payload){
+String UniversalTelegramBot::sendPostToTelegram(String command,
+ JsonObject &payload) {
String body = "";
String headers = "";
- long now;
- bool responseReceived;
-
- // Connect with api.telegram.org
- if (client->connect(HOST, SSL_PORT)) {
+ long now;
+ bool responseReceived;
+
+ // Connect with api.telegram.org if not already connected
+ if (!client->connected()) {
+ if (_debug)
+ Serial.println(F("[BOT Client]Connecting to server"));
+ if (!client->connect(HOST, SSL_PORT)) {
+ if (_debug)
+ Serial.println(F("[BOT Client]Conection error"));
+ }
+ }
+ if (client->connected()) {
// POST URI
- client->print("POST /" + command); client->println(" HTTP/1.1");
+ client->print("POST /" + command);
+ client->println(F(" HTTP/1.1"));
// Host header
- client->print("Host:"); client->println(HOST);
+ client->print(F("Host:"));
+ client->println(HOST);
// JSON content type
- client->println("Content-Type: application/json");
+ client->println(F("Content-Type: application/json"));
// Content length
int length = payload.measureLength();
- client->print("Content-Length:"); client->println(length);
+ client->print(F("Content-Length:"));
+ client->println(length);
// End of headers
client->println();
// POST message body
- //json.printTo(client); // very slow ??
+ // json.printTo(client); // very slow ??
String out;
payload.printTo(out);
client->println(out);
- int ch_count=0;
+ int ch_count = 0;
char c;
- now=millis();
- responseReceived=false;
+ now = millis();
+ responseReceived = false;
bool finishedHeaders = false;
bool currentLineIsBlank = true;
- while (millis()-now<1500) {
- while (client->available()) {
- char c = client->read();
- responseReceived=true;
-
+ while (millis() - now < waitForResponse) {
+ while (client->available()) {
+ char c = client->read();
+ responseReceived = true;
- if(!finishedHeaders){
+ if (!finishedHeaders) {
if (currentLineIsBlank && c == '\n') {
finishedHeaders = true;
- }
- else {
+ } else {
headers = headers + c;
}
} else {
if (ch_count < maxMessageLength) {
- body=body+c;
+ body = body + c;
ch_count++;
- }
+ }
}
if (c == '\n') {
currentLineIsBlank = true;
- }else if (c != '\r') {
+ } else if (c != '\r') {
currentLineIsBlank = false;
}
-
- }
+ }
if (responseReceived) {
if (_debug) {
@@ -132,121 +165,136 @@ String UniversalTelegramBot::sendPostToTelegram(String command, JsonObject& payl
Serial.println(body);
Serial.println();
}
- break;
- }
- }
+ break;
+ }
+ }
}
return body;
}
-String UniversalTelegramBot::sendMultipartFormDataToTelegram(String command, String binaryProperyName,
- String fileName, String contentType,
- String chat_id, int fileSize,
+String UniversalTelegramBot::sendMultipartFormDataToTelegram(
+ String command, String binaryProperyName, String fileName,
+ String contentType, String chat_id, int fileSize,
MoreDataAvailable moreDataAvailableCallback,
GetNextByte getNextByteCallback) {
String body = "";
String headers = "";
- long now;
- bool responseReceived;
- String boundry = "------------------------b8f610217e83e29b";
- // Connect with api.telegram.org
- if (client->connect(HOST, SSL_PORT)) {
+ long now;
+ bool responseReceived;
+ String boundry = F("------------------------b8f610217e83e29b");
+
+ // Connect with api.telegram.org if not already connected
+ if (!client->connected()) {
+ if (_debug)
+ Serial.println(F("[BOT Client]Connecting to server"));
+ if (!client->connect(HOST, SSL_PORT)) {
+ if (_debug)
+ Serial.println(F("[BOT Client]Conection error"));
+ }
+ }
+ if (client->connected()) {
String start_request = "";
String end_request = "";
start_request = start_request + "--" + boundry + "\r\n";
- start_request = start_request + "content-disposition: form-data; name=\"chat_id\"" + "\r\n";
+ start_request = start_request +
+ "content-disposition: form-data; name=\"chat_id\"" + "\r\n";
start_request = start_request + "\r\n";
start_request = start_request + chat_id + "\r\n";
start_request = start_request + "--" + boundry + "\r\n";
- start_request = start_request + "content-disposition: form-data; name=\"" + binaryProperyName + "\"; filename=\"" + fileName + "\"" + "\r\n";
+ start_request = start_request + "content-disposition: form-data; name=\"" +
+ binaryProperyName + "\"; filename=\"" + fileName + "\"" +
+ "\r\n";
start_request = start_request + "Content-Type: " + contentType + "\r\n";
start_request = start_request + "\r\n";
-
end_request = end_request + "\r\n";
end_request = end_request + "--" + boundry + "--" + "\r\n";
- client->print("POST /bot"+_token+"/" + command); client->println(" HTTP/1.1");
+ client->print("POST /bot" + _token + "/" + command);
+ client->println(F(" HTTP/1.1"));
// Host header
- client->print("Host: "); client->println(HOST);
- client->println("User-Agent: arduino/1.0");
- client->println("Accept: */*");
-
- int contentLength = fileSize + start_request.length() + end_request.length();
- if (_debug) Serial.println("Content-Length: " + String(contentLength));
- client->print("Content-Length: "); client->println(String(contentLength));
+ client->print(F("Host: "));
+ client->println(HOST);
+ client->println(F("User-Agent: arduino/1.0"));
+ client->println(F("Accept: */*"));
+
+ int contentLength =
+ fileSize + start_request.length() + end_request.length();
+ if (_debug)
+ Serial.println("Content-Length: " + String(contentLength));
+ client->print("Content-Length: ");
+ client->println(String(contentLength));
client->println("Content-Type: multipart/form-data; boundary=" + boundry);
client->println("");
client->print(start_request);
- if (_debug) Serial.print(start_request);
+ if (_debug)
+ Serial.print(start_request);
byte buffer[512];
int count = 0;
char ch;
while (moreDataAvailableCallback()) {
buffer[count] = getNextByteCallback();
- //client->write(ch);
- //Serial.write(ch);
+ // client->write(ch);
+ // Serial.write(ch);
count++;
- if(count == 512){
- //yield();
+ if (count == 512) {
+ // yield();
if (_debug) {
- Serial.println("Sending full buffer");
+ Serial.println(F("Sending full buffer"));
}
client->write((const uint8_t *)buffer, 512);
count = 0;
}
}
- if(count > 0) {
+ if (count > 0) {
if (_debug) {
- Serial.println("Sending remaining buffer");
+ Serial.println(F("Sending remaining buffer"));
}
client->write((const uint8_t *)buffer, count);
}
client->print(end_request);
- if (_debug) Serial.print(end_request);
+ if (_debug)
+ Serial.print(end_request);
count = 0;
- int ch_count=0;
+ int ch_count = 0;
char c;
- now=millis();
+ now = millis();
bool finishedHeaders = false;
bool currentLineIsBlank = true;
- while (millis()-now<1500) {
+ while (millis() - now < waitForResponse) {
while (client->available()) {
char c = client->read();
- responseReceived=true;
+ responseReceived = true;
-
- if(!finishedHeaders){
+ if (!finishedHeaders) {
if (currentLineIsBlank && c == '\n') {
finishedHeaders = true;
- }
- else {
+ } else {
headers = headers + c;
}
} else {
if (ch_count < maxMessageLength) {
- body=body+c;
+ body = body + c;
ch_count++;
}
}
if (c == '\n') {
currentLineIsBlank = true;
- }else if (c != '\r') {
+ } else if (c != '\r') {
currentLineIsBlank = false;
}
-
}
if (responseReceived) {
@@ -260,16 +308,20 @@ String UniversalTelegramBot::sendMultipartFormDataToTelegram(String command, Str
}
}
+ closeClient();
return body;
}
bool UniversalTelegramBot::getMe() {
- String command = "bot"+_token+"/getMe";
- String response = sendGetToTelegram(command); //receive reply from telegram.org
+ String command = "bot" + _token + "/getMe";
+ String response =
+ sendGetToTelegram(command); // receive reply from telegram.org
DynamicJsonBuffer jsonBuffer;
- JsonObject& root = jsonBuffer.parseObject(response);
+ JsonObject &root = jsonBuffer.parseObject(response);
+
+ closeClient();
- if(root.success()) {
+ if (root.success()) {
if (root.containsKey("result")) {
String _name = root["result"]["first_name"];
String _username = root["result"]["username"];
@@ -283,101 +335,200 @@ bool UniversalTelegramBot::getMe() {
}
/***************************************************************
-* GetUpdates - function to receive messages from telegram *
-* (Argument to pass: the last+1 message to read) *
-* Returns the number of new messages *
-***************************************************************/
-int UniversalTelegramBot::getUpdates(long offset) {
-
- if (_debug) Serial.println("GET Update Messages");
-
- String command = "bot"+_token+"/getUpdates?offset="+String(offset)+"&limit="+String(HANDLE_MESSAGES);
- String response = sendGetToTelegram(command); //receive reply from telegram.org
-
- if (response != "") {
- if (_debug) {
- Serial.print("incoming message length");
+ * GetUpdates - function to receive messages from telegram *
+ * (Argument to pass: the last+1 message to read) *
+ * Returns the number of new messages *
+ ***************************************************************/
+int UniversalTelegramBot::getUpdates(long offset) {
+
+ if (_debug)
+ Serial.println(F("GET Update Messages"));
+
+ String command = "bot" + _token + "/getUpdates?offset=" + String(offset) +
+ "&limit=" + String(HANDLE_MESSAGES);
+ if (longPoll > 0) {
+ command = command + "&timeout=" + String(longPoll);
+ }
+ String response =
+ sendGetToTelegram(command); // receive reply from telegram.org
+
+ if (response == "") {
+ if (_debug)
+ Serial.println(F("Received empty string in response!"));
+ // close the client as there's nothing to do with an empty string
+ closeClient();
+ return 0;
+ } else {
+ if (_debug) {
+ Serial.print(F("incoming message length"));
Serial.println(response.length());
- Serial.println("Creating DynamicJsonBuffer");
+ Serial.println(F("Creating DynamicJsonBuffer"));
}
// Parse response into Json object
DynamicJsonBuffer jsonBuffer;
- JsonObject& root = jsonBuffer.parseObject(response);
+ JsonObject &root = jsonBuffer.parseObject(response);
if (root.success()) {
// root.printTo(Serial);
- if (_debug) Serial.println();
+ if (_debug)
+ Serial.println();
if (root.containsKey("result")) {
int resultArrayLength = root["result"].size();
if (resultArrayLength > 0) {
int newMessageIndex = 0;
- for (int i=0; i < resultArrayLength; i++) {
- JsonObject& message = root["result"][i]["message"];
- int update_id = root["result"][i]["update_id"];
- if (last_message_received != update_id) {
- last_message_received = update_id;
- String text = message["text"];
- String date = message["date"];
- String chat_id = message["chat"]["id"];
- String from_id = message["from"]["id"];
- String from_name = message["from"]["first_name"];
-
- messages[newMessageIndex].update_id = update_id;
- messages[newMessageIndex].text = text;
- messages[newMessageIndex].date = date;
- messages[newMessageIndex].chat_id = chat_id;
- messages[newMessageIndex].from_id = from_id;
- messages[newMessageIndex].from_name = from_name;
-
+ // Step through all results
+ for (int i = 0; i < resultArrayLength; i++) {
+ JsonObject &result = root["result"][i];
+ if (processResult(result, newMessageIndex)) {
newMessageIndex++;
}
}
+ // We will keep the client open because there may be a response to be
+ // given
return newMessageIndex;
} else {
- if (_debug) Serial.println("no new messages");
+ if (_debug)
+ Serial.println(F("no new messages"));
}
} else {
- if (_debug) Serial.println("Response contained no 'result'");
+ if (_debug)
+ Serial.println(F("Response contained no 'result'"));
+ }
+ } else { // Parsing failed
+ if (response.length() <
+ 2) { // Too short a message. Maybe connection issue
+ if (_debug)
+ Serial.println(F("Parsing error: Message too short"));
+ } else {
+ // Buffer may not be big enough, increase buffer or reduce max number of
+ // messages
+ if (_debug)
+ Serial.println(F("Failed to parse update, the message could be too "
+ "big for the buffer"));
}
- } else {
- // Buffer may not be big enough, increase buffer or reduce max number of messages
- if (_debug) Serial.println("Failed to parse update, the message could be too big for the buffer");
}
-
+ // Close the client as no response is to be given
+ closeClient();
return 0;
}
}
+bool UniversalTelegramBot::processResult(JsonObject &result, int messageIndex) {
+ int update_id = result["update_id"];
+ // Check have we already dealt with this message (this shouldn't happen!)
+ if (last_message_received != update_id) {
+ last_message_received = update_id;
+ messages[messageIndex].update_id = update_id;
+
+ messages[messageIndex].text = F("");
+ messages[messageIndex].from_id = F("");
+ messages[messageIndex].from_name = F("");
+ messages[messageIndex].longitude = 0;
+ messages[messageIndex].latitude = 0;
+
+ if (result.containsKey("message")) {
+ JsonObject &message = result["message"];
+ messages[messageIndex].type = F("message");
+ messages[messageIndex].from_id = message["from"]["id"].as();
+ messages[messageIndex].from_name =
+ message["from"]["first_name"].as();
+
+ messages[messageIndex].date = message["date"].as();
+ messages[messageIndex].chat_id = message["chat"]["id"].as();
+ messages[messageIndex].chat_title = message["chat"]["title"].as();
+
+ if (message.containsKey("text")) {
+ messages[messageIndex].text = message["text"].as();
+
+ } else if (message.containsKey("location")) {
+ messages[messageIndex].longitude =
+ message["location"]["longitude"].as();
+ messages[messageIndex].latitude =
+ message["location"]["latitude"].as();
+ }
+ } else if (result.containsKey("channel_post")) {
+ JsonObject &message = result["channel_post"];
+ messages[messageIndex].type = F("channel_post");
+
+ messages[messageIndex].text = message["text"].as();
+ messages[messageIndex].date = message["date"].as();
+ messages[messageIndex].chat_id = message["chat"]["id"].as();
+ messages[messageIndex].chat_title = message["chat"]["title"].as();
+
+ } else if (result.containsKey("callback_query")) {
+ JsonObject &message = result["callback_query"];
+ messages[messageIndex].type = F("callback_query");
+ messages[messageIndex].from_id = message["from"]["id"].as();
+ messages[messageIndex].from_name =
+ message["from"]["first_name"].as();
+
+ messages[messageIndex].text = message["data"].as();
+ messages[messageIndex].date = message["date"].as();
+ messages[messageIndex].chat_id =
+ message["message"]["chat"]["id"].as();
+ messages[messageIndex].chat_title = F("");
+ } else if (result.containsKey("edited_message")) {
+ JsonObject &message = result["edited_message"];
+ messages[messageIndex].type = F("edited_message");
+ messages[messageIndex].from_id = message["from"]["id"].as();
+ messages[messageIndex].from_name =
+ message["from"]["first_name"].as();
+
+ messages[messageIndex].date = message["date"].as();
+ messages[messageIndex].chat_id = message["chat"]["id"].as();
+ messages[messageIndex].chat_title = message["chat"]["title"].as();
+
+ if (message.containsKey("text")) {
+ messages[messageIndex].text = message["text"].as();
+
+ } else if (message.containsKey("location")) {
+ messages[messageIndex].longitude =
+ message["location"]["longitude"].as();
+ messages[messageIndex].latitude =
+ message["location"]["latitude"].as();
+ }
+ }
+
+ return true;
+ }
+ return false;
+}
+
/***********************************************************************
-* SendMessage - function to send message to telegram *
-* (Arguments to pass: chat_id, text to transmit and markup(optional)) *
-***********************************************************************/
-bool UniversalTelegramBot::sendSimpleMessage(String chat_id, String text, String parse_mode) {
+ * SendMessage - function to send message to telegram *
+ * (Arguments to pass: chat_id, text to transmit and markup(optional)) *
+ ***********************************************************************/
+bool UniversalTelegramBot::sendSimpleMessage(String chat_id, String text,
+ String parse_mode) {
bool sent = false;
- if (_debug) Serial.println("SEND Simple Message");
+ if (_debug)
+ Serial.println(F("SEND Simple Message"));
long sttime = millis();
- if (text!="") {
- while (millis() < sttime+8000) { // loop for a while to send the message
- String command="bot"+_token+"/sendMessage?chat_id="+chat_id+"&text="+text+"&parse_mode="+parse_mode;
+ if (text != "") {
+ while (millis() < sttime + 8000) { // loop for a while to send the message
+ String command = "bot" + _token + "/sendMessage?chat_id=" + chat_id +
+ "&text=" + text + "&parse_mode=" + parse_mode;
String response = sendGetToTelegram(command);
- if (_debug) Serial.println(response);
+ if (_debug)
+ Serial.println(response);
sent = checkForOkResponse(response);
if (sent) {
break;
}
}
}
-
+ closeClient();
return sent;
}
-bool UniversalTelegramBot::sendMessage(String chat_id, String text, String parse_mode) {
+bool UniversalTelegramBot::sendMessage(String chat_id, String text,
+ String parse_mode) {
DynamicJsonBuffer jsonBuffer;
- JsonObject& payload = jsonBuffer.createObject();
+ JsonObject &payload = jsonBuffer.createObject();
payload["chat_id"] = chat_id;
payload["text"] = text;
@@ -389,10 +540,12 @@ bool UniversalTelegramBot::sendMessage(String chat_id, String text, String parse
return sendPostMessage(payload);
}
-bool UniversalTelegramBot::sendMessageWithReplyKeyboard(String chat_id, String text, String parse_mode, String keyboard, bool resize, bool oneTime, bool selective) {
+bool UniversalTelegramBot::sendMessageWithReplyKeyboard(
+ String chat_id, String text, String parse_mode, String keyboard,
+ bool resize, bool oneTime, bool selective) {
DynamicJsonBuffer jsonBuffer;
- JsonObject& payload = jsonBuffer.createObject();
+ JsonObject &payload = jsonBuffer.createObject();
payload["chat_id"] = chat_id;
payload["text"] = text;
@@ -401,7 +554,7 @@ bool UniversalTelegramBot::sendMessageWithReplyKeyboard(String chat_id, String t
payload["parse_mode"] = parse_mode;
}
- JsonObject& replyMarkup = payload.createNestedObject("reply_markup");
+ JsonObject &replyMarkup = payload.createNestedObject("reply_markup");
// Reply keyboard is an array of arrays.
// Outer array represents rows
@@ -411,7 +564,8 @@ bool UniversalTelegramBot::sendMessageWithReplyKeyboard(String chat_id, String t
DynamicJsonBuffer keyboardBuffer;
replyMarkup["keyboard"] = keyboardBuffer.parseArray(keyboard);
- //Telegram defaults these values to false, so to decrease the size of the payload we will only send them if needed
+ // Telegram defaults these values to false, so to decrease the size of the
+ // payload we will only send them if needed
if (resize) {
replyMarkup["resize_keyboard"] = resize;
}
@@ -427,10 +581,13 @@ bool UniversalTelegramBot::sendMessageWithReplyKeyboard(String chat_id, String t
return sendPostMessage(payload);
}
-bool UniversalTelegramBot::sendMessageWithInlineKeyboard(String chat_id, String text, String parse_mode, String keyboard) {
+bool UniversalTelegramBot::sendMessageWithInlineKeyboard(String chat_id,
+ String text,
+ String parse_mode,
+ String keyboard) {
DynamicJsonBuffer jsonBuffer;
- JsonObject& payload = jsonBuffer.createObject();
+ JsonObject &payload = jsonBuffer.createObject();
payload["chat_id"] = chat_id;
payload["text"] = text;
@@ -439,7 +596,7 @@ bool UniversalTelegramBot::sendMessageWithInlineKeyboard(String chat_id, String
payload["parse_mode"] = parse_mode;
}
- JsonObject& replyMarkup = payload.createNestedObject("reply_markup");
+ JsonObject &replyMarkup = payload.createNestedObject("reply_markup");
DynamicJsonBuffer keyboardBuffer;
replyMarkup["inline_keyboard"] = keyboardBuffer.parseArray(keyboard);
@@ -448,20 +605,22 @@ bool UniversalTelegramBot::sendMessageWithInlineKeyboard(String chat_id, String
}
/***********************************************************************
-* SendPostMessage - function to send message to telegram *
-* (Arguments to pass: chat_id, text to transmit and markup(optional)) *
-***********************************************************************/
-bool UniversalTelegramBot::sendPostMessage(JsonObject& payload) {
+ * SendPostMessage - function to send message to telegram *
+ * (Arguments to pass: chat_id, text to transmit and markup(optional)) *
+ ***********************************************************************/
+bool UniversalTelegramBot::sendPostMessage(JsonObject &payload) {
- bool sent=false;
- if (_debug) Serial.println("SEND Post Message");
- long sttime=millis();
+ bool sent = false;
+ if (_debug)
+ Serial.println(F("SEND Post Message"));
+ long sttime = millis();
if (payload.containsKey("text")) {
- while (millis() < sttime+8000) { // loop for a while to send the message
- String command = "bot"+_token+"/sendMessage";
+ while (millis() < sttime + 8000) { // loop for a while to send the message
+ String command = "bot" + _token + "/sendMessage";
String response = sendPostToTelegram(command, payload);
- if (_debug) Serial.println(response);
+ if (_debug)
+ Serial.println(response);
sent = checkForOkResponse(response);
if (sent) {
break;
@@ -469,21 +628,24 @@ bool UniversalTelegramBot::sendPostMessage(JsonObject& payload) {
}
}
+ closeClient();
return sent;
}
-String UniversalTelegramBot::sendPostPhoto(JsonObject& payload) {
+String UniversalTelegramBot::sendPostPhoto(JsonObject &payload) {
- bool sent=false;
+ bool sent = false;
String response = "";
- if (_debug) Serial.println("SEND Post Photo");
- long sttime=millis();
+ if (_debug)
+ Serial.println(F("SEND Post Photo"));
+ long sttime = millis();
if (payload.containsKey("photo")) {
- while (millis() < sttime+8000) { // loop for a while to send the message
- String command = "bot"+_token+"/sendPhoto";
+ while (millis() < sttime + 8000) { // loop for a while to send the message
+ String command = "bot" + _token + "/sendPhoto";
response = sendPostToTelegram(command, payload);
- if (_debug) Serial.println(response);
+ if (_debug)
+ Serial.println(response);
sent = checkForOkResponse(response);
if (sent) {
break;
@@ -491,28 +653,36 @@ String UniversalTelegramBot::sendPostPhoto(JsonObject& payload) {
}
}
+ closeClient();
return response;
}
-String UniversalTelegramBot::sendPhotoByBinary(String chat_id, String contentType, int fileSize,
+String UniversalTelegramBot::sendPhotoByBinary(
+ String chat_id, String contentType, int fileSize,
MoreDataAvailable moreDataAvailableCallback,
GetNextByte getNextByteCallback) {
- if (_debug) Serial.println("SEND Photo");
+ if (_debug)
+ Serial.println("SEND Photo");
- String response = sendMultipartFormDataToTelegram("sendPhoto", "photo", "img.jpg",
- contentType, chat_id, fileSize,
- moreDataAvailableCallback, getNextByteCallback);
+ String response = sendMultipartFormDataToTelegram(
+ "sendPhoto", "photo", "img.jpg", contentType, chat_id, fileSize,
+ moreDataAvailableCallback, getNextByteCallback);
- if (_debug) Serial.println(response);
+ if (_debug)
+ Serial.println(response);
return response;
}
-String UniversalTelegramBot::sendPhoto(String chat_id, String photo, String caption, bool disable_notification, int reply_to_message_id, String keyboard) {
+String UniversalTelegramBot::sendPhoto(String chat_id, String photo,
+ String caption,
+ bool disable_notification,
+ int reply_to_message_id,
+ String keyboard) {
DynamicJsonBuffer jsonBuffer;
- JsonObject& payload = jsonBuffer.createObject();
+ JsonObject &payload = jsonBuffer.createObject();
payload["chat_id"] = chat_id;
payload["photo"] = photo;
@@ -530,7 +700,7 @@ String UniversalTelegramBot::sendPhoto(String chat_id, String photo, String capt
}
if (keyboard) {
- JsonObject& replyMarkup = payload.createNestedObject("reply_markup");
+ JsonObject &replyMarkup = payload.createNestedObject("reply_markup");
DynamicJsonBuffer keyboardBuffer;
replyMarkup["keyboard"] = keyboardBuffer.parseArray(keyboard);
@@ -542,8 +712,9 @@ String UniversalTelegramBot::sendPhoto(String chat_id, String photo, String capt
bool UniversalTelegramBot::checkForOkResponse(String response) {
int responseLength = response.length();
- for (int m=5; m < responseLength+1; m++) {
- if (response.substring(m-10,m)=="{\"ok\":true") { //Chek if message has been properly sent
+ for (int m = 5; m < responseLength + 1; m++) {
+ if (response.substring(m - 10, m) ==
+ "{\"ok\":true") { // Chek if message has been properly sent
return true;
}
}
@@ -551,18 +722,21 @@ bool UniversalTelegramBot::checkForOkResponse(String response) {
return false;
}
-bool UniversalTelegramBot::sendChatAction(String chat_id, String text) {
+bool UniversalTelegramBot::sendChatAction(String chat_id, String text) {
bool sent = false;
- if (_debug) Serial.println("SEND Chat Action Message");
+ if (_debug)
+ Serial.println(F("SEND Chat Action Message"));
long sttime = millis();
if (text != "") {
- while (millis() < sttime+8000) { // loop for a while to send the message
- String command="bot"+_token+"/sendChatAction?chat_id="+chat_id+"&action="+text;
+ while (millis() < sttime + 8000) { // loop for a while to send the message
+ String command = "bot" + _token + "/sendChatAction?chat_id=" + chat_id +
+ "&action=" + text;
String response = sendGetToTelegram(command);
- if (_debug) Serial.println(response);
+ if (_debug)
+ Serial.println(response);
sent = checkForOkResponse(response);
if (sent) {
@@ -571,5 +745,15 @@ bool UniversalTelegramBot::sendChatAction(String chat_id, String text) {
}
}
+ closeClient();
return sent;
}
+
+void UniversalTelegramBot::closeClient() {
+ if (client->connected()) {
+ if (_debug) {
+ Serial.println(F("Closing client"));
+ }
+ client->stop();
+ }
+}
diff --git a/src/UniversalTelegramBot.h b/src/UniversalTelegramBot.h
index d80a903..5f7ace5 100644
--- a/src/UniversalTelegramBot.h
+++ b/src/UniversalTelegramBot.h
@@ -1,9 +1,8 @@
/*
-Copyright (c) 2015 Giancarlo Bacchio. All right reserved.
+Copyright (c) 2018 Brian Lough. All right reserved.
-TelegramBot - Library to create your own Telegram Bot using
-ESP8266 on Arduino IDE.
-Ref. Library at https:github/esp8266/Arduino
+UniversalTelegramBot - Library to create your own Telegram Bot using
+ESP8266 or ESP32 on Arduino IDE.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
@@ -34,60 +33,70 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
typedef bool (*MoreDataAvailable)();
typedef byte (*GetNextByte)();
-struct telegramMessage{
+struct telegramMessage {
String text;
String chat_id;
+ String chat_title;
String from_id;
String from_name;
String date;
+ String type;
+ float longitude;
+ float latitude;
int update_id;
};
-class UniversalTelegramBot
-{
- public:
- UniversalTelegramBot (String token, Client &client);
- String sendGetToTelegram(String command);
- String sendPostToTelegram(String command, JsonObject& payload);
- String sendMultipartFormDataToTelegram(String command, String binaryProperyName,
- String fileName, String contentType,
- String chat_id, int fileSize,
- MoreDataAvailable moreDataAvailableCallback,
- GetNextByte getNextByteCallback);
-
- bool getMe();
-
- bool sendSimpleMessage(String chat_id, String text, String parse_mode);
- bool sendMessage(String chat_id, String text, String parse_mode = "");
- bool sendMessageWithReplyKeyboard(String chat_id, String text,
- String parse_mode, String keyboard, bool resize = false,
- bool oneTime = false, bool selective = false);
- bool sendMessageWithInlineKeyboard(String chat_id, String text,
- String parse_mode, String keyboard);
-
- bool sendChatAction(String chat_id, String text);
-
- bool sendPostMessage(JsonObject& payload);
- String sendPostPhoto(JsonObject& payload);
- String sendPhotoByBinary(String chat_id, String contentType, int fileSize,
- MoreDataAvailable moreDataAvailableCallback,
- GetNextByte getNextByteCallback);
- String sendPhoto(String chat_id, String photo, String caption = "",
- bool disable_notification = false, int reply_to_message_id = 0, String keyboard = "");
-
- int getUpdates(long offset);
- bool checkForOkResponse(String response);
- telegramMessage messages[HANDLE_MESSAGES];
- long last_message_received;
- String name;
- String userName;
-
- private:
- //JsonObject * parseUpdates(String response);
- String _token;
- Client *client;
- const int maxMessageLength = 1300;
- bool _debug = false;
+class UniversalTelegramBot {
+public:
+ UniversalTelegramBot(String token, Client &client);
+ String sendGetToTelegram(String command);
+ String sendPostToTelegram(String command, JsonObject &payload);
+ String
+ sendMultipartFormDataToTelegram(String command, String binaryProperyName,
+ String fileName, String contentType,
+ String chat_id, int fileSize,
+ MoreDataAvailable moreDataAvailableCallback,
+ GetNextByte getNextByteCallback);
+
+ bool getMe();
+
+ bool sendSimpleMessage(String chat_id, String text, String parse_mode);
+ bool sendMessage(String chat_id, String text, String parse_mode = "");
+ bool sendMessageWithReplyKeyboard(String chat_id, String text,
+ String parse_mode, String keyboard,
+ bool resize = false, bool oneTime = false,
+ bool selective = false);
+ bool sendMessageWithInlineKeyboard(String chat_id, String text,
+ String parse_mode, String keyboard);
+
+ bool sendChatAction(String chat_id, String text);
+
+ bool sendPostMessage(JsonObject &payload);
+ String sendPostPhoto(JsonObject &payload);
+ String sendPhotoByBinary(String chat_id, String contentType, int fileSize,
+ MoreDataAvailable moreDataAvailableCallback,
+ GetNextByte getNextByteCallback);
+ String sendPhoto(String chat_id, String photo, String caption = "",
+ bool disable_notification = false,
+ int reply_to_message_id = 0, String keyboard = "");
+
+ int getUpdates(long offset);
+ bool checkForOkResponse(String response);
+ telegramMessage messages[HANDLE_MESSAGES];
+ long last_message_received;
+ String name;
+ String userName;
+ int longPoll = 0;
+ bool _debug = false;
+ int waitForResponse = 1500;
+
+private:
+ // JsonObject * parseUpdates(String response);
+ String _token;
+ Client *client;
+ bool processResult(JsonObject &result, int messageIndex);
+ void closeClient();
+ const int maxMessageLength = 1300;
};
#endif