1
1
/* ******************************************************************
2
- * An example of how to use a custom reply keyboard. *
2
+ * An example of how to use a custom reply keyboard markup. *
3
3
* *
4
4
* *
5
5
* written by Brian Lough *
6
6
*******************************************************************/
7
-
8
7
#include < ESP8266WiFi.h>
9
8
#include < WiFiClientSecure.h>
10
9
#include < UniversalTelegramBot.h>
11
10
12
-
13
11
// Initialize Wifi connection to the router
14
- char ssid[] = " xxxxxxxxxxxxxxxxxxxxxx" ; // your network SSID (name)
15
- char password[] = " yyyyyyyy" ; // your network key
16
-
17
-
18
-
19
- const int ledPin = 13 ;
12
+ char ssid[] = " xxxxxxx" ; // your network SSID (name)
13
+ char password[] = " yyyyyyyy" ; // your network key
20
14
21
15
// Initialize Telegram BOT
22
16
#define BOTtoken " XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
@@ -26,87 +20,95 @@ UniversalTelegramBot bot(BOTtoken, client);
26
20
27
21
int Bot_mtbs = 1000 ; // mean time between scan messages
28
22
long Bot_lasttime; // last time messages' scan has been done
23
+
24
+ const int ledPin = 13 ;
29
25
int ledStatus = 0 ;
30
26
31
27
void handleNewMessages (int numNewMessages) {
32
28
Serial.println (" handleNewMessages" );
33
29
Serial.println (String (numNewMessages));
34
- for (int i=0 ; i<numNewMessages; i++) {
30
+
31
+ for (int i=0 ; i<numNewMessages; i++) {
35
32
String chat_id = String (bot.messages [i].chat_id );
36
33
String text = bot.messages [i].text ;
34
+ String from_name = bot.messages [i].from_name ;
35
+
37
36
if (text == " /ledon" ) {
38
37
digitalWrite (ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
39
38
ledStatus = 1 ;
40
39
bot.sendMessage (chat_id, " Led is ON" , " " );
41
40
}
41
+
42
42
if (text == " /ledoff" ) {
43
43
ledStatus = 0 ;
44
44
digitalWrite (ledPin, LOW); // turn the LED off (LOW is the voltage level)
45
45
bot.sendMessage (chat_id, " Led is OFF" , " " );
46
46
}
47
+
47
48
if (text == " /status" ) {
48
49
if (ledStatus){
49
50
bot.sendMessage (chat_id, " Led is ON" , " " );
50
51
} else {
51
52
bot.sendMessage (chat_id, " Led is OFF" , " " );
52
53
}
53
54
}
55
+
54
56
if (text == " /options" ) {
55
57
String keyboardJson = " [[\" /ledon\" , \" /ledoff\" ],[\" /status\" ]]" ;
56
58
bot.sendMessageWithReplyKeyboard (chat_id, " Choose from one of the following options" , " " , keyboardJson, true );
57
59
}
58
60
59
61
if (text == " /start" ) {
60
- String welcome = " Welcome from FlashLedBot, your personal Bot on ESP8266\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 " ;
62
+ String welcome = " Welcome to Universal Telegram Bot library, " + from_name + " .\n " ;
63
+ welcome += " This is Reply Keyboard Markup example.\n\n " ;
64
+ welcome += " /ledon : to switch the Led ON\n " ;
65
+ welcome += " /ledon : to switch the Led ON\n " ;
66
+ welcome += " /ledoff : to switch the Led OFF\n " ;
67
+ welcome += " /status : Returns current status of LED\n " ;
68
+ welcome += " /options : returns the custom keyboard\n " ;
65
69
bot.sendMessage (chat_id, welcome, " Markdown" );
66
70
}
67
71
}
68
72
}
69
73
70
-
71
74
void setup () {
72
75
Serial.begin (115200 );
73
76
74
- // Set WiFi to station mode and disconnect from an AP if it was Previously
75
- // connected
77
+ // Set WiFi to station mode and disconnect from an AP if it was Previously connected
76
78
WiFi.mode (WIFI_STA);
77
79
WiFi.disconnect ();
78
80
delay (100 );
81
+
79
82
// attempt to connect to Wifi network:
80
83
Serial.print (" Connecting Wifi: " );
81
84
Serial.println (ssid);
82
85
WiFi.begin (ssid, password);
86
+
83
87
while (WiFi.status () != WL_CONNECTED) {
84
88
Serial.print (" ." );
85
89
delay (500 );
86
90
}
91
+
87
92
Serial.println (" " );
88
93
Serial.println (" WiFi connected" );
89
- Serial.println (" IP address: " );
90
- IPAddress ip = WiFi.localIP ();
91
- Serial.println (ip);
94
+ Serial.print (" IP address: " );
95
+ Serial.println (WiFi.localIP ());
92
96
93
97
pinMode (ledPin, OUTPUT); // initialize digital ledPin as an output.
94
98
delay (10 );
95
99
digitalWrite (ledPin, HIGH); // initialize pin as off
96
-
97
100
}
98
101
99
-
100
-
101
102
void loop () {
102
-
103
103
if (millis () > Bot_lasttime + Bot_mtbs) {
104
104
int numNewMessages = bot.getUpdates (bot.last_message_received + 1 );
105
+
105
106
while (numNewMessages) {
106
107
Serial.println (" got response" );
107
108
handleNewMessages (numNewMessages);
108
109
numNewMessages = bot.getUpdates (bot.last_message_received + 1 );
109
110
}
111
+
110
112
Bot_lasttime = millis ();
111
113
}
112
114
}
0 commit comments