tgbot-cpp
Loading...
Searching...
No Matches
TgWebhookServer.h
Go to the documentation of this file.
1#ifndef TGBOT_TGHTTPSERVER_H
2#define TGBOT_TGHTTPSERVER_H
3
4#include "tgbot/Bot.h"
8
9#include <string>
10#include <unordered_map>
11#include <utility>
12
13namespace TgBot {
14
15template<typename Protocol>
16class TgWebhookServer : public HttpServer<Protocol> {
17
18public:
19 TgWebhookServer(const typename boost::asio::basic_socket_acceptor<Protocol>::endpoint_type& endpoint, const typename HttpServer<Protocol>::ServerHandler& handler) = delete;
20
21 TgWebhookServer(const typename boost::asio::basic_socket_acceptor<Protocol>::endpoint_type& endpoint, std::string path, const EventHandler& eventHandler)
22 : HttpServer<Protocol>(endpoint,
23 [this](const std::string& _1, const std::unordered_map<std::string, std::string>& _2) { return _handle(_1, _2); }),
24 _path(std::move(path)), _eventHandler(eventHandler), _tgTypeParser()
25 {
26 }
27
28 TgWebhookServer(const typename boost::asio::basic_socket_acceptor<Protocol>::endpoint_type& endpoint, const Bot& bot)
29 : TgWebhookServer(endpoint, "/" + bot.getToken(), bot.getEventHandler())
30 {
31 }
32
33private:
34 std::string _handle(const std::string& data, const std::unordered_map<std::string, std::string>& headers) {
35 if (headers.at("_method") == "POST" && headers.at("_path") == _path) {
36 _eventHandler.handleUpdate(_tgTypeParser.parseJsonAndGetUpdate(_tgTypeParser.parseJson(data)));
37 }
38 return HttpServer<Protocol>::_httpParser.generateResponse("", "text/plain", 200, "OK", false);
39 }
40
41 const std::string _path;
42 const EventHandler& _eventHandler;
43 const TgTypeParser _tgTypeParser;
44};
45
46}
47
48#endif //TGBOT_TGHTTPSERVER_H
This object holds other objects specific for this bot instance.
Definition Bot.h:21
void handleUpdate(const Update::Ptr &update) const
std::string generateResponse(const std::string &data, const std::string &mimeType, unsigned short statusCode, const std::string &statusStr, bool isKeepAlive) const
This class handles HTTP requests from the Internet.
Definition HttpServer.h:25
std::function< std::string(const std::string &, const std::unordered_map< std::string, std::string > &)> ServerHandler
Definition HttpServer.h:31
const HttpParser _httpParser
Definition HttpServer.h:167
TgWebhookServer(const typename boost::asio::basic_socket_acceptor< Protocol >::endpoint_type &endpoint, const Bot &bot)
TgWebhookServer(const typename boost::asio::basic_socket_acceptor< Protocol >::endpoint_type &endpoint, std::string path, const EventHandler &eventHandler)
TgWebhookServer(const typename boost::asio::basic_socket_acceptor< Protocol >::endpoint_type &endpoint, const typename HttpServer< Protocol >::ServerHandler &handler)=delete
Definition Api.h:40