refactor: move common to internal and update public api

This commit is contained in:
2026-03-17 00:30:38 +01:00
parent 6ca04a9392
commit bb31304614
17 changed files with 284 additions and 272 deletions
+45
View File
@@ -0,0 +1,45 @@
#ifndef TGBOT_INTERNAL_COMMON_H
#define TGBOT_INTERNAL_COMMON_H
#include "types.h"
#include <curl/curl.h>
#include <stdint.h>
#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
+13
View File
@@ -0,0 +1,13 @@
#ifndef TGBOT_INTERNAL_JSON_H
#define TGBOT_INTERNAL_JSON_H
#include "types.h"
#include <json-c/json.h>
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);
#endif
+11
View File
@@ -0,0 +1,11 @@
#ifndef TGBOT_INTERNAL_PARSE_H
#define TGBOT_INTERNAL_PARSE_H
#include "types.h"
#include <json-c/json.h>
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
+3 -1
View File
@@ -1,7 +1,9 @@
#include <stdio.h>
#include <string.h>
#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();
+31 -35
View File
@@ -2,11 +2,11 @@
#include <stdio.h>
#include <string.h>
#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));
}
+6 -5
View File
@@ -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;
}
+1
View File
@@ -4,6 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "internal/common.h"
#include "tgbot.h"
tgbot_s *tgbot_new(const char *token) {
+3 -3
View File
@@ -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;
}