tgbot-cpp
Loading...
Searching...
No Matches
TgLongPoll.h
Go to the documentation of this file.
1#pragma once
2
3#include "tgbot/Types.h"
4#include "tgbot/export.h"
5
6#include <atomic>
7#include <csignal>
8#include <cstdint>
9#include <exception>
10#include <functional>
11#include <initializer_list>
12#include <memory>
13#include <string>
14#include <vector>
15
16namespace TgBot {
17
18class Api;
19class Bot;
20class EventHandler;
21
28public:
29 using ErrorHandler = std::function<void(const std::exception&)>;
30
31 TgLongPoll(const Api* api, const EventHandler* eventHandler, std::int32_t limit, std::int32_t timeout,
32 std::shared_ptr<std::vector<std::string>> allowUpdates);
33 TgLongPoll(const Bot& bot, std::int32_t limit = 100, std::int32_t timeout = 10,
34 const std::shared_ptr<std::vector<std::string>>& allowUpdates = nullptr);
35
40 void start();
41
49 void startLoop(const ErrorHandler& errorHandler = { }, std::initializer_list<int> signals = { SIGINT, SIGTERM });
50
54 void stop();
55
59 bool isRunning() const {
60 return _isRunning.load();
61 }
62
63private:
64 const Api* _api;
65 const EventHandler* _eventHandler;
66 std::int32_t _lastUpdateId = 0;
67 std::int32_t _limit;
68 std::int32_t _timeout;
69 std::shared_ptr<std::vector<std::string>> _allowUpdates;
70 std::atomic<bool> _isRunning { false };
71
72 std::vector<std::shared_ptr<Update>> _updates;
73};
74
75} // namespace TgBot
Executes methods from the Telegram Bot API.
Definition Api.h:30
This object holds other objects specific for this bot instance.
Definition Bot.h:20
TgLongPoll(const Bot &bot, std::int32_t limit=100, std::int32_t timeout=10, const std::shared_ptr< std::vector< std::string > > &allowUpdates=nullptr)
void stop()
Requests cancellation of the active HTTP request and stops startLoop().
bool isRunning() const
Returns true while startLoop() is active.
Definition TgLongPoll.h:59
std::function< void(const std::exception &)> ErrorHandler
Definition TgLongPoll.h:29
void start()
Performs one long-polling iteration and dispatches received updates. Parses received updates and pass...
TgLongPoll(const Api *api, const EventHandler *eventHandler, std::int32_t limit, std::int32_t timeout, std::shared_ptr< std::vector< std::string > > allowUpdates)
void startLoop(const ErrorHandler &errorHandler={ }, std::initializer_list< int > signals={ SIGINT, SIGTERM })
Polls for updates until stop() is called, one of the configured signals is received,...
#define TGBOT_API
Definition export.h:5
Definition Api.h:17