Skip to content

Commit bd24ec3

Browse files
committed
upd
1 parent dc9ce55 commit bd24ec3

File tree

12 files changed

+178
-23
lines changed

12 files changed

+178
-23
lines changed

examples/inlineMenu/inlineMenu.ino

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <Arduino.h>
2+
3+
#define WIFI_SSID ""
4+
#define WIFI_PASS ""
5+
#define BOT_TOKEN ""
6+
#define CHAT_ID ""
7+
8+
#include <FastBot2.h>
9+
FastBot2 bot;
10+
11+
void updateh(fb::Update& u) {
12+
if (u.isQuery()) {
13+
Serial.println("NEW QUERY");
14+
Serial.println(u.query().data());
15+
16+
// ответ на query
17+
// bot.answerCallbackQuery(u.query().id());
18+
bot.answerCallbackQuery(u.query().id(), "query answered");
19+
// bot.answerCallbackQuery(u.query().id(), u.query().data(), true);
20+
21+
// реакция на query. Для удобства обработаем через хэш
22+
switch (u.query().data().hash()) {
23+
case "test"_h:
24+
// кнопка kek1
25+
Serial.println("включить светодиод");
26+
break;
27+
28+
case "pest"_h:
29+
// кнопка kek2
30+
break;
31+
32+
case "lol"_h:
33+
// кнопка kek3
34+
break;
35+
}
36+
}
37+
}
38+
39+
void setup() {
40+
Serial.begin(115200);
41+
Serial.println();
42+
43+
WiFi.begin(WIFI_SSID, WIFI_PASS);
44+
while (WiFi.status() != WL_CONNECTED) {
45+
delay(500);
46+
Serial.print(".");
47+
}
48+
Serial.println("Connected");
49+
50+
// ============
51+
bot.attachUpdate(updateh); // подключить обработчик обновлений
52+
bot.setToken(F(BOT_TOKEN)); // установить токен
53+
54+
// режим опроса обновлений. Самый быстрый - Long
55+
// особенности читай тут в самом низу
56+
// https://github.com/GyverLibs/FastBot2/blob/main/docs/3.start.md
57+
58+
// bot.setPollMode(fb::Poll::Sync, 4000); // умолч
59+
// bot.setPollMode(fb::Poll::Async, 4000);
60+
bot.setPollMode(fb::Poll::Long, 20000);
61+
62+
fb::Message msg("Send inline menu", CHAT_ID);
63+
fb::InlineMenu menu("kek 1 ; kek 2 ; kek 3 \n kek 4 ; kek 5", "test;pest;lol;https://www.google.ru/;https://www.yandex.ru/");
64+
msg.setInlineMenu(menu);
65+
bot.sendMessage(msg);
66+
}
67+
68+
void loop() {
69+
// вызывать тикер в loop
70+
bot.tick();
71+
}

examples/minimal/minimal.ino

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <Arduino.h>
2+
3+
#define WIFI_SSID ""
4+
#define WIFI_PASS ""
5+
#define BOT_TOKEN ""
6+
#define CHAT_ID ""
7+
8+
#include <FastBot2.h>
9+
FastBot2 bot;
10+
11+
void updateh(fb::Update& u) {
12+
Serial.println("NEW MESSAGE");
13+
Serial.println(u.message().from().username());
14+
Serial.println(u.message().text());
15+
16+
// отправить обратно в чат (эхо)
17+
bot.sendMessage(fb::Message(u.message().text(), u.message().chat().id()));
18+
19+
// или так
20+
// fb::Message msg;
21+
// msg.text = u.message().text().toString();
22+
// msg.chatID = u.message().chat().id();
23+
// bot.sendMessage(msg);
24+
}
25+
26+
void setup() {
27+
Serial.begin(115200);
28+
Serial.println();
29+
30+
WiFi.begin(WIFI_SSID, WIFI_PASS);
31+
while (WiFi.status() != WL_CONNECTED) {
32+
delay(500);
33+
Serial.print(".");
34+
}
35+
Serial.println("Connected");
36+
37+
// ============
38+
bot.attachUpdate(updateh); // подключить обработчик обновлений
39+
bot.setToken(F(BOT_TOKEN)); // установить токен
40+
41+
// режим опроса обновлений. Самый быстрый - Long
42+
// особенности читай тут в самом низу
43+
// https://github.com/GyverLibs/FastBot2/blob/main/docs/3.start.md
44+
45+
// bot.setPollMode(fb::Poll::Sync, 4000); // умолч
46+
// bot.setPollMode(fb::Poll::Async, 4000);
47+
bot.setPollMode(fb::Poll::Long, 20000);
48+
49+
// поприветствуем админа
50+
bot.sendMessage(fb::Message("Hello!", CHAT_ID));
51+
}
52+
53+
void loop() {
54+
// вызывать тикер в loop
55+
bot.tick();
56+
}

examples/test/test.ino

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// ВНИМАНИЕ!
2+
// это тестовый пример, я им тестирую функциональность и стабильность библиотеки
3+
// тут расписана приличная часть возможностей вместе с тестами и комментариями
4+
15
#include <Arduino.h>
26

37
#define WIFI_SSID ""
@@ -237,10 +241,12 @@ void handleMessage(fb::Update& u) {
237241
// эхо, вариант 2
238242
bot.sendMessage(fb::Message(u.message().text().toString(), u.message().chat().id()));
239243

244+
// ============================
240245
// удалить сообщение юзера
241246
// bot.deleteMessage(u.message().chat().id(), u.message().id());
242247
}
243248

249+
// ============================
244250
// изменить последнее сообщение на текст из чата
245251
// if (bot.lastBotMessage()) {
246252
// fb::TextEdit et;
@@ -335,6 +341,7 @@ void updateh(fb::Update& u) {
335341
// Например для сообщения:
336342
// Serial.println(u[tg_apih::from][tg_apih::username]);
337343

344+
// ============================
338345
// service
339346
// if (heap != ESP.getFreeHeap()) {
340347
// Serial.println("MEMORY LEAK!!!!!!!!!!!!");
@@ -432,6 +439,7 @@ void setup() {
432439
// bot.sendMessage(msg);
433440
// }
434441

442+
// ============================
435443
// отправка кастомной команды из gson::string
436444
// gson::string g;
437445
// g.beginObj();
@@ -442,6 +450,7 @@ void setup() {
442450
// fb::Result res = bot.sendCommand(tg_cmd::sendMessage, g);
443451
// res.stringify(Serial);
444452

453+
// ============================
445454
// отправка команды без параметров
446455
// fb::Result res = bot.sendCommand(tg_cmd::getMe);
447456
// Serial.println(res[tg_apih::first_name]);

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=FastBot2
2-
version=1.0.1
2+
version=1.0.2
33
author=AlexGyver <[email protected]>
44
maintainer=AlexGyver <[email protected]>
55
sentence=Fast and universal Arduino/ESP8266/ESP32 library for Telegram bot

src/core/core.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,15 @@ class Core : public Http {
165165
} else if (_online) {
166166
bool tout = millis() - _last_send >= (_poll_mode == Poll::Long ? FB_LONG_POLL_TOUT : _poll_prd);
167167
if (!_last_send || tout) {
168-
getUpdates(false);
168+
if (_poll_mode == Poll::Sync) {
169+
Result res = getUpdates(true);
170+
if (res.isArray()) {
171+
_parseUpdates(res);
172+
return 1;
173+
}
174+
} else {
175+
getUpdates(false);
176+
}
169177
}
170178
}
171179

@@ -204,7 +212,7 @@ class Core : public Http {
204212
p[tg_api::offset] = _poll_offset;
205213
updates.makePacket(p);
206214

207-
if (_poll_mode == Poll::Sync || wait) {
215+
if (wait) {
208216
_poll_wait = 0;
209217
return sendPacket(p, true);
210218
} else {
@@ -296,14 +304,18 @@ class Core : public Http {
296304
FB_ESP_YIELD();
297305
if (resp) {
298306
if (resp.type() == F("application/json")) {
307+
FB_LOG("got json");
299308
Result res(resp.body());
300309
res.parseJson();
301310
http.flush();
302-
if (_cbRaw) _cbRaw(res.getRaw());
303311
FB_ESP_YIELD();
304-
if (res && res.isObject()) _parseResult(res);
312+
if (res) {
313+
if (_cbRaw) _cbRaw(res.getRaw());
314+
if (res.isObject()) _parseResult(res);
315+
}
305316
return res;
306317
} else if (resp.type() == F("application/octet-stream")) {
318+
FB_LOG("got file");
307319
return Result(resp.body());
308320
} else {
309321
FB_LOG("unknown response");
@@ -340,7 +352,7 @@ class Core : public Http {
340352
uint32_t offset = result[i][tg_apih::update_id].toInt32();
341353
if (!_poll_offset) _poll_offset = offset;
342354
if (_incr_auto) _poll_offset = offset + 1;
343-
355+
344356
size_t typeHash = upd.keyHash();
345357
Update update(upd, typeHash, offset);
346358
if (typeHash == tg_apih::callback_query) _query_answ = false;

src/core/types/InlineMenu.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ class InlineMenu {
1515

1616
public:
1717
InlineMenu() {}
18-
InlineMenu(const String& text, const String& data) : text(text), data(data) {}
18+
InlineMenu(Text text, Text data) {
19+
text.toString(this->text);
20+
data.toString(this->data);
21+
}
1922
InlineMenu(uint16_t reserve) {
2023
this->reserve(reserve);
2124
}
@@ -39,7 +42,7 @@ class InlineMenu {
3942
this->text += ';';
4043
this->data += ';';
4144
}
42-
45+
4346
text.addString(this->text);
4447
if (data) data.addString(this->data);
4548
else text.addString(this->data);

src/core/types/Location.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Location : private Message {
1414

1515
public:
1616
Location();
17-
Location(float latitude, float longitude, const Value& chatID) : latitude(latitude), longitude(longitude) {
17+
Location(float latitude, float longitude, Value chatID) : latitude(latitude), longitude(longitude) {
1818
this->chatID = chatID;
1919
}
2020

src/core/types/Menu.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ class Menu {
1515

1616
public:
1717
Menu() {}
18-
Menu(const String& text) : text(text) {}
18+
Menu(Text text) {
19+
text.toString(this->text);
20+
}
1921

2022
// надписи кнопок. Гор. разделитель - ;, верт. - \n (кнопка_1 ; кнопка_2 \n кнопка_3 ; кнопка_4)
2123
String text = "";

src/core/types/Message.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ class Message {
3232
HTML,
3333
};
3434
Message() {}
35-
Message(const String& text, const Value& chatID) : text(text), chatID(chatID) {}
35+
Message(Text text, Value chatID) : chatID(chatID) {
36+
text.toString(this->text);
37+
}
3638

3739
// для ручного добавления тех параметров, которых нет в классе!
3840
gson::string json;

src/core/types/MessageEdit.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class TextEdit : private Message {
1313

1414
public:
1515
TextEdit() {}
16-
TextEdit(const String& text, uint32_t messageID, const Value& chatID) : messageID(messageID) {
17-
this->text = text;
16+
TextEdit(Text text, uint32_t messageID, Value chatID) : messageID(messageID) {
17+
text.toString(this->text);
1818
this->chatID = chatID;
1919
}
2020

@@ -41,10 +41,10 @@ class MenuEdit : private Message {
4141

4242
public:
4343
MenuEdit() {}
44-
MenuEdit(uint32_t messageID, const Value& chatID) : messageID(messageID) {
44+
MenuEdit(uint32_t messageID, Value chatID) : messageID(messageID) {
4545
this->chatID = chatID;
4646
}
47-
MenuEdit(uint32_t messageID, const Value& chatID, InlineMenu& menu) : messageID(messageID) {
47+
MenuEdit(uint32_t messageID, Value chatID, InlineMenu& menu) : messageID(messageID) {
4848
this->chatID = chatID;
4949
setInlineMenu(menu);
5050
}
@@ -69,7 +69,7 @@ class CaptionEdit : private Message {
6969

7070
public:
7171
CaptionEdit() {}
72-
CaptionEdit(const String& caption, uint32_t messageID, const Value& chatID) : caption(caption), messageID(messageID) {
72+
CaptionEdit(const String& caption, uint32_t messageID, Value chatID) : caption(caption), messageID(messageID) {
7373
this->chatID = chatID;
7474
}
7575

@@ -98,7 +98,7 @@ class LocationEdit : private Message {
9898

9999
public:
100100
LocationEdit() {}
101-
LocationEdit(float latitude, float longitude, uint32_t messageID, const Value& chatID) : latitude(latitude), longitude(longitude), messageID(messageID) {
101+
LocationEdit(float latitude, float longitude, uint32_t messageID, Value chatID) : latitude(latitude), longitude(longitude), messageID(messageID) {
102102
this->chatID = chatID;
103103
}
104104

@@ -142,7 +142,7 @@ class LocationStop : private Message {
142142

143143
public:
144144
LocationStop() {}
145-
LocationStop(uint32_t messageID, const Value& chatID) : messageID(messageID) {
145+
LocationStop(uint32_t messageID, Value chatID) : messageID(messageID) {
146146
this->chatID = chatID;
147147
}
148148

src/core/types/MessageForward.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ class MessageForward {
1717
public:
1818
MessageForward() {}
1919
MessageForward(uint32_t messageID,
20-
const Value& fromChatID,
21-
const Value& chatID) : messageID(messageID),
22-
fromChatID(fromChatID),
23-
chatID(chatID) {}
20+
Value fromChatID,
21+
Value chatID) : messageID(messageID),
22+
fromChatID(fromChatID),
23+
chatID(chatID) {}
2424

2525
// id пересылаемого сообщения в чате
2626
uint32_t messageID;

src/core/updates.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class Updates {
9999
tg_api::chat_boost,
100100
tg_api::removed_chat_boost,
101101
};
102-
102+
103103
p.beginArr(tg_api::allowed_updates);
104104
for (uint8_t i = 0; i < FB_UPDATES_AMOUNT; i++) {
105105
if (read(i)) p += upd_arr[i];

0 commit comments

Comments
 (0)