Compare commits
6 Commits
b40c061ccf
...
6ca04a9392
| Author | SHA1 | Date | |
|---|---|---|---|
| 6ca04a9392 | |||
| 035483f54f | |||
| 4186b23e1a | |||
| da146feec8 | |||
| 773dae4676 | |||
| 7054ad3525 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,4 +1,4 @@
|
|||||||
.build/
|
build/
|
||||||
.cache/
|
.cache/
|
||||||
.token
|
.token
|
||||||
|
|
||||||
|
|||||||
@@ -8,9 +8,8 @@
|
|||||||
|
|
||||||
#define WELCOME_MSG "Hi there! This bot is coded in C."
|
#define WELCOME_MSG "Hi there! This bot is coded in C."
|
||||||
|
|
||||||
void parse_command(tgbot_s *bot, tgbot_update_s *update);
|
void parse_command(const tgbot_s *bot, const tgbot_update_s *update);
|
||||||
void sighandler(int signum);
|
void sighandler(int signum);
|
||||||
void callback_parser(tgbot_s *bot, tgbot_cbquery_s *query);
|
|
||||||
|
|
||||||
bool run = true;
|
bool run = true;
|
||||||
tgbot_s *bot;
|
tgbot_s *bot;
|
||||||
@@ -35,11 +34,12 @@ void callback_handler(tgbot_s *bot, tgbot_cbquery_s *query) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void sighandler(int signum) {
|
void sighandler(int signum) {
|
||||||
|
(void)signum;
|
||||||
fprintf(stdout, "Closing...\n");
|
fprintf(stdout, "Closing...\n");
|
||||||
run = false;
|
run = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void parse_command(tgbot_s *bot, tgbot_update_s *update) {
|
void parse_command(const tgbot_s *bot, const tgbot_update_s *update) {
|
||||||
tgbot_rc ret;
|
tgbot_rc ret;
|
||||||
|
|
||||||
if (strcmp("/start", update->text) == 0) {
|
if (strcmp("/start", update->text) == 0) {
|
||||||
@@ -63,7 +63,6 @@ int main(void) {
|
|||||||
/* Find "your" way to free the resources */
|
/* Find "your" way to free the resources */
|
||||||
signal(SIGINT, sighandler);
|
signal(SIGINT, sighandler);
|
||||||
|
|
||||||
tgbot_rc ret;
|
|
||||||
char token[256];
|
char token[256];
|
||||||
|
|
||||||
FILE *fp = fopen(".token", "r");
|
FILE *fp = fopen(".token", "r");
|
||||||
@@ -71,7 +70,7 @@ int main(void) {
|
|||||||
fprintf(stderr, "No .token file found!\n");
|
fprintf(stderr, "No .token file found!\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
fscanf(fp, "%s", token);
|
fscanf(fp, "%255s", token);
|
||||||
fprintf(stdout, "Token: %s\n", token);
|
fprintf(stdout, "Token: %s\n", token);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
@@ -101,7 +100,7 @@ int main(void) {
|
|||||||
|
|
||||||
/* Main loop */
|
/* Main loop */
|
||||||
while (run) {
|
while (run) {
|
||||||
ret = tgbot_get_update(bot, &update, callback_handler);
|
tgbot_rc ret = tgbot_get_update(bot, &update, callback_handler);
|
||||||
if (ret != TGBOT_OK) {
|
if (ret != TGBOT_OK) {
|
||||||
fprintf(stderr, "tgbot_get_updates()\n");
|
fprintf(stderr, "tgbot_get_updates()\n");
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#define TOKEN_SIZE 128
|
#define TOKEN_SIZE 128
|
||||||
#define API_SIZE 512
|
#define API_SIZE 512
|
||||||
|
#define URL_LEN 1024
|
||||||
|
|
||||||
#define FIRSTNAME_SIZE 256
|
#define FIRSTNAME_SIZE 256
|
||||||
#define USERNAME_SIZE 32
|
#define USERNAME_SIZE 32
|
||||||
@@ -76,7 +77,7 @@ typedef void (*Callback)(tgbot_s *bot, tgbot_cbquery_s *query);
|
|||||||
/**
|
/**
|
||||||
* @brief An enum to represent error codes.
|
* @brief An enum to represent error codes.
|
||||||
*/
|
*/
|
||||||
enum tgbot_rc_t {
|
enum tgbot_rc {
|
||||||
TGBOT_OK = 0,
|
TGBOT_OK = 0,
|
||||||
TGBOT_INIT_ERROR,
|
TGBOT_INIT_ERROR,
|
||||||
TGBOT_REQUEST_ERROR,
|
TGBOT_REQUEST_ERROR,
|
||||||
@@ -87,22 +88,22 @@ enum tgbot_rc_t {
|
|||||||
TGBOT_SENDDICE_ERROR,
|
TGBOT_SENDDICE_ERROR,
|
||||||
TGBOT_TELEGRAM_OK_ERROR,
|
TGBOT_TELEGRAM_OK_ERROR,
|
||||||
};
|
};
|
||||||
typedef enum tgbot_rc_t tgbot_rc;
|
typedef enum tgbot_rc tgbot_rc;
|
||||||
|
|
||||||
enum tgbot_json_opt_type_t {
|
enum tgbot_opt_type {
|
||||||
tgbot_opt_int,
|
tgbot_opt_int,
|
||||||
tgbot_opt_int64,
|
tgbot_opt_int64,
|
||||||
tgbot_opt_string,
|
tgbot_opt_string,
|
||||||
tgbot_opt_inlinekeyboard,
|
tgbot_opt_inlinekeyboard,
|
||||||
tgbot_opt_null,
|
tgbot_opt_null,
|
||||||
};
|
};
|
||||||
typedef enum tgbot_json_opt_type_t tgbot_json_opt_type;
|
typedef enum tgbot_opt_type tgbot_opt_type_e;
|
||||||
|
|
||||||
struct tgbot_json_option_t {
|
struct tgbot_option {
|
||||||
char key[32];
|
char key[32];
|
||||||
void *value;
|
void *value;
|
||||||
tgbot_json_opt_type type;
|
tgbot_opt_type_e type;
|
||||||
};
|
};
|
||||||
typedef struct tgbot_json_option_t tgbot_json_option;
|
typedef struct tgbot_option tgbot_option_s;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include <json-c/json.h>
|
#include <json-c/json.h>
|
||||||
|
|
||||||
json_object *json_builder(tgbot_json_option *options, size_t optionslen);
|
json_object *json_builder(tgbot_option_s *options, size_t optionslen);
|
||||||
|
|
||||||
json_object *json_ikb_new(tgbot_inlinekeyboard_s *keyboard);
|
json_object *json_ikb_new(tgbot_inlinekeyboard_s *keyboard);
|
||||||
|
|
||||||
|
|||||||
@@ -4,21 +4,17 @@
|
|||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include <json-c/json.h>
|
#include <json-c/json.h>
|
||||||
|
|
||||||
|
/* Retrieve update */
|
||||||
tgbot_rc tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_handler);
|
tgbot_rc tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_handler);
|
||||||
tgbot_rc tgbot_parse_message(tgbot_s *bot, tgbot_update_s *update, json_object *result);
|
|
||||||
tgbot_rc tgbot_parse_cbquery(tgbot_s *bot, tgbot_cbquery_s *query, json_object *result, Callback query_handler);
|
|
||||||
|
|
||||||
/* Request */
|
|
||||||
tgbot_rc tgbot_request(const char *url, struct memory_buffer **mb, json_object *json);
|
|
||||||
|
|
||||||
/* Methods */
|
/* Methods */
|
||||||
tgbot_rc tgbot_get_me(tgbot_s *bot, tgbot_me_s *me);
|
tgbot_rc tgbot_get_me(const tgbot_s *bot, tgbot_me_s *me);
|
||||||
tgbot_rc tgbot_send_message(tgbot_s *bot, int64_t chat_id, const char *text, const char *parse_mode,
|
tgbot_rc tgbot_send_message(const tgbot_s *bot, int64_t chat_id, const char *text, const char *parse_mode,
|
||||||
tgbot_inlinekeyboard_s *reply_markup);
|
tgbot_inlinekeyboard_s *reply_markup);
|
||||||
tgbot_rc tgbot_send_dice(tgbot_s *bot, int64_t chat_id, const char *emoji);
|
tgbot_rc tgbot_send_dice(const tgbot_s *bot, int64_t chat_id, const char *emoji);
|
||||||
|
|
||||||
/* Updating Methods */
|
/* Updating Methods */
|
||||||
tgbot_rc tgbot_edit_message_text(tgbot_s *bot, int64_t chat_id, long message_id, const char *text,
|
tgbot_rc tgbot_edit_message_text(const tgbot_s *bot, int64_t chat_id, long message_id, const char *text,
|
||||||
tgbot_inlinekeyboard_s *keyboard);
|
tgbot_inlinekeyboard_s *keyboard);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
11
include/parse.h
Normal file
11
include/parse.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef TGBOT_PARSE_H
|
||||||
|
#define TGBOT_PARSE_H
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
#include <json-c/json.h>
|
||||||
|
|
||||||
|
tgbot_rc tgbot_parse_message(tgbot_s *bot, tgbot_update_s *update, json_object *result);
|
||||||
|
|
||||||
|
tgbot_rc tgbot_parse_cbquery(tgbot_s *bot, tgbot_cbquery_s *query, json_object *result, Callback cbq_handler);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -12,7 +12,7 @@
|
|||||||
*
|
*
|
||||||
* @return Bot pointer.
|
* @return Bot pointer.
|
||||||
*/
|
*/
|
||||||
tgbot_s *tgbot_new(char *token);
|
tgbot_s *tgbot_new(const char *token);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Cleans the memory.
|
* @brief Cleans the memory.
|
||||||
|
|||||||
@@ -34,6 +34,6 @@ tgbot_dep = declare_dependency(
|
|||||||
# Example
|
# Example
|
||||||
executable(
|
executable(
|
||||||
'example',
|
'example',
|
||||||
'examples/echobot/echobot.c',
|
'examples/inlinekeyboard/inlinekeyboard.c',
|
||||||
dependencies: tgbot_dep,
|
dependencies: tgbot_dep,
|
||||||
)
|
)
|
||||||
|
|||||||
44
src/json.c
44
src/json.c
@@ -1,27 +1,41 @@
|
|||||||
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "json.h"
|
#include "json.h"
|
||||||
|
|
||||||
json_object *json_builder(tgbot_json_option *options, size_t optionslen) {
|
json_object *json_builder(tgbot_option_s *options, size_t optionslen) {
|
||||||
json_object *rjson = json_object_new_object();
|
json_object *rjson = json_object_new_object();
|
||||||
|
if (!rjson) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < optionslen; ++i) {
|
for (size_t i = 0; i < optionslen; ++i) {
|
||||||
if (options[i].type == tgbot_opt_int) {
|
switch (options[i].type) {
|
||||||
|
case tgbot_opt_int: {
|
||||||
json_object_object_add(rjson, options[i].key, json_object_new_int(*((int32_t *)options[i].value)));
|
json_object_object_add(rjson, options[i].key, json_object_new_int(*((int32_t *)options[i].value)));
|
||||||
} else if (options[i].type == tgbot_opt_string) {
|
break;
|
||||||
|
}
|
||||||
|
case tgbot_opt_string: {
|
||||||
if (!options[i].value) {
|
if (!options[i].value) {
|
||||||
continue;
|
break;
|
||||||
}
|
}
|
||||||
json_object_object_add(rjson, options[i].key, json_object_new_string((char *)options[i].value));
|
json_object_object_add(rjson, options[i].key, json_object_new_string((char *)options[i].value));
|
||||||
} else if (options[i].type == tgbot_opt_int64) {
|
break;
|
||||||
|
}
|
||||||
|
case tgbot_opt_int64: {
|
||||||
json_object_object_add(rjson, options[i].key, json_object_new_int64(*((int64_t *)options[i].value)));
|
json_object_object_add(rjson, options[i].key, json_object_new_int64(*((int64_t *)options[i].value)));
|
||||||
} else if (options[i].type == tgbot_opt_inlinekeyboard) {
|
break;
|
||||||
|
}
|
||||||
|
case tgbot_opt_inlinekeyboard: {
|
||||||
if (options[i].value != NULL) {
|
if (options[i].value != NULL) {
|
||||||
json_object *reply_markup = json_ikb_new((tgbot_inlinekeyboard_s *)options[i].value);
|
json_object *reply_markup = json_ikb_new((tgbot_inlinekeyboard_s *)options[i].value);
|
||||||
json_object_object_add(rjson, "reply_markup", reply_markup);
|
json_object_object_add(rjson, options[i].key, reply_markup);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case tgbot_opt_null: {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
} else if (options[i].type == tgbot_opt_null) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,7 +44,15 @@ json_object *json_builder(tgbot_json_option *options, size_t optionslen) {
|
|||||||
|
|
||||||
json_object *json_ikb_new(tgbot_inlinekeyboard_s *keyboard) {
|
json_object *json_ikb_new(tgbot_inlinekeyboard_s *keyboard) {
|
||||||
json_object *reply_markup = json_object_new_object();
|
json_object *reply_markup = json_object_new_object();
|
||||||
|
if (!reply_markup) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
json_object *inline_keyboard_array = json_object_new_array();
|
json_object *inline_keyboard_array = json_object_new_array();
|
||||||
|
if (!inline_keyboard_array) {
|
||||||
|
json_object_put(reply_markup);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < keyboard->rows; ++i) {
|
for (size_t i = 0; i < keyboard->rows; ++i) {
|
||||||
json_object *row = json_object_new_array();
|
json_object *row = json_object_new_array();
|
||||||
@@ -41,6 +63,10 @@ json_object *json_ikb_new(tgbot_inlinekeyboard_s *keyboard) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
json_object *button = json_object_new_object();
|
json_object *button = json_object_new_object();
|
||||||
|
if (!button) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
json_object_object_add(button, "text", json_object_new_string(kbbutton->text));
|
json_object_object_add(button, "text", json_object_new_string(kbbutton->text));
|
||||||
json_object_object_add(button, "url", json_object_new_string(kbbutton->url));
|
json_object_object_add(button, "url", json_object_new_string(kbbutton->url));
|
||||||
json_object_object_add(button, "callback_data", json_object_new_string(kbbutton->callback_data));
|
json_object_object_add(button, "callback_data", json_object_new_string(kbbutton->callback_data));
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
sources += files(
|
sources += files(
|
||||||
'json.c',
|
'json.c',
|
||||||
'methods.c',
|
'methods.c',
|
||||||
|
'parse.c',
|
||||||
'tgbot.c',
|
'tgbot.c',
|
||||||
'types.c',
|
'types.c',
|
||||||
)
|
)
|
||||||
312
src/methods.c
312
src/methods.c
@@ -1,5 +1,4 @@
|
|||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
#include <curl/easy.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@@ -7,6 +6,9 @@
|
|||||||
#include "json.h"
|
#include "json.h"
|
||||||
#include "json_object.h"
|
#include "json_object.h"
|
||||||
#include "methods.h"
|
#include "methods.h"
|
||||||
|
#include "parse.h"
|
||||||
|
|
||||||
|
#define opt_size(arr) (sizeof(arr) / sizeof(arr[0]))
|
||||||
|
|
||||||
static size_t write_callback(void *ptr, size_t size, size_t nmemb, char *userdata) {
|
static size_t write_callback(void *ptr, size_t size, size_t nmemb, char *userdata) {
|
||||||
size_t real_size = size * nmemb;
|
size_t real_size = size * nmemb;
|
||||||
@@ -31,168 +33,7 @@ static size_t discard_callback(char *ptr, size_t size, size_t nmemb, void *userd
|
|||||||
return size * nmemb;
|
return size * nmemb;
|
||||||
}
|
}
|
||||||
|
|
||||||
tgbot_rc tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_handler) {
|
static tgbot_rc tgbot_request(const char *url, struct memory_buffer **mb, json_object *json) {
|
||||||
char url[1024];
|
|
||||||
|
|
||||||
memset(update, 0, sizeof(tgbot_update_s));
|
|
||||||
|
|
||||||
int limit = 1;
|
|
||||||
int timeout = 30;
|
|
||||||
tgbot_json_option options[3] = {
|
|
||||||
{"offset", &bot->offset, tgbot_opt_int64},
|
|
||||||
{"limit", &limit, tgbot_opt_int},
|
|
||||||
{"timeout", &timeout, tgbot_opt_int},
|
|
||||||
};
|
|
||||||
json_object *rjson = json_builder(options, 3);
|
|
||||||
|
|
||||||
snprintf(url, sizeof(url), "%sgetUpdates", bot->api);
|
|
||||||
|
|
||||||
struct memory_buffer *mb;
|
|
||||||
tgbot_rc ret = tgbot_request(url, &mb, rjson);
|
|
||||||
json_object_put(rjson);
|
|
||||||
if (ret != TGBOT_OK) {
|
|
||||||
if (mb) {
|
|
||||||
free(mb->data);
|
|
||||||
free(mb);
|
|
||||||
}
|
|
||||||
|
|
||||||
return TGBOT_GETUPDATES_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
json_object *json = json_tokener_parse(mb->data);
|
|
||||||
free(mb->data);
|
|
||||||
free(mb);
|
|
||||||
|
|
||||||
json_object *ok = json_object_object_get(json, "ok");
|
|
||||||
if (!json_object_is_type(ok, json_type_boolean) || !json_object_get_boolean(ok)) {
|
|
||||||
json_object_put(json);
|
|
||||||
|
|
||||||
return TGBOT_TELEGRAM_OK_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
json_object *results = json_object_object_get(json, "result");
|
|
||||||
size_t results_len = json_object_array_length(results);
|
|
||||||
|
|
||||||
if (results_len == 0) {
|
|
||||||
json_object_put(json);
|
|
||||||
|
|
||||||
return TGBOT_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Check if it is a Message or a CallbackQuery*/
|
|
||||||
json_object *result = json_object_array_get_idx(results, 0);
|
|
||||||
json_object *message = json_object_object_get(result, "message");
|
|
||||||
if (message) {
|
|
||||||
tgbot_parse_message(bot, update, result);
|
|
||||||
} else if (cbq_handler != NULL) {
|
|
||||||
tgbot_cbquery_s query;
|
|
||||||
tgbot_parse_cbquery(bot, &query, result, cbq_handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
json_object_put(json);
|
|
||||||
|
|
||||||
return TGBOT_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
tgbot_rc tgbot_parse_message(tgbot_s *bot, tgbot_update_s *update, json_object *result) {
|
|
||||||
json_object *update_id = json_object_object_get(result, "update_id");
|
|
||||||
bot->offset = json_object_get_int(update_id) + 1;
|
|
||||||
update->update_id = json_object_get_int(update_id);
|
|
||||||
|
|
||||||
json_object *message = json_object_object_get(result, "message");
|
|
||||||
json_object *message_id = json_object_object_get(message, "message_id");
|
|
||||||
if (message_id) {
|
|
||||||
update->message_id = json_object_get_int(message_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
json_object *chat = json_object_object_get(message, "chat");
|
|
||||||
json_object *chat_id = json_object_object_get(chat, "id");
|
|
||||||
if (chat_id) {
|
|
||||||
update->chat_id = json_object_get_int64(chat_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
json_object *chat_first_name = json_object_object_get(chat, "first_name");
|
|
||||||
if (chat_first_name) {
|
|
||||||
strncpy(update->chat_first_name, json_object_get_string(chat_first_name), sizeof(update->chat_first_name) - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
json_object *chat_last_name = json_object_object_get(chat, "last_name");
|
|
||||||
if (chat_last_name) {
|
|
||||||
strncpy(update->chat_last_name, json_object_get_string(chat_last_name), sizeof(update->chat_last_name) - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
json_object *chat_username = json_object_object_get(chat, "username");
|
|
||||||
if (chat_username != NULL) {
|
|
||||||
strncpy(update->chat_username, json_object_get_string(chat_username), sizeof(update->chat_username) - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
json_object *chat_type = json_object_object_get(chat, "type");
|
|
||||||
if (chat_type) {
|
|
||||||
strncpy(update->chat_type, json_object_get_string(chat_type), sizeof(update->chat_type) - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
json_object *date = json_object_object_get(message, "date");
|
|
||||||
if (date) {
|
|
||||||
update->date = json_object_get_int(date);
|
|
||||||
}
|
|
||||||
|
|
||||||
json_object *text = json_object_object_get(message, "text");
|
|
||||||
if (text) {
|
|
||||||
snprintf(update->text, sizeof(update->text), "%s", json_object_get_string(text));
|
|
||||||
}
|
|
||||||
|
|
||||||
return TGBOT_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
tgbot_rc tgbot_parse_cbquery(tgbot_s *bot, tgbot_cbquery_s *query, json_object *result, Callback cbq_handler) {
|
|
||||||
json_object *update_id = json_object_object_get(result, "update_id");
|
|
||||||
bot->offset = json_object_get_int(update_id) + 1;
|
|
||||||
query->update_id = json_object_get_int(update_id);
|
|
||||||
|
|
||||||
json_object *callback_query = json_object_object_get(result, "callback_query");
|
|
||||||
json_object *message = json_object_object_get(callback_query, "message");
|
|
||||||
json_object *message_id = json_object_object_get(message, "message_id");
|
|
||||||
if (message_id) {
|
|
||||||
query->message_id = json_object_get_int(message_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
json_object *chat = json_object_object_get(message, "chat");
|
|
||||||
json_object *chat_id = json_object_object_get(chat, "id");
|
|
||||||
if (chat_id) {
|
|
||||||
query->chat_id = json_object_get_int64(chat_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
json_object *chat_username = json_object_object_get(chat, "username");
|
|
||||||
if (chat_username) {
|
|
||||||
strncpy(query->chat_username, json_object_get_string(chat_username), sizeof(query->chat_username) - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
json_object *date = json_object_object_get(message, "date");
|
|
||||||
if (date) {
|
|
||||||
query->date = json_object_get_int(date);
|
|
||||||
}
|
|
||||||
|
|
||||||
json_object *text = json_object_object_get(message, "text");
|
|
||||||
if (text) {
|
|
||||||
strncpy(query->text, json_object_get_string(text), sizeof(query->text) - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
json_object *chat_instance = json_object_object_get(callback_query, "chat_instance");
|
|
||||||
if (chat_instance) {
|
|
||||||
strncpy(query->chat_instance, json_object_get_string(chat_instance), sizeof(query->chat_instance) - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
json_object *data = json_object_object_get(callback_query, "data");
|
|
||||||
if (data) {
|
|
||||||
strncpy(query->data, json_object_get_string(data), sizeof(query->data) - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
cbq_handler(bot, query);
|
|
||||||
|
|
||||||
return TGBOT_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
tgbot_rc tgbot_request(const char *url, struct memory_buffer **mb, json_object *json) {
|
|
||||||
CURL *curl = curl_easy_init();
|
CURL *curl = curl_easy_init();
|
||||||
if (!curl) {
|
if (!curl) {
|
||||||
return TGBOT_REQUEST_ERROR;
|
return TGBOT_REQUEST_ERROR;
|
||||||
@@ -244,8 +85,82 @@ tgbot_rc tgbot_request(const char *url, struct memory_buffer **mb, json_object *
|
|||||||
return TGBOT_OK;
|
return TGBOT_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
tgbot_rc tgbot_get_me(tgbot_s *bot, tgbot_me_s *me) {
|
static tgbot_rc tgbot_execute_method(const tgbot_s *bot, const char *method, tgbot_option_s *options, size_t optlen) {
|
||||||
char url[1024];
|
char url[URL_LEN] = {0};
|
||||||
|
snprintf(url, sizeof(url), "%s%s", bot->api, method);
|
||||||
|
|
||||||
|
json_object *rjson = json_builder(options, optlen);
|
||||||
|
tgbot_rc ret = tgbot_request(url, NULL, rjson);
|
||||||
|
json_object_put(rjson);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
tgbot_rc tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_handler) {
|
||||||
|
char url[URL_LEN];
|
||||||
|
|
||||||
|
memset(update, 0, sizeof(tgbot_update_s));
|
||||||
|
|
||||||
|
int limit = 1;
|
||||||
|
int timeout = 30;
|
||||||
|
tgbot_option_s options[3] = {
|
||||||
|
{"offset", &bot->offset, tgbot_opt_int64},
|
||||||
|
{"limit", &limit, tgbot_opt_int},
|
||||||
|
{"timeout", &timeout, tgbot_opt_int},
|
||||||
|
};
|
||||||
|
json_object *rjson = json_builder(options, 3);
|
||||||
|
|
||||||
|
snprintf(url, sizeof(url), "%sgetUpdates", bot->api);
|
||||||
|
|
||||||
|
struct memory_buffer *mb;
|
||||||
|
tgbot_rc ret = tgbot_request(url, &mb, rjson);
|
||||||
|
json_object_put(rjson);
|
||||||
|
if (ret != TGBOT_OK) {
|
||||||
|
if (mb) {
|
||||||
|
free(mb->data);
|
||||||
|
free(mb);
|
||||||
|
}
|
||||||
|
|
||||||
|
return TGBOT_GETUPDATES_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
json_object *json = json_tokener_parse(mb->data);
|
||||||
|
free(mb->data);
|
||||||
|
free(mb);
|
||||||
|
|
||||||
|
const json_object *ok = json_object_object_get(json, "ok");
|
||||||
|
if (!json_object_is_type(ok, json_type_boolean) || !json_object_get_boolean(ok)) {
|
||||||
|
json_object_put(json);
|
||||||
|
|
||||||
|
return TGBOT_TELEGRAM_OK_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
const json_object *results = json_object_object_get(json, "result");
|
||||||
|
size_t results_len = json_object_array_length(results);
|
||||||
|
|
||||||
|
if (results_len == 0) {
|
||||||
|
json_object_put(json);
|
||||||
|
|
||||||
|
return TGBOT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check if it is a Message or a CallbackQuery*/
|
||||||
|
json_object *result = json_object_array_get_idx(results, 0);
|
||||||
|
const json_object *message = json_object_object_get(result, "message");
|
||||||
|
if (message) {
|
||||||
|
tgbot_parse_message(bot, update, result);
|
||||||
|
} else if (cbq_handler != NULL) {
|
||||||
|
tgbot_cbquery_s query;
|
||||||
|
tgbot_parse_cbquery(bot, &query, result, cbq_handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
json_object_put(json);
|
||||||
|
|
||||||
|
return TGBOT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
tgbot_rc tgbot_get_me(const tgbot_s *bot, tgbot_me_s *me) {
|
||||||
|
char url[URL_LEN];
|
||||||
snprintf(url, sizeof(url), "%sgetMe", bot->api);
|
snprintf(url, sizeof(url), "%sgetMe", bot->api);
|
||||||
|
|
||||||
struct memory_buffer *mb;
|
struct memory_buffer *mb;
|
||||||
@@ -261,89 +176,58 @@ tgbot_rc tgbot_get_me(tgbot_s *bot, tgbot_me_s *me) {
|
|||||||
free(mb->data);
|
free(mb->data);
|
||||||
free(mb);
|
free(mb);
|
||||||
|
|
||||||
json_object *ok = json_object_object_get(json, "ok");
|
const json_object *ok = json_object_object_get(json, "ok");
|
||||||
if (!json_object_get_boolean(ok)) {
|
if (!json_object_get_boolean(ok)) {
|
||||||
json_object_put(json);
|
json_object_put(json);
|
||||||
|
|
||||||
return TGBOT_GETME_ERROR;
|
return TGBOT_GETME_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
json_object *result = json_object_object_get(json, "result");
|
const json_object *result = json_object_object_get(json, "result");
|
||||||
json_object *first_name = json_object_object_get(result, "first_name");
|
json_object *first_name = json_object_object_get(result, "first_name");
|
||||||
strncpy(me->first_name, json_object_get_string(first_name), sizeof(me->first_name) - 1);
|
snprintf(me->first_name, sizeof(me->first_name), "%s", json_object_get_string(first_name));
|
||||||
|
|
||||||
json_object *username = json_object_object_get(result, "username");
|
json_object *username = json_object_object_get(result, "username");
|
||||||
strncpy(me->username, json_object_get_string(username), sizeof(me->username) - 1);
|
snprintf(me->username, sizeof(me->username), "%s", json_object_get_string(username));
|
||||||
|
|
||||||
json_object_put(json);
|
json_object_put(json);
|
||||||
|
|
||||||
return TGBOT_OK;
|
return TGBOT_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
tgbot_rc tgbot_send_message(tgbot_s *bot, int64_t chat_id, const char *text, const char *parse_mode,
|
tgbot_rc tgbot_send_message(const tgbot_s *bot, int64_t chat_id, const char *text, const char *parse_mode,
|
||||||
tgbot_inlinekeyboard_s *keyboard) {
|
tgbot_inlinekeyboard_s *keyboard) {
|
||||||
char url[1024];
|
tgbot_option_s options[4] = {
|
||||||
|
|
||||||
tgbot_json_option options[4] = {
|
|
||||||
{"chat_id", &chat_id, tgbot_opt_int64},
|
{"chat_id", &chat_id, tgbot_opt_int64},
|
||||||
{"text", (void *)text, tgbot_opt_string},
|
{"text", (void *)text, tgbot_opt_string},
|
||||||
{"parse_mode", (void *)parse_mode, tgbot_opt_string},
|
{"parse_mode", (void *)parse_mode, tgbot_opt_string},
|
||||||
{"reply_markup", keyboard, tgbot_opt_inlinekeyboard},
|
{"reply_markup", keyboard, tgbot_opt_inlinekeyboard},
|
||||||
};
|
};
|
||||||
json_object *rjson = json_builder(options, 4);
|
|
||||||
|
|
||||||
snprintf(url, sizeof(url), "%ssendMessage", bot->api);
|
return tgbot_execute_method(bot, "sendMessage", options, opt_size(options)) == TGBOT_OK ? TGBOT_OK
|
||||||
|
: TGBOT_SENDMESSAGE_ERROR;
|
||||||
tgbot_rc ret = tgbot_request(url, NULL, rjson);
|
|
||||||
json_object_put(rjson);
|
|
||||||
|
|
||||||
if (ret != TGBOT_OK) {
|
|
||||||
return TGBOT_SENDMESSAGE_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
return TGBOT_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tgbot_rc tgbot_edit_message_text(tgbot_s *bot, int64_t chat_id, long message_id, const char *text,
|
tgbot_rc tgbot_edit_message_text(const tgbot_s *bot, int64_t chat_id, long message_id, const char *text,
|
||||||
tgbot_inlinekeyboard_s *keyboard) {
|
tgbot_inlinekeyboard_s *keyboard) {
|
||||||
char url[1024];
|
tgbot_option_s options[4] = {
|
||||||
|
|
||||||
tgbot_json_option options[4] = {
|
|
||||||
{"chat_id", &chat_id, tgbot_opt_int64},
|
{"chat_id", &chat_id, tgbot_opt_int64},
|
||||||
{"message_id", &message_id, tgbot_opt_int},
|
{"message_id", &message_id, tgbot_opt_int},
|
||||||
{"text", (void *)text, tgbot_opt_string},
|
{"text", (void *)text, tgbot_opt_string},
|
||||||
{"reply_markup", keyboard, tgbot_opt_inlinekeyboard},
|
{"reply_markup", keyboard, tgbot_opt_inlinekeyboard},
|
||||||
};
|
};
|
||||||
json_object *rjson = json_builder(options, 4);
|
|
||||||
|
|
||||||
snprintf(url, sizeof(url), "%seditMessageText", bot->api);
|
return tgbot_execute_method(bot, "editMessageText", options, opt_size(options)) == TGBOT_OK
|
||||||
|
? TGBOT_OK
|
||||||
tgbot_rc ret = tgbot_request(url, NULL, rjson);
|
: TGBOT_EDITMESSAGETEXT_ERROR;
|
||||||
json_object_put(rjson);
|
|
||||||
|
|
||||||
if (ret != TGBOT_OK) {
|
|
||||||
return TGBOT_EDITMESSAGETEXT_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
return TGBOT_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tgbot_rc tgbot_send_dice(tgbot_s *bot, int64_t chat_id, const char *emoji) {
|
tgbot_rc tgbot_send_dice(const tgbot_s *bot, int64_t chat_id, const char *emoji) {
|
||||||
char url[1024];
|
tgbot_option_s options[2] = {
|
||||||
|
|
||||||
tgbot_json_option options[2] = {
|
|
||||||
{"chat_id", &chat_id, tgbot_opt_int64},
|
{"chat_id", &chat_id, tgbot_opt_int64},
|
||||||
{"emoji", (void *)emoji, tgbot_opt_string},
|
{"emoji", (void *)emoji, tgbot_opt_string},
|
||||||
};
|
};
|
||||||
json_object *rjson = json_builder(options, 2);
|
|
||||||
|
|
||||||
snprintf(url, sizeof(url), "%ssendDice", bot->api);
|
return tgbot_execute_method(bot, "sendDice", options, opt_size(options)) == TGBOT_OK ? TGBOT_OK
|
||||||
|
: TGBOT_SENDDICE_ERROR;
|
||||||
tgbot_rc ret = tgbot_request(url, NULL, rjson);
|
|
||||||
json_object_put(rjson);
|
|
||||||
|
|
||||||
if (ret != TGBOT_OK) {
|
|
||||||
return TGBOT_SENDDICE_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
return TGBOT_OK;
|
|
||||||
}
|
}
|
||||||
|
|||||||
100
src/parse.c
Normal file
100
src/parse.c
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
#include "parse.h"
|
||||||
|
|
||||||
|
tgbot_rc tgbot_parse_message(tgbot_s *bot, tgbot_update_s *update, json_object *result) {
|
||||||
|
json_object *update_id = json_object_object_get(result, "update_id");
|
||||||
|
bot->offset = json_object_get_int(update_id) + 1;
|
||||||
|
update->update_id = json_object_get_int(update_id);
|
||||||
|
|
||||||
|
json_object *message = json_object_object_get(result, "message");
|
||||||
|
json_object *message_id = json_object_object_get(message, "message_id");
|
||||||
|
if (message_id) {
|
||||||
|
update->message_id = json_object_get_int(message_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
json_object *chat = json_object_object_get(message, "chat");
|
||||||
|
json_object *chat_id = json_object_object_get(chat, "id");
|
||||||
|
if (chat_id) {
|
||||||
|
update->chat_id = json_object_get_int64(chat_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
json_object *chat_first_name = json_object_object_get(chat, "first_name");
|
||||||
|
if (chat_first_name) {
|
||||||
|
snprintf(update->chat_first_name, sizeof(update->chat_first_name), "%s",
|
||||||
|
json_object_get_string(chat_first_name));
|
||||||
|
}
|
||||||
|
|
||||||
|
json_object *chat_last_name = json_object_object_get(chat, "last_name");
|
||||||
|
if (chat_last_name) {
|
||||||
|
snprintf(update->chat_last_name, sizeof(update->chat_last_name), "%s", json_object_get_string(chat_last_name));
|
||||||
|
}
|
||||||
|
|
||||||
|
json_object *chat_username = json_object_object_get(chat, "username");
|
||||||
|
if (chat_username != NULL) {
|
||||||
|
snprintf(update->chat_username, sizeof(update->chat_username), "%s", json_object_get_string(chat_username));
|
||||||
|
}
|
||||||
|
|
||||||
|
json_object *chat_type = json_object_object_get(chat, "type");
|
||||||
|
if (chat_type) {
|
||||||
|
snprintf(update->chat_type, sizeof(update->chat_type), "%s", json_object_get_string(chat_type));
|
||||||
|
}
|
||||||
|
|
||||||
|
json_object *date = json_object_object_get(message, "date");
|
||||||
|
if (date) {
|
||||||
|
update->date = json_object_get_int(date);
|
||||||
|
}
|
||||||
|
|
||||||
|
json_object *text = json_object_object_get(message, "text");
|
||||||
|
if (text) {
|
||||||
|
snprintf(update->text, sizeof(update->text), "%s", json_object_get_string(text));
|
||||||
|
}
|
||||||
|
|
||||||
|
return TGBOT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
tgbot_rc tgbot_parse_cbquery(tgbot_s *bot, tgbot_cbquery_s *query, json_object *result, Callback cbq_handler) {
|
||||||
|
json_object *update_id = json_object_object_get(result, "update_id");
|
||||||
|
bot->offset = json_object_get_int(update_id) + 1;
|
||||||
|
query->update_id = json_object_get_int(update_id);
|
||||||
|
|
||||||
|
json_object *callback_query = json_object_object_get(result, "callback_query");
|
||||||
|
json_object *message = json_object_object_get(callback_query, "message");
|
||||||
|
json_object *message_id = json_object_object_get(message, "message_id");
|
||||||
|
if (message_id) {
|
||||||
|
query->message_id = json_object_get_int(message_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
json_object *chat = json_object_object_get(message, "chat");
|
||||||
|
json_object *chat_id = json_object_object_get(chat, "id");
|
||||||
|
if (chat_id) {
|
||||||
|
query->chat_id = json_object_get_int64(chat_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
json_object *chat_username = json_object_object_get(chat, "username");
|
||||||
|
if (chat_username) {
|
||||||
|
snprintf(query->chat_username, sizeof(query->chat_username), "%s", json_object_get_string(chat_username));
|
||||||
|
}
|
||||||
|
|
||||||
|
json_object *date = json_object_object_get(message, "date");
|
||||||
|
if (date) {
|
||||||
|
query->date = json_object_get_int(date);
|
||||||
|
}
|
||||||
|
|
||||||
|
json_object *text = json_object_object_get(message, "text");
|
||||||
|
if (text) {
|
||||||
|
snprintf(query->text, sizeof(query->text), "%s", json_object_get_string(text));
|
||||||
|
}
|
||||||
|
|
||||||
|
json_object *chat_instance = json_object_object_get(callback_query, "chat_instance");
|
||||||
|
if (chat_instance) {
|
||||||
|
snprintf(query->chat_instance, sizeof(query->chat_instance), "%s", json_object_get_string(chat_instance));
|
||||||
|
}
|
||||||
|
|
||||||
|
json_object *data = json_object_object_get(callback_query, "data");
|
||||||
|
if (data) {
|
||||||
|
snprintf(query->data, sizeof(query->data), "%s", json_object_get_string(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
cbq_handler(bot, query);
|
||||||
|
|
||||||
|
return TGBOT_OK;
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
#include "tgbot.h"
|
#include "tgbot.h"
|
||||||
|
|
||||||
tgbot_s *tgbot_new(char *token) {
|
tgbot_s *tgbot_new(const char *token) {
|
||||||
tgbot_s *bot = malloc(sizeof(tgbot_s));
|
tgbot_s *bot = malloc(sizeof(tgbot_s));
|
||||||
if (!bot) {
|
if (!bot) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
Reference in New Issue
Block a user