5252 - Добавил ответ на сообщение
5353
5454 v2.2:
55- - оптимизация памяти
56- - добавил notify() - уведомления от сообщений бота
57- - добавил единоразовый показ клавиатуры
55+ - Большая оптимизация памяти
56+ - Добавил notify() - уведомления от сообщений бота
57+ - Добавил единоразовый показ клавиатуры
5858*/
5959
6060/*
@@ -110,6 +110,7 @@ class FastBot {
110110 // инициализация (токен, макс кол-во сообщений на запрос, макс символов, период)
111111 FastBot (String token = " " , uint16_t limit = 10 , uint16_t ovf = 10000 , uint16_t period = 1000 ) {
112112 _token.reserve (50 );
113+ chatIDs.reserve (10 );
113114 _token = token;
114115 _ovf = ovf;
115116 _limit = limit;
@@ -221,10 +222,7 @@ class FastBot {
221222
222223 uint8_t sendMessage (String msg, String id) {
223224 if (!id.length ()) return 5 ;
224- if (id.indexOf (' ,' ) < 0 ) return _sendMessage (msg, id);
225- FB_Parser ids;
226- while (ids.parse (id)) _sendMessage (msg, ids.str );
227- return 6 ;
225+ return _sendMessage (msg, id);
228226 }
229227
230228 // ответить на сообщение
@@ -313,10 +311,7 @@ class FastBot {
313311 uint8_t showMenu (String str, String id, bool once = false ) {
314312 String text (F (" Show Menu" ));
315313 if (!id.length ()) return 5 ;
316- if (id.indexOf (' ,' ) < 0 ) return _showMenu (text, str, id, once);
317- FB_Parser ids;
318- while (ids.parse (id)) _showMenu (text, str, ids.str , once);
319- return 6 ;
314+ return _showMenu (text, str, id, once);
320315 }
321316
322317 // показать меню с текстом
@@ -326,10 +321,7 @@ class FastBot {
326321
327322 uint8_t showMenuText (String text, String str, String id, bool once = false ) {
328323 if (!id.length ()) return 5 ;
329- if (id.indexOf (' ,' ) < 0 ) return _showMenu (text, str, id, once);
330- FB_Parser ids;
331- while (ids.parse (id)) _showMenu (text, str, ids.str , once);
332- return 6 ;
324+ return _showMenu (text, str, id, once);
333325 }
334326
335327 // закрыть меню
@@ -340,10 +332,7 @@ class FastBot {
340332 uint8_t closeMenu (String id) {
341333 String msg (F (" Close Menu" ));
342334 if (!id.length ()) return 5 ;
343- if (id.indexOf (' ,' ) < 0 ) return _closeMenu (msg, id);
344- FB_Parser ids;
345- while (ids.parse (id)) _closeMenu (msg, ids.str );
346- return 6 ;
335+ return _closeMenu (msg, id);
347336 }
348337
349338 // закрыть меню с текстом
@@ -353,10 +342,7 @@ class FastBot {
353342
354343 uint8_t closeMenuText (String msg, String id) {
355344 if (!id.length ()) return 5 ;
356- if (id.indexOf (' ,' ) < 0 ) return _closeMenu (msg, id);
357- FB_Parser ids;
358- while (ids.parse (id)) _closeMenu (msg, ids.str );
359- return 6 ;
345+ return _closeMenu (msg, id);
360346 }
361347
362348 // показать инлайн меню
@@ -366,11 +352,8 @@ class FastBot {
366352
367353 uint8_t inlineMenu (String msg, String str, String id) {
368354 if (!id.length ()) return 5 ;
369- String cb;
370- if (id.indexOf (' ,' ) < 0 ) return _inlineMenu (msg, str, id, cb);
371- FB_Parser ids;
372- while (ids.parse (id)) _inlineMenu (msg, str, ids.str , cb);
373- return 6 ;
355+ String cbck (" " );
356+ return _inlineMenu (msg, str, id, cbck);
374357 }
375358
376359 // показать инлайн меню с коллбэками
@@ -380,24 +363,36 @@ class FastBot {
380363
381364 uint8_t inlineMenuCallback (String msg, String str, String cbacks, String id) {
382365 if (!id.length ()) return 5 ;
383- if (id.indexOf (' ,' ) < 0 ) return _inlineMenu (msg, str, id, cbacks);
384- FB_Parser ids;
385- while (ids.parse (id)) _inlineMenu (msg, str, ids.str , cbacks);
386- return 6 ;
366+ return _inlineMenu (msg, str, id, cbacks);
387367 }
388368
389369 // ===================== ВСЯКОЕ =====================
390370 // отправить запрос
371+ uint8_t sendRequest (String& req, String& id) {
372+ if (id.indexOf (' ,' ) < 0 ) {
373+ _addChatID (req, id);
374+ return sendRequest (req);
375+ }
376+ uint16_t endpos = req.length ();
377+ FB_Parser ids;
378+ uint8_t status;
379+ while (ids.parse (id)) {
380+ req.remove (endpos);
381+ _addChatID (req, ids.str );
382+ status = sendRequest (req);
383+ }
384+ return status;
385+ }
386+
391387 uint8_t sendRequest (String& req) {
392388 uint8_t status = 1 ;
393389 if (_FB_httpReq.begin (_FB_client, req)) {
394390 if (_FB_httpReq.GET () != HTTP_CODE_OK) status = 3 ;
395- const String* str = &_FB_httpReq.getString ();
396- int idx = str->indexOf (" message_id" );
397- if (idx > 0 && idx < (int )str->length ()) {
398- int end = str->indexOf (" ,\" " , idx);
399- _lastBotMsg = str->substring (idx + 12 , end).toInt ();
400- }
391+ String msgID;
392+ const String& answ = _FB_httpReq.getString ();
393+ int16_t st = 0 , end;
394+ find (answ, msgID, st, end, F (" \" message_id\" :" ), F (" ,\" " ), 13 , answ.length ());
395+ _lastBotMsg = msgID.toInt ();
401396 _FB_httpReq.end ();
402397 } else status = 4 ;
403398 return status;
@@ -422,17 +417,16 @@ class FastBot {
422417 s += F (" https://api.telegram.org/bot" );
423418 s += _token;
424419 }
425- void _addChatID (String& s, String& id) {
426- s += F (" &chat_id=" );
427- s += id;
428- }
429420 void _addMsgID (String& s, int32_t & id) {
430421 s += F (" &message_id=" );
431422 s += id;
432423 }
433- void _addMessage (String& req, String& id, String& text) {
424+ void _addChatID (String& s, String& id) {
425+ s += F (" &chat_id=" );
426+ s += id;
427+ }
428+ void _addMessage (String& req, String& text) {
434429 req += F (" /sendMessage?" );
435- _addChatID (req, id);
436430 _addText (req, text);
437431 if (!notif) req += F (" &disable_notification=true" );
438432 }
@@ -448,50 +442,47 @@ class FastBot {
448442 req.reserve (75 + 25 );
449443 _addToken (req);
450444 req += F (" /editMessageReplyMarkup?" );
451- _addChatID (req, id);
452445 _addMsgID (req, msgid);
453446 _addInlineMenu (req, str, cbck);
454- return sendRequest (req);
447+ return sendRequest (req, id );
455448 }
456449
457450 uint8_t _editMessage (int32_t msgid, String& text, String& id) {
458451 String req;
459452 req.reserve (75 + 20 );
460453 _addToken (req);
461454 req += F (" /editMessageText?" );
462- _addChatID (req, id);
463455 _addMsgID (req, msgid);
464456 _addText (req, text);
465- return sendRequest (req);
457+ return sendRequest (req, id );
466458 }
467459
468460 uint8_t _deleteMessage (int32_t msgid, String& id) {
469461 String req;
470462 req.reserve (75 + 20 );
471463 _addToken (req);
472464 req += F (" /deleteMessage?" );
473- _addChatID (req, id);
474465 _addMsgID (req, msgid);
475- return sendRequest (req);
466+ return sendRequest (req, id );
476467 }
477468
478469 uint8_t _sendMessage (String& text, String& id, int32_t replyID = 0 ) {
479470 String req;
480471 req.reserve (75 + 15 );
481472 _addToken (req);
482- _addMessage (req, id, text);
473+ _addMessage (req, text);
483474 if (replyID) {
484475 req += F (" &reply_to_message_id=" );
485476 req += replyID;
486477 }
487- return sendRequest (req);
478+ return sendRequest (req, id );
488479 }
489480
490481 uint8_t _showMenu (String& text, String& str, String& id, bool once) {
491482 String req;
492483 req.reserve (150 + str.length () + text.length ());
493484 _addToken (req);
494- _addMessage (req, id, text);
485+ _addMessage (req, text);
495486 req += F (" &reply_markup={\" keyboard\" :[[\" " );
496487 FB_Parser t;
497488 while (t.parseNT (str)) {
@@ -502,25 +493,25 @@ class FastBot {
502493 req += F (" \" ]],\" resize_keyboard\" :true" );
503494 if (once) req += F (" ,\" one_time_keyboard\" :true" );
504495 req += ' }' ;
505- return sendRequest (req);
496+ return sendRequest (req, id );
506497 }
507498
508499 uint8_t _closeMenu (String& text, String& id) {
509500 String req;
510501 req.reserve (75 + 50 );
511502 _addToken (req);
512- _addMessage (req, id, text);
503+ _addMessage (req, text);
513504 req += F (" &reply_markup={\" remove_keyboard\" :true}" );
514- return sendRequest (req);
505+ return sendRequest (req, id );
515506 }
516507
517508 uint8_t _inlineMenu (String& text, String& str, String& id, String& cback) {
518509 String req;
519510 req.reserve (150 + str.length () + cback.length ());
520511 _addToken (req);
521- _addMessage (req, id, text);
512+ _addMessage (req, text);
522513 _addInlineMenu (req, str, cback);
523- return sendRequest (req);
514+ return sendRequest (req, id );
524515 }
525516
526517 void _addInlineMenu (String& req, String& str, String& cback) {
0 commit comments