From bb313046145048248031222021f97fd596acd828 Mon Sep 17 00:00:00 2001 From: Francesco Date: Tue, 17 Mar 2026 00:30:38 +0100 Subject: [PATCH] refactor: move common to internal and update public api --- examples/echobot/echobot.c | 3 +- examples/inlinekeyboard/inlinekeyboard.c | 14 ++- include/common.h | 109 ---------------- include/methods.h | 14 +-- include/parse.h | 11 -- include/tg_types.h | 49 ++++++++ include/tgbot.h | 5 +- include/types.h | 154 ++++++++++++----------- meson.build | 47 ++++--- src/internal/common.h | 45 +++++++ {include => src/internal}/json.h | 6 +- src/internal/parse.h | 11 ++ src/json.c | 4 +- src/methods.c | 66 +++++----- src/parse.c | 11 +- src/tgbot.c | 1 + src/types.c | 6 +- 17 files changed, 284 insertions(+), 272 deletions(-) delete mode 100644 include/common.h delete mode 100644 include/parse.h create mode 100644 include/tg_types.h create mode 100644 src/internal/common.h rename {include => src/internal}/json.h (61%) create mode 100644 src/internal/parse.h diff --git a/examples/echobot/echobot.c b/examples/echobot/echobot.c index 6e0b16f..08bf854 100644 --- a/examples/echobot/echobot.c +++ b/examples/echobot/echobot.c @@ -3,7 +3,8 @@ #include #include -#include +#include +#include bool run = true; diff --git a/examples/inlinekeyboard/inlinekeyboard.c b/examples/inlinekeyboard/inlinekeyboard.c index 0fb85d9..189ff06 100644 --- a/examples/inlinekeyboard/inlinekeyboard.c +++ b/examples/inlinekeyboard/inlinekeyboard.c @@ -4,7 +4,9 @@ #include #include -#include +#include +#include +#include #define WELCOME_MSG "Hi there! This bot is coded in C." @@ -40,12 +42,12 @@ void sighandler(int signum) { } void parse_command(const tgbot_s *bot, const tgbot_update_s *update) { - tgbot_rc ret; + int ret; if (strcmp("/start", update->text) == 0) { ret = tgbot_send_message(bot, update->chat_id, WELCOME_MSG, "Markdown", keyboard); - if (ret != TGBOT_OK) { + if (ret != 0) { fprintf(stderr, "Failed to send message\n"); } @@ -54,7 +56,7 @@ void parse_command(const tgbot_s *bot, const tgbot_update_s *update) { /* Echo the message */ ret = tgbot_send_message(bot, update->chat_id, update->text, "Markdown", NULL); - if (ret != TGBOT_OK) { + if (ret != 0) { fprintf(stderr, "Failed to send message\n"); } } @@ -100,8 +102,8 @@ int main(void) { /* Main loop */ while (run) { - tgbot_rc ret = tgbot_get_update(bot, &update, callback_handler); - if (ret != TGBOT_OK) { + int ret = tgbot_get_update(bot, &update, callback_handler); + if (ret != 0) { fprintf(stderr, "tgbot_get_updates()\n"); continue; } diff --git a/include/common.h b/include/common.h deleted file mode 100644 index 1091560..0000000 --- a/include/common.h +++ /dev/null @@ -1,109 +0,0 @@ -#ifndef TGBOT_COMMON_H -#define TGBOT_COMMON_H - -#include -#include - -#define TOKEN_SIZE 128 -#define API_SIZE 512 -#define URL_LEN 1024 - -#define FIRSTNAME_SIZE 256 -#define USERNAME_SIZE 32 - -#define CHAT_LASTNAME_SIZE 256 -#define CHAT_TYPE_SIZE 32 -#define CHAT_TEXT_SIZE 4096 - -/** - * @brief A structure used to get curl response. - */ -struct memory_buffer { - char *data; - size_t size; -}; - -/** - * @brief A structure to represent bot object. - */ -typedef struct tgbot { - char token[TOKEN_SIZE]; /**< Bot token. */ - char api[API_SIZE]; /**< Bot API url. */ - int32_t offset; /**< Bot offset. */ -} tgbot_s; - -/** - * @brief A structure to represent Bot information got from getMe API. - */ -typedef struct tgbot_me { - char first_name[FIRSTNAME_SIZE]; /**< Bot's first name. */ - char username[USERNAME_SIZE]; /**< Bot's username. */ -} tgbot_me_s; - -/** - * @brief A structure to represent Update object. - */ -typedef struct tgbot_update { - int64_t update_id; /**< Update id. */ - long message_id; /**< Message id. */ - int64_t chat_id; /**< Chat id. */ - char chat_first_name[FIRSTNAME_SIZE]; /**< Chat first name. */ - char chat_last_name[CHAT_LASTNAME_SIZE]; /**< Chat last name. */ - char chat_username[USERNAME_SIZE]; /**< Chat username. */ - char chat_type[CHAT_TYPE_SIZE]; /**< Chat type (private/public). */ - int32_t date; /**< Date in unix timestamp. */ - char text[CHAT_TEXT_SIZE]; /**< Message text. */ -} tgbot_update_s; - -/** - * @brief A structure to represent CallbackQuery object. - */ -typedef struct tgbot_cbquery { - int64_t update_id; - long message_id; - int64_t chat_id; - char chat_username[USERNAME_SIZE]; - int32_t date; - char text[CHAT_TEXT_SIZE]; - char chat_instance[128]; - char data[64]; /**> Callback data. */ -} tgbot_cbquery_s; - -/** - * @brief Callback function pointer. - */ -typedef void (*Callback)(tgbot_s *bot, tgbot_cbquery_s *query); - -/** - * @brief An enum to represent error codes. - */ -enum tgbot_rc { - TGBOT_OK = 0, - TGBOT_INIT_ERROR, - TGBOT_REQUEST_ERROR, - TGBOT_GETUPDATES_ERROR, - TGBOT_GETME_ERROR, - TGBOT_SENDMESSAGE_ERROR, - TGBOT_EDITMESSAGETEXT_ERROR, - TGBOT_SENDDICE_ERROR, - TGBOT_TELEGRAM_OK_ERROR, -}; -typedef enum tgbot_rc tgbot_rc; - -enum tgbot_opt_type { - tgbot_opt_int, - tgbot_opt_int64, - tgbot_opt_string, - tgbot_opt_inlinekeyboard, - tgbot_opt_null, -}; -typedef enum tgbot_opt_type tgbot_opt_type_e; - -struct tgbot_option { - char key[32]; - void *value; - tgbot_opt_type_e type; -}; -typedef struct tgbot_option tgbot_option_s; - -#endif diff --git a/include/methods.h b/include/methods.h index 9b147b6..622c4ef 100644 --- a/include/methods.h +++ b/include/methods.h @@ -5,16 +5,16 @@ #include /* Retrieve update */ -tgbot_rc tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_handler); +int tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_handler); /* Methods */ -tgbot_rc tgbot_get_me(const tgbot_s *bot, tgbot_me_s *me); -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_rc tgbot_send_dice(const tgbot_s *bot, int64_t chat_id, const char *emoji); +int tgbot_get_me(const tgbot_s *bot, tgbot_me_s *me); +int tgbot_send_message(const tgbot_s *bot, int64_t chat_id, const char *text, const char *parse_mode, + tgbot_inlinekeyboard_s *reply_markup); +int tgbot_send_dice(const tgbot_s *bot, int64_t chat_id, const char *emoji); /* Updating Methods */ -tgbot_rc tgbot_edit_message_text(const tgbot_s *bot, int64_t chat_id, long message_id, const char *text, - tgbot_inlinekeyboard_s *keyboard); +int tgbot_edit_message_text(const tgbot_s *bot, int64_t chat_id, long message_id, const char *text, + tgbot_inlinekeyboard_s *keyboard); #endif diff --git a/include/parse.h b/include/parse.h deleted file mode 100644 index 405115f..0000000 --- a/include/parse.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef TGBOT_PARSE_H -#define TGBOT_PARSE_H - -#include "common.h" -#include - -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 diff --git a/include/tg_types.h b/include/tg_types.h new file mode 100644 index 0000000..9a9c086 --- /dev/null +++ b/include/tg_types.h @@ -0,0 +1,49 @@ +#ifndef TGBOT_TG_TYPES_H +#define TGBOT_TG_TYPES_H + +#include "types.h" + +/** + * @brief Allocates a new inline keyboard. + * + * @param[in] rows Number of rows in the keyboard. + * @param[in] columns Number of columns in each row. + * + * @return The pointer to the keyboard or NULL on allocation failure. + */ +tgbot_inlinekeyboard_s *tgbot_inlinekb_new(size_t rows, size_t columns); + +/** + * @brief Adds or updates a button at the specified position in the keyboard. + * + * @param[out] keyboard Pointer to the keyboard to modify. + * @param[in] row Row index of the button (starting from 0). + * @param[in] column Column index of the button (starting from 0). + * @param[in] text Display text for the button. + * @param[in] url Optional URL for the button. + * @param[in] callback_data Optional callback data for the button. + * + * @return 0 on success. + */ +int tgbot_inlinekb_button(tgbot_inlinekeyboard_s *keyboard, size_t row, size_t column, const char *text, + const char *url, const char *callback_data); + +/** + * @brief Returns a pointer to the keyboard's button. + * + * @param[in] keyboard Keyboard. + * @param[in] row Row index of the button. + * @param[in] column Column index of the button. + * + * @return Pointer to the button, or NULL if the position is invalid. + */ +tgbot_inlinekeyboardbutton_s *tgbot_inlinekb_button_at(tgbot_inlinekeyboard_s *keyboard, size_t row, size_t column); + +/** + * @brief Frees all memory associated with the given inline keyboard. + * + * @param[in,out] keyboard Pointer to the keyboard structure to deallocate. + */ +void tgbot_inlinekb_free(tgbot_inlinekeyboard_s *keyboard); + +#endif diff --git a/include/tgbot.h b/include/tgbot.h index 0ca2c64..baad2d5 100644 --- a/include/tgbot.h +++ b/include/tgbot.h @@ -1,9 +1,8 @@ #ifndef TGBOT_MAIN_H #define TGBOT_MAIN_H -#include "common.h" -#include "methods.h" #include "types.h" +#include /** * @brief Create a new Bot object. @@ -21,4 +20,4 @@ tgbot_s *tgbot_new(const char *token); */ void tgbot_free(tgbot_s *bot); -#endif // TGBOT_H +#endif diff --git a/include/types.h b/include/types.h index 2de0b14..96ed899 100644 --- a/include/types.h +++ b/include/types.h @@ -1,71 +1,83 @@ -#ifndef TGBOT_TYPES_H -#define TGBOT_TYPES_H - -#include "common.h" - -#define TEXT_SIZE 200 -#define URL_SIZE 200 -#define CB_DATA_SIZE 64 - -/** - * @brief Represents a single button on an inline keyboard. - */ -typedef struct tgbot_inlinekeyboardbutton { - char text[TEXT_SIZE]; /**< Text of the button. If empty, the button is ignored. */ - char url[URL_SIZE]; /**< (Optional) URL to be opened when the button is pressed. */ - char callback_data[CB_DATA_SIZE]; /**< (Optional) Data sent to the bot when the button is pressed. */ -} tgbot_inlinekeyboardbutton_s; - -/** - * @brief Represents an inline keyboard. - */ -typedef struct tgbot_inlinekeyboard { - size_t rows; /**< Number of rows in the keyboard. */ - size_t columns; /**< Number of columns per row. */ - tgbot_inlinekeyboardbutton_s *buttons; /**< Array of buttons. */ -} tgbot_inlinekeyboard_s; - -/** - * @brief Allocates a new inline keyboard. - * - * @param[in] rows Number of rows in the keyboard. - * @param[in] columns Number of columns in each row. - * - * @return The pointer to the keyboard or NULL on allocation failure. - */ -tgbot_inlinekeyboard_s *tgbot_inlinekb_new(size_t rows, size_t columns); - -/** - * @brief Adds or updates a button at the specified position in the keyboard. - * - * @param[out] keyboard Pointer to the keyboard to modify. - * @param[in] row Row index of the button (starting from 0). - * @param[in] column Column index of the button (starting from 0). - * @param[in] text Display text for the button. - * @param[in] url Optional URL for the button. - * @param[in] callback_data Optional callback data for the button. - * - * @return TGBOT_OK on success. - */ -tgbot_rc tgbot_inlinekb_button(tgbot_inlinekeyboard_s *keyboard, size_t row, size_t column, const char *text, - const char *url, const char *callback_data); - -/** - * @brief Returns a pointer to the keyboard's button. - * - * @param[in] keyboard Keyboard. - * @param[in] row Row index of the button. - * @param[in] column Column index of the button. - * - * @return Pointer to the button, or NULL if the position is invalid. - */ -tgbot_inlinekeyboardbutton_s *tgbot_inlinekb_button_at(tgbot_inlinekeyboard_s *keyboard, size_t row, size_t column); - -/** - * @brief Frees all memory associated with the given inline keyboard. - * - * @param[in,out] keyboard Pointer to the keyboard structure to deallocate. - */ -void tgbot_inlinekb_free(tgbot_inlinekeyboard_s *keyboard); - -#endif +#ifndef TGBOT_TYPES_H +#define TGBOT_TYPES_H + +#include +#include + +#define FIRSTNAME_SIZE 256 +#define USERNAME_SIZE 32 + +#define CHAT_LASTNAME_SIZE 256 +#define CHAT_TYPE_SIZE 32 +#define CHAT_TEXT_SIZE 4096 + +/** + * @brief Telegram bot struct. + */ +typedef struct tgbot tgbot_s; + +/** + * @brief A structure to represent Bot information got from getMe API. + */ +typedef struct tgbot_me { + char first_name[FIRSTNAME_SIZE]; /**< Bot's first name. */ + char username[USERNAME_SIZE]; /**< Bot's username. */ +} tgbot_me_s; + +/** + * @brief A structure to represent Update object. + */ +typedef struct tgbot_update { + int64_t update_id; /**< Update id. */ + long message_id; /**< Message id. */ + int64_t chat_id; /**< Chat id. */ + char chat_first_name[FIRSTNAME_SIZE]; /**< Chat first name. */ + char chat_last_name[CHAT_LASTNAME_SIZE]; /**< Chat last name. */ + char chat_username[USERNAME_SIZE]; /**< Chat username. */ + char chat_type[CHAT_TYPE_SIZE]; /**< Chat type (private/public). */ + int32_t date; /**< Date in unix timestamp. */ + char text[CHAT_TEXT_SIZE]; /**< Message text. */ +} tgbot_update_s; + +/** + * @brief A structure to represent CallbackQuery object. + */ +typedef struct tgbot_cbquery { + int64_t update_id; + long message_id; + int64_t chat_id; + char chat_username[USERNAME_SIZE]; + int32_t date; + char text[CHAT_TEXT_SIZE]; + char chat_instance[128]; + char data[64]; /**> Callback data. */ +} tgbot_cbquery_s; + +/** + * @brief Callback function pointer. + */ +typedef void (*Callback)(tgbot_s *bot, tgbot_cbquery_s *query); + +#define TEXT_SIZE 200 +#define URL_SIZE 200 +#define CB_DATA_SIZE 64 + +/** + * @brief Represents a single button on an inline keyboard. + */ +typedef struct tgbot_inlinekeyboardbutton { + char text[TEXT_SIZE]; /**< Text of the button. If empty, the button is ignored. */ + char url[URL_SIZE]; /**< (Optional) URL to be opened when the button is pressed. */ + char callback_data[CB_DATA_SIZE]; /**< (Optional) Data sent to the bot when the button is pressed. */ +} tgbot_inlinekeyboardbutton_s; + +/** + * @brief Represents an inline keyboard. + */ +typedef struct tgbot_inlinekeyboard { + size_t rows; /**< Number of rows in the keyboard. */ + size_t columns; /**< Number of columns per row. */ + tgbot_inlinekeyboardbutton_s *buttons; /**< Array of buttons. */ +} tgbot_inlinekeyboard_s; + +#endif diff --git a/meson.build b/meson.build index d7be8d1..6366616 100644 --- a/meson.build +++ b/meson.build @@ -1,8 +1,8 @@ project( - 'tgbot', - 'c', - version: '0.1', - default_options: ['warning_level=3', 'c_std=c18'], + 'tgbot', + 'c', + version: '0.1', + default_options: ['warning_level=3', 'c_std=c18'], ) sources = [] @@ -15,25 +15,36 @@ json_c_dep = dependency('json-c') deps = [curl_dep, json_c_dep] -install_headers('include/tgbot.h') +install_headers( + [ + 'include/methods.h', + 'include/tg_types.h', + 'include/tgbot.h', + 'include/types.h', + ], + subdir: 'tgbot', +) -tgbot_lib = library( - 'tgbot', - sources, - dependencies: deps, - include_directories: inc_dir, - install: true, +tgbot_lib = static_library( + 'tgbot', + sources, + dependencies: deps, + include_directories: inc_dir, + install: true, ) tgbot_dep = declare_dependency( - link_with: tgbot_lib, - include_directories: inc_dir, - dependencies: deps, + link_with: tgbot_lib, + include_directories: inc_dir, + dependencies: deps, ) # Example -executable( - 'example', - 'examples/inlinekeyboard/inlinekeyboard.c', - dependencies: tgbot_dep, +example = executable( + 'example', + 'examples/inlinekeyboard/inlinekeyboard.c', + dependencies: tgbot_dep, + build_by_default: false, ) + +test('example_inlinekeyboard', example) \ No newline at end of file diff --git a/src/internal/common.h b/src/internal/common.h new file mode 100644 index 0000000..4f0531e --- /dev/null +++ b/src/internal/common.h @@ -0,0 +1,45 @@ +#ifndef TGBOT_INTERNAL_COMMON_H +#define TGBOT_INTERNAL_COMMON_H + +#include "types.h" +#include +#include + +#define TOKEN_SIZE 128 +#define API_SIZE 512 +#define URL_LEN 1024 + +/** + * @brief A structure used to get curl response. + */ +struct memory_buffer { + char *data; + size_t size; +}; + +/** + * @brief A structure to represent bot object. + */ +typedef struct tgbot { + char token[TOKEN_SIZE]; /**< Bot token. */ + char api[API_SIZE]; /**< Bot API url. */ + int32_t offset; /**< Bot offset. */ +} tgbot_s; + +enum tgbot_opt_type { + tgbot_opt_int, + tgbot_opt_int64, + tgbot_opt_string, + tgbot_opt_inlinekeyboard, + tgbot_opt_null, +}; +typedef enum tgbot_opt_type tgbot_opt_type_e; + +struct tgbot_option { + char key[32]; + void *value; + tgbot_opt_type_e type; +}; +typedef struct tgbot_option tgbot_option_s; + +#endif diff --git a/include/json.h b/src/internal/json.h similarity index 61% rename from include/json.h rename to src/internal/json.h index 23ba9a4..5af0d24 100644 --- a/include/json.h +++ b/src/internal/json.h @@ -1,9 +1,11 @@ -#ifndef TGBOT_JSON_H -#define TGBOT_JSON_H +#ifndef TGBOT_INTERNAL_JSON_H +#define TGBOT_INTERNAL_JSON_H #include "types.h" #include +typedef struct tgbot_option tgbot_option_s; + json_object *json_builder(tgbot_option_s *options, size_t optionslen); json_object *json_ikb_new(tgbot_inlinekeyboard_s *keyboard); diff --git a/src/internal/parse.h b/src/internal/parse.h new file mode 100644 index 0000000..5dcbe65 --- /dev/null +++ b/src/internal/parse.h @@ -0,0 +1,11 @@ +#ifndef TGBOT_INTERNAL_PARSE_H +#define TGBOT_INTERNAL_PARSE_H + +#include "types.h" +#include + +int tgbot_parse_message(tgbot_s *bot, tgbot_update_s *update, json_object *result); + +int tgbot_parse_cbquery(tgbot_s *bot, tgbot_cbquery_s *query, json_object *result, Callback cbq_handler); + +#endif diff --git a/src/json.c b/src/json.c index 9ee8c37..f269dcc 100644 --- a/src/json.c +++ b/src/json.c @@ -1,7 +1,9 @@ #include #include -#include "json.h" +#include "internal/common.h" +#include "internal/json.h" +#include "tg_types.h" json_object *json_builder(tgbot_option_s *options, size_t optionslen) { json_object *rjson = json_object_new_object(); diff --git a/src/methods.c b/src/methods.c index 3ee530d..a0579ca 100644 --- a/src/methods.c +++ b/src/methods.c @@ -2,11 +2,11 @@ #include #include -#include "common.h" -#include "json.h" +#include "internal/common.h" +#include "internal/json.h" +#include "internal/parse.h" #include "json_object.h" #include "methods.h" -#include "parse.h" #define opt_size(arr) (sizeof(arr) / sizeof(arr[0])) @@ -33,10 +33,10 @@ static size_t discard_callback(char *ptr, size_t size, size_t nmemb, void *userd return size * nmemb; } -static tgbot_rc tgbot_request(const char *url, struct memory_buffer **mb, json_object *json) { +static int tgbot_request(const char *url, struct memory_buffer **mb, json_object *json) { CURL *curl = curl_easy_init(); if (!curl) { - return TGBOT_REQUEST_ERROR; + return -1; } const char *json_string = NULL; @@ -77,26 +77,26 @@ static tgbot_rc tgbot_request(const char *url, struct memory_buffer **mb, json_o *mb = NULL; } - return TGBOT_REQUEST_ERROR; + return -1; } curl_easy_cleanup(curl); - return TGBOT_OK; + return 0; } -static tgbot_rc tgbot_execute_method(const tgbot_s *bot, const char *method, tgbot_option_s *options, size_t optlen) { +static int tgbot_execute_method(const tgbot_s *bot, const char *method, tgbot_option_s *options, size_t optlen) { 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); + int 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) { +int tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_handler) { char url[URL_LEN]; memset(update, 0, sizeof(tgbot_update_s)); @@ -112,16 +112,16 @@ tgbot_rc tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_han snprintf(url, sizeof(url), "%sgetUpdates", bot->api); - struct memory_buffer *mb; - tgbot_rc ret = tgbot_request(url, &mb, rjson); + struct memory_buffer *mb = {0}; + int ret = tgbot_request(url, &mb, rjson); json_object_put(rjson); - if (ret != TGBOT_OK) { + if (ret != 0) { if (mb) { free(mb->data); free(mb); } - return TGBOT_GETUPDATES_ERROR; + return -1; } json_object *json = json_tokener_parse(mb->data); @@ -132,7 +132,7 @@ tgbot_rc tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_han if (!json_object_is_type(ok, json_type_boolean) || !json_object_get_boolean(ok)) { json_object_put(json); - return TGBOT_TELEGRAM_OK_ERROR; + return -1; } const json_object *results = json_object_object_get(json, "result"); @@ -141,7 +141,7 @@ tgbot_rc tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_han if (results_len == 0) { json_object_put(json); - return TGBOT_OK; + return 0; } /* Check if it is a Message or a CallbackQuery*/ @@ -156,20 +156,20 @@ tgbot_rc tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_han json_object_put(json); - return TGBOT_OK; + return 0; } -tgbot_rc tgbot_get_me(const tgbot_s *bot, tgbot_me_s *me) { +int tgbot_get_me(const tgbot_s *bot, tgbot_me_s *me) { char url[URL_LEN]; snprintf(url, sizeof(url), "%sgetMe", bot->api); struct memory_buffer *mb; - tgbot_rc ret = tgbot_request(url, &mb, NULL); - if (ret != TGBOT_OK) { + int ret = tgbot_request(url, &mb, NULL); + if (ret != 0) { free(mb->data); free(mb); - return TGBOT_GETME_ERROR; + return -1; } json_object *json = json_tokener_parse(mb->data); @@ -180,7 +180,7 @@ tgbot_rc tgbot_get_me(const tgbot_s *bot, tgbot_me_s *me) { if (!json_object_get_boolean(ok)) { json_object_put(json); - return TGBOT_GETME_ERROR; + return -1; } const json_object *result = json_object_object_get(json, "result"); @@ -192,11 +192,11 @@ tgbot_rc tgbot_get_me(const tgbot_s *bot, tgbot_me_s *me) { json_object_put(json); - return TGBOT_OK; + return 0; } -tgbot_rc tgbot_send_message(const tgbot_s *bot, int64_t chat_id, const char *text, const char *parse_mode, - tgbot_inlinekeyboard_s *keyboard) { +int tgbot_send_message(const tgbot_s *bot, int64_t chat_id, const char *text, const char *parse_mode, + tgbot_inlinekeyboard_s *keyboard) { tgbot_option_s options[4] = { {"chat_id", &chat_id, tgbot_opt_int64}, {"text", (void *)text, tgbot_opt_string}, @@ -204,12 +204,11 @@ tgbot_rc tgbot_send_message(const tgbot_s *bot, int64_t chat_id, const char *tex {"reply_markup", keyboard, tgbot_opt_inlinekeyboard}, }; - return tgbot_execute_method(bot, "sendMessage", options, opt_size(options)) == TGBOT_OK ? TGBOT_OK - : TGBOT_SENDMESSAGE_ERROR; + return tgbot_execute_method(bot, "sendMessage", options, opt_size(options)); } -tgbot_rc tgbot_edit_message_text(const tgbot_s *bot, int64_t chat_id, long message_id, const char *text, - tgbot_inlinekeyboard_s *keyboard) { +int tgbot_edit_message_text(const tgbot_s *bot, int64_t chat_id, long message_id, const char *text, + tgbot_inlinekeyboard_s *keyboard) { tgbot_option_s options[4] = { {"chat_id", &chat_id, tgbot_opt_int64}, {"message_id", &message_id, tgbot_opt_int}, @@ -217,17 +216,14 @@ tgbot_rc tgbot_edit_message_text(const tgbot_s *bot, int64_t chat_id, long messa {"reply_markup", keyboard, tgbot_opt_inlinekeyboard}, }; - return tgbot_execute_method(bot, "editMessageText", options, opt_size(options)) == TGBOT_OK - ? TGBOT_OK - : TGBOT_EDITMESSAGETEXT_ERROR; + return tgbot_execute_method(bot, "editMessageText", options, opt_size(options)); } -tgbot_rc tgbot_send_dice(const tgbot_s *bot, int64_t chat_id, const char *emoji) { +int tgbot_send_dice(const tgbot_s *bot, int64_t chat_id, const char *emoji) { tgbot_option_s options[2] = { {"chat_id", &chat_id, tgbot_opt_int64}, {"emoji", (void *)emoji, tgbot_opt_string}, }; - return tgbot_execute_method(bot, "sendDice", options, opt_size(options)) == TGBOT_OK ? TGBOT_OK - : TGBOT_SENDDICE_ERROR; + return tgbot_execute_method(bot, "sendDice", options, opt_size(options)); } diff --git a/src/parse.c b/src/parse.c index 9ac61d6..6b77c28 100644 --- a/src/parse.c +++ b/src/parse.c @@ -1,6 +1,7 @@ -#include "parse.h" +#include "internal/parse.h" +#include "internal/common.h" -tgbot_rc tgbot_parse_message(tgbot_s *bot, tgbot_update_s *update, json_object *result) { +int 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); @@ -48,10 +49,10 @@ tgbot_rc tgbot_parse_message(tgbot_s *bot, tgbot_update_s *update, json_object * snprintf(update->text, sizeof(update->text), "%s", json_object_get_string(text)); } - return TGBOT_OK; + return 0; } -tgbot_rc tgbot_parse_cbquery(tgbot_s *bot, tgbot_cbquery_s *query, json_object *result, Callback cbq_handler) { +int 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); @@ -96,5 +97,5 @@ tgbot_rc tgbot_parse_cbquery(tgbot_s *bot, tgbot_cbquery_s *query, json_object * cbq_handler(bot, query); - return TGBOT_OK; + return 0; } diff --git a/src/tgbot.c b/src/tgbot.c index dbdc85d..198ff78 100644 --- a/src/tgbot.c +++ b/src/tgbot.c @@ -4,6 +4,7 @@ #include #include +#include "internal/common.h" #include "tgbot.h" tgbot_s *tgbot_new(const char *token) { diff --git a/src/types.c b/src/types.c index cc741d0..e641a0a 100644 --- a/src/types.c +++ b/src/types.c @@ -32,13 +32,13 @@ tgbot_inlinekeyboardbutton_s *tgbot_inlinekb_button_at(tgbot_inlinekeyboard_s *k return &keyboard->buttons[row * keyboard->columns + column]; } -tgbot_rc tgbot_inlinekb_button(tgbot_inlinekeyboard_s *keyboard, size_t row, size_t column, const char *text, - const char *url, const char *callback_data) { +int tgbot_inlinekb_button(tgbot_inlinekeyboard_s *keyboard, size_t row, size_t column, const char *text, + const char *url, const char *callback_data) { tgbot_inlinekeyboardbutton_s *button = tgbot_inlinekb_button_at(keyboard, row, column); strncpy(button->text, text, sizeof(button->text) - 1); strncpy(button->url, url, sizeof(button->url) - 1); strncpy(button->callback_data, callback_data, sizeof(button->callback_data) - 1); - return TGBOT_OK; + return 0; }