Skip to content

Commit c881134

Browse files
committed
upd
1 parent bfbd8a0 commit c881134

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

docs/1.main.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ void skipNextMessage();
6262
// id последнего отправленного сообщения от бота
6363
uint32_t lastBotMessage();
6464
65+
// авто-инкремент update offset (умолч. true). Если отключен - нужно вызывать skipNextMessage() в update
66+
void autoIncrement(bool incr);
67+
68+
// автоматически отвечать на query, если юзер не ответил в update (умолч. true)
69+
void autoQuery(bool query);
70+
71+
// покинуть цикл разбора updates, вызывать в обработичке update
72+
void exitUpdates();
73+
6574
// запустить (по умолчанию уже запущен)
6675
void begin();
6776
@@ -253,6 +262,9 @@ RemovedChatBoost
253262
// тип апдейта
254263
Type type();
255264

265+
// id апдейта
266+
uint32_t id();
267+
256268
// ================ QUERY ================
257269

258270
// это query

src/core/core.h

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,21 @@ class Core : public Http {
8383
_poll_offset++;
8484
}
8585

86+
// авто-инкремент update offset (умолч. true). Если отключен - нужно вызывать skipNextMessage() в update
87+
void autoIncrement(bool incr) {
88+
_incr_auto = incr;
89+
}
90+
91+
// автоматически отвечать на query, если юзер не ответил в update (умолч. true)
92+
void autoQuery(bool query) {
93+
_query_auto = query;
94+
}
95+
96+
// покинуть цикл разбора updates, вызывать в обработичке update
97+
void exitUpdates() {
98+
_exit_f = true;
99+
}
100+
86101
// id последнего отправленного сообщения от бота
87102
uint32_t lastBotMessage() {
88103
return _last_bot;
@@ -262,8 +277,11 @@ class Core : public Http {
262277
uint8_t _poll_limit = 3;
263278
bool _poll_wait = 0;
264279
bool _query_answ = 0;
280+
bool _query_auto = true;
265281
bool _state = true;
266282
bool _online = true;
283+
bool _incr_auto = true;
284+
bool _exit_f = false;
267285
int32_t _poll_offset = 0;
268286
uint32_t _last_bot = 0;
269287
uint32_t _last_send = 0;
@@ -311,33 +329,40 @@ class Core : public Http {
311329
#endif
312330
return;
313331
}
332+
_exit_f = false;
314333
uint8_t len = result.length();
315-
if (len) _poll_offset = result[0][tg_apih::update_id].toInt32();
316334

317335
for (uint8_t i = 0; i < len; i++) {
318336
FB_ESP_YIELD();
319-
_poll_offset++;
320337
gson::Entry upd = result[i][1];
321338
if (!upd) continue;
322339

340+
uint32_t offset = result[i][tg_apih::update_id].toInt32();
341+
if (!_poll_offset) _poll_offset = offset;
342+
if (_incr_auto) _poll_offset = offset + 1;
343+
323344
size_t typeHash = upd.keyHash();
324-
Update update(upd, typeHash);
325-
if (typeHash == tg_apih::callback_query) _query_answ = 0;
345+
Update update(upd, typeHash, offset);
346+
if (typeHash == tg_apih::callback_query) _query_answ = false;
326347

327348
if (_cbUpdate) _cbUpdate(update);
328349
FB_ESP_YIELD();
329350

330351
if (typeHash == tg_apih::callback_query && !_query_answ) {
331352
_query_answ = true;
332-
fb::Packet p(tg_cmd::answerCallbackQuery, _token);
333-
p[tg_api::callback_query_id] = update.query().id();
334-
sendPacket(p, false);
353+
if (_query_auto) {
354+
fb::Packet p(tg_cmd::answerCallbackQuery, _token);
355+
p[tg_api::callback_query_id] = update.query().id();
356+
sendPacket(p, false);
357+
}
335358
}
336359

337360
if (_reboot == Fetcher::Reboot::Triggered) {
338361
_reboot = Fetcher::Reboot::WaitUpdate;
339362
return;
340363
}
364+
365+
if (_exit_f) break;
341366
}
342367
}
343368
};

src/core/types/Update.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,18 @@ class Update : public EntryAccess {
3737
RemovedChatBoost = tg_apih::removed_chat_boost,
3838
};
3939

40-
Update(gson::Entry& entry, size_t type) : EntryAccess(entry), _type((Type)type) {}
40+
Update(gson::Entry& entry, size_t type, uint32_t id) : EntryAccess(entry), _type((Type)type), _id(id) {}
4141

4242
// тип апдейта
4343
Type type() {
4444
return _type;
4545
}
4646

47+
// id апдейта
48+
uint32_t id() {
49+
return _id;
50+
}
51+
4752
// ================ QUERY ================
4853

4954
// это query
@@ -80,6 +85,7 @@ class Update : public EntryAccess {
8085

8186
private:
8287
Type _type;
88+
uint32_t _id;
8389
};
8490

8591
} // namespace fb

0 commit comments

Comments
 (0)