33
44#include < vector>
55#include < algorithm>
6+ #include < memory>
67#include " to_string.h"
78#include " json.hpp"
9+ #include " botkey.h"
10+ #include " const_str.h"
811
912using nlohmann::json;
1013
@@ -19,6 +22,13 @@ const char *postChannelName =
1922#include " cfg/channelname"
2023;
2124
25+ // Bot commands config
26+ const TgInteger idOwner =
27+ #include " cfg/ownerid"
28+ ;
29+ const char *postCommandName =
30+ #include " cfg/postcommandname"
31+ ;
2232
2333class UpdateStateHandler {
2434 std::vector<TgInteger> postUserIds;
@@ -231,20 +241,64 @@ class AddAdminHandler : public PostHandler {
231241 }
232242};
233243
234- PhotoChannelPostHandler rcnPostHandler (idPostChannel, postChannelName);
244+ class BotCommand {
245+ public:
246+ virtual bool command (CURL *c, const json &upd, const std::string &cmd, size_t off) = 0;
247+ };
248+
249+ class BotCommandsHandler {
250+ std::map<std::string, std::unique_ptr<BotCommand> > commands;
251+
252+ public:
253+
254+ bool handleCommands (CURL *c, TgInteger fromId,
255+ TgInteger chatId, const json &upd)
256+ {
257+ auto text = upd[" message" ].get <std::string>();
258+ const auto &it = commands.find (" vzhuh" );
259+ size_t off = 0 ;
260+ if (!easy_bot_check_command (text.c_str (), text.length (),
261+ BOT_NAME, COUNTOF (BOT_NAME), &off)) {
262+ return false ;
263+ }
264+
265+ return true ;
266+ }
267+
268+ void addCommand (const std::string &name, std::unique_ptr<BotCommand> command)
269+ {
270+ commands[name].swap (command);
271+ }
272+ };
273+
274+ class PostCommandHandler : public BotCommand {
275+ PhotoChannelPostHandler &h;
276+ public:
277+ PostCommandHandler (PhotoChannelPostHandler &ph) : h(ph) {}
278+
279+ bool command (CURL *c, const json &upd, const std::string &cmd, size_t off) override
280+ {
281+
282+ return true ;
283+ }
284+ };
285+
286+ PhotoChannelPostHandler photoPostHandler (idPostChannel, postChannelName);
287+ BotCommandsHandler commandsHandler;
235288
236- void handle_update_message (CURL *c, json &msg, bool &quit, size_t &upd_id )
289+ void handle_update_message (CURL *c, json &msg, bool &quit, size_t &updId )
237290{
238291 TgInteger fromId = 0 ;
239292 TgInteger chatId = 0 ;
240293 auto &from = msg[" from" ];
241294 auto &chat = msg[" chat" ];
242295 fromId = from[" id" ].get <TgInteger>();
243296 chatId = chat[" id" ].get <TgInteger>();
244- rcnPostHandler.handle_private_updates (c, fromId, chatId, msg);
297+ photoPostHandler.handle_private_updates (c, fromId, chatId, msg);
298+ commandsHandler.handleCommands (c, fromId, chatId, msg);
245299}
246300
247- void handle_all_updates (CURL *c, json &upd, bool &quit, size_t &upd_id )
301+ void handle_all_updates (CURL *c, json &upd, bool &quit, size_t &updId )
248302{
249303 auto &r = upd[" result" ];
250304 if (!r.is_array ()) {
@@ -253,9 +307,8 @@ void handle_all_updates(CURL *c, json &upd, bool &quit, size_t &upd_id)
253307 }
254308
255309 for (auto msg : r) {
256- handle_all_updates (c, msg, quit, upd_id );
310+ handle_all_updates (c, msg, quit, updId );
257311 }
258- // rcnPostHandler.handle_private_updates();
259312}
260313
261314int main (int argc, char *argv[])
@@ -267,6 +320,8 @@ int main(int argc, char *argv[])
267320 CURL *c = bot_network_init ();
268321 json upd;
269322 bool quit = false ;
323+ commandsHandler.addCommand (postCommandName,
324+ std::make_unique<PostCommandHandler>(photoPostHandler));
270325 do {
271326 if (easy_perform_getUpdates (c, &d, upd_id, sleep_time) != CURLE_OK) {
272327 fprintf (stderr, " Bot network error.\n " );
0 commit comments