Skip to content

Commit dca42a7

Browse files
committed
upd
1 parent bd24ec3 commit dca42a7

File tree

6 files changed

+35
-4
lines changed

6 files changed

+35
-4
lines changed

docs/1.main.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#define FB_USE_LOG Serial // выводить логи ошибок
1111
```
1212
13+
### Указатель на текущий объект
14+
Внутри подключаемых обработчиков можно использовать `fb::thisBot` - указатель на текущий объект бота, который вызвал обработчик
15+
1316
### Класс бота
1417
<details>
1518
<summary>FastBot2Client</summary>

docs/4.updates.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ void update(fb::Update& u) {
1414
}
1515
```
1616

17+
> Указатель на текущий объект FastBot (тот, который вызвал коллбэк) можно получить из переменной `fb::Core* fb::thisBot`. Кастовать его в нужный класс и использовать:
18+
19+
```cpp
20+
void update(fb::Update& u) {
21+
static_cast<FastBot2*>(fb::thisBot)->sendMessage(...);
22+
}
23+
```
24+
1725
Отличие FastBot2 состоит в том, что библиотека не разбирает пакет обновления по отдельным переменным: данные извлекаются в момент обращения к ним. Это сильно быстрее и расходует в сотни раз меньше памяти.
1826
1927
### Тип данных Text

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.2
2+
version=1.0.3
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: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "Fetcher.h"
77
#include "api.h"
88
#include "bot_config.h"
9+
#include "core_class.h"
910
#include "http.h"
1011
#include "packet.h"
1112
#include "result.h"
@@ -310,7 +311,11 @@ class Core : public Http {
310311
http.flush();
311312
FB_ESP_YIELD();
312313
if (res) {
313-
if (_cbRaw) _cbRaw(res.getRaw());
314+
if (_cbRaw) {
315+
thisBot = this;
316+
_cbRaw(res.getRaw());
317+
thisBot = nullptr;
318+
}
314319
if (res.isObject()) _parseResult(res);
315320
}
316321
return res;
@@ -330,7 +335,11 @@ class Core : public Http {
330335
void _parseResult(gson::Entry& result) {
331336
FB_LOG("got result");
332337
if (result.has(tg_apih::message_id)) _last_bot = result[tg_apih::message_id];
333-
if (_cbResult) _cbResult(result);
338+
if (_cbResult) {
339+
thisBot = this;
340+
_cbResult(result);
341+
thisBot = nullptr;
342+
}
334343
}
335344

336345
void _parseUpdates(gson::Entry& result) {
@@ -341,6 +350,7 @@ class Core : public Http {
341350
#endif
342351
return;
343352
}
353+
thisBot = this;
344354
_exit_f = false;
345355
uint8_t len = result.length();
346356

@@ -371,11 +381,12 @@ class Core : public Http {
371381

372382
if (_reboot == Fetcher::Reboot::Triggered) {
373383
_reboot = Fetcher::Reboot::WaitUpdate;
374-
return;
384+
break;
375385
}
376386

377387
if (_exit_f) break;
378388
}
389+
thisBot = nullptr;
379390
}
380391
};
381392

src/core/core_class.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "core_class.h"
2+
3+
namespace fb {
4+
5+
Core* thisBot = nullptr;
6+
7+
}

src/core/core_class.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ namespace fb {
44

55
class Core;
66

7+
extern Core* thisBot;
8+
79
}

0 commit comments

Comments
 (0)