Skip to content

Commit dbd576f

Browse files
committed
Add bot update and handling commands logic.
1 parent 755183d commit dbd576f

File tree

8 files changed

+83
-6
lines changed

8 files changed

+83
-6
lines changed

bot_easy_api.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "tgtypes.h"
44

55
// struct CURL;
6+
// TODO: get rid of this header.
67
#include <curl/curl.h>
78
struct writefn_data;
89

@@ -30,6 +31,7 @@ int easy_perform_sendLocation(CURL *c, TgInteger chat_id,
3031
const TgLocation &loc,
3132
TgInteger reply_id = 0, writefn_data *d = 0);
3233
int easy_perform_leaveChat(CURL *c, TgInteger chatId);
34+
3335
bool easy_bot_check_command(const char *cmd, size_t sz, const char *name,
3436
size_t name_sz, size_t *cmd_end_off, bool *shortcmd = 0);
3537
#endif

botkey.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
#define BOT_KEY "yourbotkey"
22
#define BOT_URL "https://api.telegram.org/bot" BOT_KEY
3+
#define BOT_NAME "YourBotUserName"

cfg/channelid

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0 /* your channel id */

cfg/channelname

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"your channel name in utf-8."

cfg/ownerid

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0 /*insert your telegram user ID. */

cfg/postcommandname

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"post" /* insert your own command name. */

const_str.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// const_str.h - contains a declaration of token helper struct and macros.
2+
//
3+
#ifndef TGBOTLIB_CONST_STR_H
4+
#define TGBOTLIB_CONST_STR_H
5+
#include <stddef.h>
6+
7+
struct const_str {
8+
char *str;
9+
size_t sz;
10+
};
11+
12+
#define COUNTOF(str) (sizeof(str) / sizeof(str[0]))
13+
#define MAKE_CONST_STR(str), { (str), COUNTOF(str) }
14+
15+
#endif

main.cpp

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
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

912
using 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

2333
class 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

261314
int 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

Comments
 (0)