1
-
2
1
/*
3
2
Copyright (c) 2015 Giancarlo Bacchio. All right reserved.
4
3
@@ -21,7 +20,6 @@ License along with this library; if not, write to the Free Software
21
20
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
21
*/
23
22
24
-
25
23
#include " UniversalTelegramBot.h"
26
24
27
25
UniversalTelegramBot::UniversalTelegramBot (String token, Client &client) {
@@ -30,9 +28,10 @@ UniversalTelegramBot::UniversalTelegramBot(String token, Client &client) {
30
28
}
31
29
32
30
String UniversalTelegramBot::sendGetToTelegram (String command) {
33
- String mess= " " ;
31
+ String mess = " " ;
34
32
long now;
35
33
bool avail;
34
+
36
35
// Connect with api.telegram.org
37
36
if (client->connect (HOST, SSL_PORT)) {
38
37
if (_debug) Serial.println (" .... connected to server" );
@@ -42,7 +41,7 @@ String UniversalTelegramBot::sendGetToTelegram(String command) {
42
41
client->println (" GET /" +command);
43
42
now=millis ();
44
43
avail=false ;
45
- while (millis ()- now<1500 ) {
44
+ while (millis () - now<1500 ) {
46
45
while (client->available ()) {
47
46
char c = client->read ();
48
47
// Serial.write(c);
@@ -53,15 +52,16 @@ String UniversalTelegramBot::sendGetToTelegram(String command) {
53
52
avail=true ;
54
53
}
55
54
if (avail) {
56
- if (_debug) {
57
- Serial.println ();
58
- Serial.println (mess);
59
- Serial.println ();
60
- }
55
+ if (_debug) {
56
+ Serial.println ();
57
+ Serial.println (mess);
58
+ Serial.println ();
59
+ }
61
60
break ;
62
61
}
63
62
}
64
63
}
64
+
65
65
return mess;
66
66
}
67
67
@@ -70,6 +70,7 @@ String UniversalTelegramBot::sendPostToTelegram(String command, JsonObject& payl
70
70
String response = " " ;
71
71
long now;
72
72
bool responseReceived;
73
+
73
74
// Connect with api.telegram.org
74
75
if (client->connect (HOST, SSL_PORT)) {
75
76
// POST URI
@@ -78,6 +79,7 @@ String UniversalTelegramBot::sendPostToTelegram(String command, JsonObject& payl
78
79
client->print (" Host:" ); client->println (HOST);
79
80
// JSON content type
80
81
client->println (" Content-Type: application/json" );
82
+
81
83
// Content length
82
84
int length = payload.measureLength ();
83
85
client->print (" Content-Length:" ); client->println (length);
@@ -104,11 +106,11 @@ String UniversalTelegramBot::sendPostToTelegram(String command, JsonObject& payl
104
106
responseReceived=true ;
105
107
}
106
108
if (responseReceived) {
107
- if (_debug) {
108
- Serial.println ();
109
- Serial.println (response);
110
- Serial.println ();
111
- }
109
+ if (_debug) {
110
+ Serial.println ();
111
+ Serial.println (response);
112
+ Serial.println ();
113
+ }
112
114
break ;
113
115
}
114
116
}
@@ -117,12 +119,12 @@ String UniversalTelegramBot::sendPostToTelegram(String command, JsonObject& payl
117
119
return response;
118
120
}
119
121
120
-
121
122
bool UniversalTelegramBot::getMe () {
122
- String command= " bot" +_token+" /getMe" ;
123
- String response = sendGetToTelegram (command); // receive reply from telegram.org
123
+ String command = " bot" +_token+" /getMe" ;
124
+ String response = sendGetToTelegram (command); // receive reply from telegram.org
124
125
DynamicJsonBuffer jsonBuffer;
125
126
JsonObject& root = jsonBuffer.parseObject (response);
127
+
126
128
if (root.success ()) {
127
129
if (root.containsKey (" result" )) {
128
130
String _name = root[" result" ][" first_name" ];
@@ -141,39 +143,35 @@ bool UniversalTelegramBot::getMe() {
141
143
* (Argument to pass: the last+1 message to read) *
142
144
* Returns the number of new messages *
143
145
***************************************************************/
144
-
145
- // JsonObject * UniversalTelegramBot::parseUpdates(String response) {
146
- // DynamicJsonBuffer jsonBuffer;
147
- // return *jsonBuffer.parseObject(response);
148
- // }
149
-
150
146
int UniversalTelegramBot::getUpdates (long offset) {
151
147
152
148
if (_debug) Serial.println (" GET Update Messages" );
153
- String command=" bot" +_token+" /getUpdates?offset=" +String (offset)+" &limit=" +String (HANDLE_MESSAGES);
154
- String response = sendGetToTelegram (command); // receive reply from telegram.org
149
+
150
+ String command = " bot" +_token+" /getUpdates?offset=" +String (offset)+" &limit=" +String (HANDLE_MESSAGES);
151
+ String response = sendGetToTelegram (command); // receive reply from telegram.org
152
+
155
153
if (response != " " ) {
156
154
if (_debug) {
157
- Serial.print (" incoming message length" );
158
- Serial.println (response.length ());
159
- Serial.println (" Creating DynamicJsonBuffer" );
155
+ Serial.print (" incoming message length" );
156
+ Serial.println (response.length ());
157
+ Serial.println (" Creating DynamicJsonBuffer" );
160
158
}
161
159
162
160
// Parse response into Json object
163
161
DynamicJsonBuffer jsonBuffer;
164
162
JsonObject& root = jsonBuffer.parseObject (response);
165
163
166
- if (root.success ()) {
164
+ if (root.success ()) {
167
165
// root.printTo(Serial);
168
166
if (_debug) Serial.println ();
169
167
if (root.containsKey (" result" )) {
170
168
int resultArrayLength = root[" result" ].size ();
171
- if (resultArrayLength > 0 ) {
169
+ if (resultArrayLength > 0 ) {
172
170
int newMessageIndex = 0 ;
173
- for (int i=0 ; i < resultArrayLength; i++){
171
+ for (int i=0 ; i < resultArrayLength; i++) {
174
172
JsonObject& message = root[" result" ][i][" message" ];
175
173
int update_id = root[" result" ][i][" update_id" ];
176
- if (last_message_received != update_id) {
174
+ if (last_message_received != update_id) {
177
175
last_message_received = update_id;
178
176
String text = message[" text" ];
179
177
String date = message[" date" ];
@@ -213,16 +211,17 @@ int UniversalTelegramBot::getUpdates(long offset) {
213
211
***********************************************************************/
214
212
bool UniversalTelegramBot::sendSimpleMessage (String chat_id, String text, String parse_mode) {
215
213
216
- bool sent= false ;
214
+ bool sent = false ;
217
215
if (_debug) Serial.println (" SEND Simple Message" );
218
- long sttime=millis ();
216
+ long sttime = millis ();
217
+
219
218
if (text!=" " ) {
220
- while (millis ()< sttime+8000 ) { // loop for a while to send the message
219
+ while (millis () < sttime+8000 ) { // loop for a while to send the message
221
220
String command=" bot" +_token+" /sendMessage?chat_id=" +chat_id+" &text=" +text+" &parse_mode=" +parse_mode;
222
221
String response = sendGetToTelegram (command);
223
222
if (_debug) Serial.println (response);
224
223
sent = checkForOkResponse (response);
225
- if (sent){
224
+ if (sent) {
226
225
break ;
227
226
}
228
227
}
@@ -233,12 +232,13 @@ bool UniversalTelegramBot::sendSimpleMessage(String chat_id, String text, String
233
232
234
233
bool UniversalTelegramBot::sendMessage (String chat_id, String text, String parse_mode) {
235
234
236
-
237
235
DynamicJsonBuffer jsonBuffer;
238
236
JsonObject& payload = jsonBuffer.createObject ();
237
+
239
238
payload[" chat_id" ] = chat_id;
240
239
payload[" text" ] = text;
241
- if (parse_mode != " " ){
240
+
241
+ if (parse_mode != " " ) {
242
242
payload[" parse_mode" ] = parse_mode;
243
243
}
244
244
@@ -247,14 +247,16 @@ bool UniversalTelegramBot::sendMessage(String chat_id, String text, String parse
247
247
248
248
bool UniversalTelegramBot::sendMessageWithReplyKeyboard (String chat_id, String text, String parse_mode, String keyboard, bool resize, bool oneTime, bool selective) {
249
249
250
-
251
250
DynamicJsonBuffer jsonBuffer;
252
251
JsonObject& payload = jsonBuffer.createObject ();
252
+
253
253
payload[" chat_id" ] = chat_id;
254
254
payload[" text" ] = text;
255
- if (parse_mode != " " ){
255
+
256
+ if (parse_mode != " " ) {
256
257
payload[" parse_mode" ] = parse_mode;
257
258
}
259
+
258
260
JsonObject& replyMarkup = payload.createNestedObject (" reply_markup" );
259
261
260
262
// Reply keyboard is an array of arrays.
@@ -266,21 +268,41 @@ bool UniversalTelegramBot::sendMessageWithReplyKeyboard(String chat_id, String t
266
268
replyMarkup[" keyboard" ] = keyboardBuffer.parseArray (keyboard);
267
269
268
270
// Telegram defaults these values to false, so to decrease the size of the payload we will only send them if needed
269
- if (resize){
271
+ if (resize) {
270
272
replyMarkup[" resize_keyboard" ] = resize;
271
273
}
272
274
273
- if (oneTime){
275
+ if (oneTime) {
274
276
replyMarkup[" one_time_keyboard" ] = oneTime;
275
277
}
276
278
277
- if (selective){
279
+ if (selective) {
278
280
replyMarkup[" selective" ] = selective;
279
281
}
280
282
281
283
return sendPostMessage (payload);
282
284
}
283
285
286
+ bool UniversalTelegramBot::sendMessageWithInlineKeyboard (String chat_id, String text, String parse_mode, String keyboard) {
287
+
288
+ DynamicJsonBuffer jsonBuffer;
289
+ JsonObject& payload = jsonBuffer.createObject ();
290
+
291
+ payload[" chat_id" ] = chat_id;
292
+ payload[" text" ] = text;
293
+
294
+ if (parse_mode != " " ) {
295
+ payload[" parse_mode" ] = parse_mode;
296
+ }
297
+
298
+ JsonObject& replyMarkup = payload.createNestedObject (" reply_markup" );
299
+
300
+ DynamicJsonBuffer keyboardBuffer;
301
+ replyMarkup[" inline_keyboard" ] = keyboardBuffer.parseArray (keyboard);
302
+
303
+ return sendPostMessage (payload);
304
+ }
305
+
284
306
/* **********************************************************************
285
307
* SendPostMessage - function to send message to telegram *
286
308
* (Arguments to pass: chat_id, text to transmit and markup(optional)) *
@@ -290,13 +312,14 @@ bool UniversalTelegramBot::sendPostMessage(JsonObject& payload) {
290
312
bool sent=false ;
291
313
if (_debug) Serial.println (" SEND Post Message" );
292
314
long sttime=millis ();
315
+
293
316
if (payload.containsKey (" text" )) {
294
- while (millis ()< sttime+8000 ) { // loop for a while to send the message
317
+ while (millis () < sttime+8000 ) { // loop for a while to send the message
295
318
String command = " bot" +_token+" /sendMessage" ;
296
319
String response = sendPostToTelegram (command, payload);
297
320
if (_debug) Serial.println (response);
298
321
sent = checkForOkResponse (response);
299
- if (sent){
322
+ if (sent) {
300
323
break ;
301
324
}
302
325
}
@@ -307,8 +330,9 @@ bool UniversalTelegramBot::sendPostMessage(JsonObject& payload) {
307
330
308
331
bool UniversalTelegramBot::checkForOkResponse (String response) {
309
332
int responseLength = response.length ();
310
- for (int m=5 ; m<responseLength+1 ; m++) {
311
- if (response.substring (m-10 ,m)==" {\" ok\" :true" ) { // Chek if message has been properly sent
333
+
334
+ for (int m=5 ; m < responseLength+1 ; m++) {
335
+ if (response.substring (m-10 ,m)==" {\" ok\" :true" ) { // Chek if message has been properly sent
312
336
return true ;
313
337
}
314
338
}
0 commit comments