Skip to content

Commit bda42a1

Browse files
committed
Changing send message to a post and adding a sendSimpleMessage. Also improving examples as per #5
1 parent 42fa4dd commit bda42a1

File tree

12 files changed

+127
-354
lines changed

12 files changed

+127
-354
lines changed

examples/101/CustomKeyboard/CustomKeyboard.ino

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -52,45 +52,26 @@ void handleNewMessages(int numNewMessages) {
5252
}
5353
}
5454
if (text == "/options") {
55-
StaticJsonBuffer<500> jsonBuffer;
56-
JsonObject& payload = jsonBuffer.createObject();
57-
payload["chat_id"] = chat_id;
58-
payload["text"] = "Choose from one of the following options";
59-
JsonObject& replyMarkup = payload.createNestedObject("reply_markup");
60-
61-
// Reply keyboard is an array of arrays.
62-
// Outer array represents rows
63-
// Inner arrays represents columns
64-
// This example "ledon" and "ledoff" are two buttons on the top row
65-
// and "status is a single button on the next row"
66-
StaticJsonBuffer<200> keyboardBuffer;
67-
char keyboardJson[] = "[[\"/ledon\", \"/ledoff\"],[\"/status\"]]";
68-
replyMarkup["keyboard"] = keyboardBuffer.parseArray(keyboardJson);
69-
replyMarkup["resize_keyboard"] = true;
70-
71-
bot.sendPostMessage(payload);
55+
String keyboardJson = "[[\"/ledon\", \"/ledoff\"],[\"/status\"]]";
56+
bot.sendMessageWithReplyKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson, true);
7257
}
7358

7459
if (text == "/start") {
75-
String wellcome = "The custom keyboard example for ESP8266TelegramBot";
76-
String wellcome1 = "/ledon : to switch the Led ON";
77-
String wellcome2 = "/ledoff : to switch the Led OFF";
78-
String wellcome3 = "/status : Returns current status of LED";
79-
String wellcome4 = "/options : returns the custom keyboard";
80-
bot.sendMessage(chat_id, wellcome, "");
81-
bot.sendMessage(chat_id, wellcome1, "");
82-
bot.sendMessage(chat_id, wellcome2, "");
83-
bot.sendMessage(chat_id, wellcome3, "");
84-
bot.sendMessage(chat_id, wellcome4, "");
60+
String welcome = "Wellcome from FlashLedBot, your personal Bot on ESP8266 board \n";
61+
welcome = welcome + "/ledon : to switch the Led ON \n";
62+
welcome = welcome + "/ledoff : to switch the Led OFF \n";
63+
welcome = welcome + "/status : Returns current status of LED \n";
64+
welcome = welcome + "/options : returns the custom keyboard \n";
65+
bot.sendMessage(chat_id, welcome, "Markdown");
8566
}
8667
}
8768
}
8869

8970

9071
void setup() {
9172
Serial.begin(115200);
92-
delay(3000);
9373

74+
delay(100);
9475
// attempt to connect to Wifi network:
9576
Serial.print("Connecting Wifi: ");
9677
Serial.println(ssid);
@@ -113,12 +94,13 @@ void setup() {
11394

11495

11596
void loop() {
116-
if (millis() > Bot_lasttime + Bot_mtbs) {
11797

98+
if (millis() > Bot_lasttime + Bot_mtbs) {
11899
int numNewMessages = bot.getUpdates(bot.last_message_recived + 1);
119-
if(numNewMessages) {
100+
while(numNewMessages) {
120101
Serial.println("got response");
121102
handleNewMessages(numNewMessages);
103+
numNewMessages = bot.getUpdates(bot.last_message_recived + 1);
122104
}
123105
Bot_lasttime = millis();
124106
}

examples/101/EchoBot/EchoBot.ino

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,13 @@ int Bot_mtbs = 1000; //mean time between scan messages
2828
long Bot_lasttime; //last time messages' scan has been done
2929

3030

31-
/********************************************
32-
* EchoMessages - function to Echo messages *
33-
********************************************/
34-
void Bot_EchoMessages(int numNewMessages) {
35-
for(int i=0; i<numNewMessages; i++) {
36-
bot.sendMessage(bot.messages[i].chat_id, bot.messages[i].text, "");
37-
}
38-
}
39-
40-
4131
void setup() {
4232

4333
Serial.begin(115200);
44-
delay(3000);
4534

46-
// attempt to connect to Wifi network:
35+
delay(100);
36+
37+
// Attempt to connect to Wifi network:
4738
Serial.print("Connecting Wifi: ");
4839
Serial.println(ssid);
4940
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
@@ -59,15 +50,16 @@ void setup() {
5950

6051
}
6152

62-
63-
6453
void loop() {
6554

6655
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) {
56+
int numNewMessages = bot.getUpdates(bot.last_message_recived + 1);
57+
while(numNewMessages) {
6958
Serial.println("got response");
70-
Bot_EchoMessages(numNewMessages); // reply to message with Echo
59+
for(int i=0; i<numNewMessages; i++) {
60+
bot.sendMessage(bot.messages[i].chat_id, bot.messages[i].text, "");
61+
}
62+
numNewMessages = bot.getUpdates(bot.last_message_recived + 1);
7163
}
7264
Bot_lasttime = millis();
7365
}

examples/101/EchoBotWithPost/EchoBotWithPost.ino

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

examples/101/EchoBotWithPost/README.md

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

examples/101/FlashledBot/FlashledBot.ino

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,20 @@ void handleNewMessages(int numNewMessages) {
5454
}
5555
}
5656
if (text == "/start") {
57-
String wellcome = "Wellcome from FlashLedBot, your personal Bot on ESP8266 board";
58-
String wellcome1 = "/ledon : to switch the Led ON";
59-
String wellcome2 = "/ledoff : to switch the Led OFF";
60-
String wellcome3 = "/status : Returns current status of LED";
61-
bot.sendMessage(chat_id, wellcome, "");
62-
bot.sendMessage(chat_id, wellcome1, "");
63-
bot.sendMessage(chat_id, wellcome2, "");
64-
bot.sendMessage(chat_id, wellcome3, "");
65-
Start = true;
57+
String welcome = "Wellcome from FlashLedBot, your personal Bot on ESP8266 board \n";
58+
welcome = welcome + "/ledon : to switch the Led ON \n";
59+
welcome = welcome + "/ledoff : to switch the Led OFF \n";
60+
welcome = welcome + "/status : Returns current status of LED \n";
61+
bot.sendMessage(chat_id, welcome, "Markdown");
6662
}
6763
}
6864
}
6965

7066

7167
void setup() {
7268
Serial.begin(115200);
73-
delay(3000);
69+
70+
delay(100);
7471

7572
// attempt to connect to Wifi network:
7673
Serial.print("Connecting Wifi: ");
@@ -87,19 +84,18 @@ void setup() {
8784

8885
pinMode(ledPin, OUTPUT); // initialize digital ledPin as an output.
8986
delay(10);
90-
digitalWrite(ledPin, HIGH); //initilase pin as off
87+
digitalWrite(ledPin, LOW); //initilase pin as off
9188

9289
}
9390

94-
95-
9691
void loop() {
97-
if (millis() > Bot_lasttime + Bot_mtbs) {
9892

99-
int numNewMessages = bot.getUpdates(bot.last_message_recived + 1); // launch API GetUpdates up to xxx message
100-
if(numNewMessages) {
93+
if (millis() > Bot_lasttime + Bot_mtbs) {
94+
int numNewMessages = bot.getUpdates(bot.last_message_recived + 1);
95+
while(numNewMessages) {
10196
Serial.println("got response");
102-
handleNewMessages(numNewMessages); // reply to message with Echo
97+
handleNewMessages(numNewMessages);
98+
numNewMessages = bot.getUpdates(bot.last_message_recived + 1);
10399
}
104100
Bot_lasttime = millis();
105101
}

examples/ESP8266/CustomKeyboard/CustomKeyboard.ino

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -52,45 +52,30 @@ void handleNewMessages(int numNewMessages) {
5252
}
5353
}
5454
if (text == "/options") {
55-
StaticJsonBuffer<500> jsonBuffer;
56-
JsonObject& payload = jsonBuffer.createObject();
57-
payload["chat_id"] = chat_id;
58-
payload["text"] = "Choose from one of the following options";
59-
JsonObject& replyMarkup = payload.createNestedObject("reply_markup");
60-
61-
// Reply keyboard is an array of arrays.
62-
// Outer array represents rows
63-
// Inner arrays represents columns
64-
// This example "ledon" and "ledoff" are two buttons on the top row
65-
// and "status is a single button on the next row"
66-
StaticJsonBuffer<200> keyboardBuffer;
67-
char keyboardJson[] = "[[\"/ledon\", \"/ledoff\"],[\"/status\"]]";
68-
replyMarkup["keyboard"] = keyboardBuffer.parseArray(keyboardJson);
69-
replyMarkup["resize_keyboard"] = true;
70-
71-
bot.sendPostMessage(payload);
55+
String keyboardJson = "[[\"/ledon\", \"/ledoff\"],[\"/status\"]]";
56+
bot.sendMessageWithReplyKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson, true);
7257
}
7358

7459
if (text == "/start") {
75-
String wellcome = "The custom keyboard example for ESP8266TelegramBot";
76-
String wellcome1 = "/ledon : to switch the Led ON";
77-
String wellcome2 = "/ledoff : to switch the Led OFF";
78-
String wellcome3 = "/status : Returns current status of LED";
79-
String wellcome4 = "/options : returns the custom keyboard";
80-
bot.sendMessage(chat_id, wellcome, "");
81-
bot.sendMessage(chat_id, wellcome1, "");
82-
bot.sendMessage(chat_id, wellcome2, "");
83-
bot.sendMessage(chat_id, wellcome3, "");
84-
bot.sendMessage(chat_id, wellcome4, "");
60+
String welcome = "Wellcome from FlashLedBot, your personal Bot on ESP8266 board \n";
61+
welcome = welcome + "/ledon : to switch the Led ON \n";
62+
welcome = welcome + "/ledoff : to switch the Led OFF \n";
63+
welcome = welcome + "/status : Returns current status of LED \n";
64+
welcome = welcome + "/options : returns the custom keyboard \n";
65+
bot.sendMessage(chat_id, welcome, "Markdown");
8566
}
8667
}
8768
}
8869

8970

9071
void setup() {
9172
Serial.begin(115200);
92-
delay(3000);
9373

74+
// Set WiFi to station mode and disconnect from an AP if it was Previously
75+
// connected
76+
WiFi.mode(WIFI_STA);
77+
WiFi.disconnect();
78+
delay(100);
9479
// attempt to connect to Wifi network:
9580
Serial.print("Connecting Wifi: ");
9681
Serial.println(ssid);
@@ -113,12 +98,13 @@ void setup() {
11398

11499

115100
void loop() {
116-
if (millis() > Bot_lasttime + Bot_mtbs) {
117101

102+
if (millis() > Bot_lasttime + Bot_mtbs) {
118103
int numNewMessages = bot.getUpdates(bot.last_message_recived + 1);
119-
if(numNewMessages) {
104+
while(numNewMessages) {
120105
Serial.println("got response");
121106
handleNewMessages(numNewMessages);
107+
numNewMessages = bot.getUpdates(bot.last_message_recived + 1);
122108
}
123109
Bot_lasttime = millis();
124110
}

examples/ESP8266/EchoBot/EchoBot.ino

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,17 @@ int Bot_mtbs = 1000; //mean time between scan messages
2727
long Bot_lasttime; //last time messages' scan has been done
2828

2929

30-
/********************************************
31-
* EchoMessages - function to Echo messages *
32-
********************************************/
33-
void Bot_EchoMessages(int numNewMessages) {
34-
for(int i=0; i<numNewMessages; i++) {
35-
bot.sendMessage(bot.messages[i].chat_id, bot.messages[i].text, "");
36-
}
37-
}
38-
39-
4030
void setup() {
4131

4232
Serial.begin(115200);
43-
delay(3000);
4433

45-
// attempt to connect to Wifi network:
34+
// Set WiFi to station mode and disconnect from an AP if it was Previously
35+
// connected
36+
WiFi.mode(WIFI_STA);
37+
WiFi.disconnect();
38+
delay(100);
39+
40+
// Attempt to connect to Wifi network:
4641
Serial.print("Connecting Wifi: ");
4742
Serial.println(ssid);
4843
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
@@ -58,15 +53,16 @@ void setup() {
5853

5954
}
6055

61-
62-
6356
void loop() {
6457

6558
if (millis() > Bot_lasttime + Bot_mtbs) {
66-
int numNewMessages = bot.getUpdates(bot.last_message_recived + 1); // launch API GetUpdates up to xxx message
67-
if(numNewMessages) {
59+
int numNewMessages = bot.getUpdates(bot.last_message_recived + 1);
60+
while(numNewMessages) {
6861
Serial.println("got response");
69-
Bot_EchoMessages(numNewMessages); // reply to message with Echo
62+
for(int i=0; i<numNewMessages; i++) {
63+
bot.sendMessage(bot.messages[i].chat_id, bot.messages[i].text, "");
64+
}
65+
numNewMessages = bot.getUpdates(bot.last_message_recived + 1);
7066
}
7167
Bot_lasttime = millis();
7268
}

0 commit comments

Comments
 (0)