Compare commits

...

10 Commits

Author SHA1 Message Date
b40c061ccf refactor(methods): improve security 2026-01-06 18:21:54 +01:00
c68ec3c02e fix(tgbot.h): add missing headers 2026-01-06 18:21:19 +01:00
4be30d0830 refactor(echobot): remove warning 2026-01-06 18:20:54 +01:00
eb267c1f84 chore: improve meson build 2026-01-06 18:20:33 +01:00
b401e8c4e4 style: format header files 2026-01-06 18:01:20 +01:00
0114ca3c97 style: format file 2026-01-06 17:59:58 +01:00
ec2095caf8 chore: remove vscode folder 2026-01-06 17:55:41 +01:00
210aac2a3a refactor(methods): change funcs to static 2025-09-17 00:06:12 +02:00
c9af1429f9 refactor: cleanup 2025-09-16 23:53:50 +02:00
235c032062 refactor: readme 2025-09-16 23:14:28 +02:00
16 changed files with 993 additions and 950 deletions

View File

@@ -1,8 +1,8 @@
BasedOnStyle: LLVM
UseTab: Always UseTab: Always
IndentWidth: 4
TabWidth: 4 TabWidth: 4
ColumnLimit: 0 IndentWidth: 4
AllowAllParametersOfDeclarationOnNextLine: false IndentCaseLabels: true
BreakBeforeBinaryOperators: None ColumnLimit: 120
SpacesInParentheses: false PointerAlignment: Right
BinPackParameters: true AllowShortFunctionsOnASingleLine: None

View File

@@ -1 +0,0 @@
HeaderInsertion: Never

View File

@@ -1,5 +0,0 @@
{
"clangd.arguments": [
"--header-insertion=never",
]
}

View File

@@ -1,8 +1,9 @@
# tgbot # tgbot
A minimal C Telegram API Framework
A minimal C Telegram API Framework.
## Requirements ## Requirements
- meson
- libcurl - libcurl
- json-c - json-c
@@ -39,19 +40,23 @@ $ meson install
</details> </details>
## Examples ## Examples
You can find some examples [here](./examples/). You can find some examples [here](./examples/).
### Supported Types ### Supported Types
- **InlineKeyboardMarkup** - **InlineKeyboardMarkup**
- Note: Standard `KeyboardMarkup` is intentionally not supported. - Note: Standard `KeyboardMarkup` is intentionally not supported.
#### Supported Methods #### Supported Methods
- `getMe` - `getMe`
- `sendMessage` - `sendMessage`
- `editMessageText` - `editMessageText`
- `sendDice` - `sendDice`
## Roadmap ## Roadmap
- `sendPhoto` - `sendPhoto`
- `sendAudio` - `sendAudio`
- `sendDocument` - `sendDocument`

View File

@@ -8,10 +8,11 @@
bool run = true; bool run = true;
void sighandler(int signum) { void sighandler(int signum) {
(void)signum;
run = false; run = false;
} }
void echo_message(tgbot *bot, tgbot_update *update) { void echo_message(tgbot_s *bot, tgbot_update_s *update) {
tgbot_send_message(bot, update->chat_id, update->text, "MARKDOWN", NULL); tgbot_send_message(bot, update->chat_id, update->text, "MARKDOWN", NULL);
} }
@@ -31,22 +32,21 @@ int main(void) {
signal(SIGINT, sighandler); signal(SIGINT, sighandler);
/* Initialize bot */ /* Initialize bot */
tgbot bot; tgbot_s *bot = tgbot_new(token);
tgbot_init(&bot, token); tgbot_update_s update;
tgbot_update update;
while (run) { while (run) {
tgbot_get_update(&bot, &update, NULL); tgbot_get_update(bot, &update, NULL);
if (strcmp(update.text, "/start") == 0) { if (strcmp(update.text, "/start") == 0) {
/* Send dice if /start otherwise echo the message */ /* Send dice if /start otherwise echo the message */
tgbot_send_dice(&bot, update.chat_id, NULL); tgbot_send_dice(bot, update.chat_id, NULL);
} else { } else {
echo_message(&bot, &update); echo_message(bot, &update);
} }
} }
fprintf(stdout, "Closing..."); fprintf(stdout, "Closing...");
tgbot_destroy(&bot); tgbot_free(bot);
return 0; return 0;
} }

View File

@@ -8,23 +8,23 @@
#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 *bot, tgbot_update *update); void parse_command(tgbot_s *bot, tgbot_update_s *update);
void sighandler(int signum); void sighandler(int signum);
void callback_parser(tgbot *bot, tgbot_cbquery *query); void callback_parser(tgbot_s *bot, tgbot_cbquery_s *query);
bool run = true; bool run = true;
tgbot bot; tgbot_s *bot;
tgbot_inlinekeyboard *keyboard; tgbot_inlinekeyboard_s *keyboard;
/* Callback handler function */ /* Callback handler function */
void callback_handler(tgbot *bot, tgbot_cbquery *query) { void callback_handler(tgbot_s *bot, tgbot_cbquery_s *query) {
if (strcmp("test-callback", query->data) == 0) { if (strcmp("test-callback", query->data) == 0) {
/* Handle `test-callback` */ /* Handle `test-callback` */
tgbot_inlinekeyboard *home_keyboard = tgbot_new_inlinekeyboard(1, 1); tgbot_inlinekeyboard_s *home_keyboard = tgbot_inlinekb_new(1, 1);
/* Add buttons */ /* Add buttons */
tgbot_inlinekeyboard_button(home_keyboard, 0, 0, "Home", "", "home"); tgbot_inlinekb_button(home_keyboard, 0, 0, "Home", "", "home");
tgbot_edit_message_text(bot, query->chat_id, query->message_id, "Callback called!", home_keyboard); tgbot_edit_message_text(bot, query->chat_id, query->message_id, "Callback called!", home_keyboard);
tgbot_destroy_inlinekeyboard(home_keyboard); tgbot_inlinekb_free(home_keyboard);
return; return;
} else if (strcmp("home", query->data) == 0) { } else if (strcmp("home", query->data) == 0) {
@@ -39,7 +39,7 @@ void sighandler(int signum) {
run = false; run = false;
} }
void parse_command(tgbot *bot, tgbot_update *update) { void parse_command(tgbot_s *bot, tgbot_update_s *update) {
tgbot_rc ret; tgbot_rc ret;
if (strcmp("/start", update->text) == 0) { if (strcmp("/start", update->text) == 0) {
@@ -76,8 +76,8 @@ int main(void) {
fclose(fp); fclose(fp);
/* Initialize the bot */ /* Initialize the bot */
ret = tgbot_init(&bot, token); bot = tgbot_new(token);
if (ret != TGBOT_OK) { if (!bot) {
fprintf(stderr, "tgbot_init()\n"); fprintf(stderr, "tgbot_init()\n");
exit(1); exit(1);
} }
@@ -85,35 +85,35 @@ int main(void) {
/* Calling tgbot_get_me() you can get bot's info */ /* Calling tgbot_get_me() you can get bot's info */
fprintf(stdout, "Running Telegram bot...\nPress Ctrl-C to close.\n"); fprintf(stdout, "Running Telegram bot...\nPress Ctrl-C to close.\n");
tgbot_update update; tgbot_update_s update;
/* Allocate the new inline keyboard (remember to free!) */ /* Allocate the new inline keyboard (remember to free!) */
keyboard = tgbot_new_inlinekeyboard(1, 2); keyboard = tgbot_inlinekb_new(1, 2);
if (keyboard == NULL) { if (keyboard == NULL) {
tgbot_destroy(&bot); tgbot_free(bot);
return 1; return 1;
} }
/* Populate the InlineKeyboardMarkup */ /* Populate the InlineKeyboardMarkup */
tgbot_inlinekeyboard_button(keyboard, 0, 0, "Google", "https://google.com", ""); tgbot_inlinekb_button(keyboard, 0, 0, "Google", "https://google.com", "");
tgbot_inlinekeyboard_button(keyboard, 0, 1, "Callback", "", "test-callback"); tgbot_inlinekb_button(keyboard, 0, 1, "Callback", "", "test-callback");
/* If you want 3 buttons on 2 rows, for example on the first row 2 buttons and on the second only one */ /* If you want 3 buttons on 2 rows, for example on the first row 2 buttons and on the second only one */
/* you have to put rows = 2, columns = 2 and pass an empty string to .text field */ /* you have to put rows = 2, columns = 2 and pass an empty string to .text field */
/* Main loop */ /* Main loop */
while (run) { while (run) {
ret = tgbot_get_update(&bot, &update, callback_handler); 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;
} }
if (update.message_id > 0) { if (update.message_id > 0) {
parse_command(&bot, &update); parse_command(bot, &update);
} }
} }
tgbot_destroy_inlinekeyboard(keyboard); tgbot_inlinekb_free(keyboard);
tgbot_destroy(&bot); tgbot_free(bot);
return 0; return 0;
} }

View File

@@ -4,6 +4,16 @@
#include <curl/curl.h> #include <curl/curl.h>
#include <stdint.h> #include <stdint.h>
#define TOKEN_SIZE 128
#define API_SIZE 512
#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. * @brief A structure used to get curl response.
*/ */
@@ -15,57 +25,53 @@ struct memory_buffer {
/** /**
* @brief A structure to represent bot object. * @brief A structure to represent bot object.
*/ */
struct tgbot_t { typedef struct tgbot {
char token[128]; /**< Bot token. */ char token[TOKEN_SIZE]; /**< Bot token. */
char api[512]; /**< Bot API url. */ char api[API_SIZE]; /**< Bot API url. */
int32_t offset; /**< Bot offset. */ int32_t offset; /**< Bot offset. */
}; } tgbot_s;
typedef struct tgbot_t tgbot;
/** /**
* @brief A structure to represent Bot information got from getMe API. * @brief A structure to represent Bot information got from getMe API.
*/ */
struct tgbot_me_t { typedef struct tgbot_me {
char first_name[256]; /**< Bot's first name. */ char first_name[FIRSTNAME_SIZE]; /**< Bot's first name. */
char username[32]; /**< Bot's username. */ char username[USERNAME_SIZE]; /**< Bot's username. */
}; } tgbot_me_s;
typedef struct tgbot_me_t tgbot_me;
/** /**
* @brief A structure to represent Update object. * @brief A structure to represent Update object.
*/ */
struct tgbot_update_t { typedef struct tgbot_update {
int64_t update_id; /**< Update id. */ int64_t update_id; /**< Update id. */
long message_id; /**< Message id. */ long message_id; /**< Message id. */
int64_t chat_id; /**< Chat id. */ int64_t chat_id; /**< Chat id. */
char chat_first_name[256]; /**< Chat first name. */ char chat_first_name[FIRSTNAME_SIZE]; /**< Chat first name. */
char chat_last_name[256]; /**< Chat last name. */ char chat_last_name[CHAT_LASTNAME_SIZE]; /**< Chat last name. */
char chat_username[32]; /**< Chat username. */ char chat_username[USERNAME_SIZE]; /**< Chat username. */
char chat_type[32]; /**< Chat type (private/public). */ char chat_type[CHAT_TYPE_SIZE]; /**< Chat type (private/public). */
int32_t date; /**< Date in unix timestamp. */ int32_t date; /**< Date in unix timestamp. */
char text[4096]; /**< Message text. */ char text[CHAT_TEXT_SIZE]; /**< Message text. */
}; } tgbot_update_s;
typedef struct tgbot_update_t tgbot_update;
/** /**
* @brief A structure to represent CallbackQuery object. * @brief A structure to represent CallbackQuery object.
*/ */
struct tgbot_cbquery_t { typedef struct tgbot_cbquery {
int64_t update_id; int64_t update_id;
long message_id; long message_id;
int64_t chat_id; int64_t chat_id;
char chat_username[32]; char chat_username[USERNAME_SIZE];
int32_t date; int32_t date;
char text[4096]; char text[CHAT_TEXT_SIZE];
char chat_instance[128]; char chat_instance[128];
char data[64]; /**> Callback data. */ char data[64]; /**> Callback data. */
}; } tgbot_cbquery_s;
typedef struct tgbot_cbquery_t tgbot_cbquery;
/** /**
* @brief Callback function pointer. * @brief Callback function pointer.
*/ */
typedef void (*Callback)(tgbot *bot, tgbot_cbquery *query); typedef void (*Callback)(tgbot_s *bot, tgbot_cbquery_s *query);
/** /**
* @brief An enum to represent error codes. * @brief An enum to represent error codes.

View File

@@ -4,7 +4,8 @@
#include "types.h" #include "types.h"
#include <json-c/json.h> #include <json-c/json.h>
json_object *tgbot_json_builder(tgbot_json_option *options, size_t optionslen); json_object *json_builder(tgbot_json_option *options, size_t optionslen);
json_object *tgbot_new_inlinekeyboardmarkup(tgbot_inlinekeyboard *keyboard);
json_object *json_ikb_new(tgbot_inlinekeyboard_s *keyboard);
#endif #endif

View File

@@ -4,20 +4,21 @@
#include "types.h" #include "types.h"
#include <json-c/json.h> #include <json-c/json.h>
tgbot_rc tgbot_get_update(tgbot *bot, tgbot_update *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 *bot, tgbot_update *update, json_object *result); tgbot_rc tgbot_parse_message(tgbot_s *bot, tgbot_update_s *update, json_object *result);
tgbot_rc tgbot_parse_cbquery(tgbot *bot, tgbot_cbquery *query, json_object *result, Callback query_handler); tgbot_rc tgbot_parse_cbquery(tgbot_s *bot, tgbot_cbquery_s *query, json_object *result, Callback query_handler);
/* Request */ /* Request */
size_t write_callback(void *ptr, size_t size, size_t nmemb, char **userdata);
tgbot_rc tgbot_request(const char *url, struct memory_buffer **mb, json_object *json); tgbot_rc tgbot_request(const char *url, struct memory_buffer **mb, json_object *json);
/* Methods */ /* Methods */
tgbot_rc tgbot_get_me(tgbot *bot, tgbot_me *me); tgbot_rc tgbot_get_me(tgbot_s *bot, tgbot_me_s *me);
tgbot_rc tgbot_send_message(tgbot *bot, int64_t chat_id, const char *text, const char *parse_mode, tgbot_inlinekeyboard *reply_markup); tgbot_rc tgbot_send_message(tgbot_s *bot, int64_t chat_id, const char *text, const char *parse_mode,
tgbot_rc tgbot_send_dice(tgbot *bot, int64_t chat_id, const char *emoji); tgbot_inlinekeyboard_s *reply_markup);
tgbot_rc tgbot_send_dice(tgbot_s *bot, int64_t chat_id, const char *emoji);
/* Updating Methods */ /* Updating Methods */
tgbot_rc tgbot_edit_message_text(tgbot *bot, int64_t chat_id, long message_id, const char *text, tgbot_inlinekeyboard *keyboard); tgbot_rc tgbot_edit_message_text(tgbot_s *bot, int64_t chat_id, long message_id, const char *text,
tgbot_inlinekeyboard_s *keyboard);
#endif #endif

View File

@@ -6,20 +6,19 @@
#include "types.h" #include "types.h"
/** /**
* @brief Initializes the Bot object. * @brief Create a new Bot object.
* *
* @param[out] bot The Bot object.
* @param[in] token The Bot token (obtained from @BotFather). * @param[in] token The Bot token (obtained from @BotFather).
* *
* @return TGBOT_OK on success. * @return Bot pointer.
*/ */
tgbot_rc tgbot_init(tgbot *bot, char *token); tgbot_s *tgbot_new(char *token);
/** /**
* @brief Cleans the memory. * @brief Cleans the memory.
* *
* @param[out] bot The Bot object. * @param[out] bot The Bot object.
*/ */
void tgbot_destroy(tgbot *bot); void tgbot_free(tgbot_s *bot);
#endif // TGBOT_H #endif // TGBOT_H

View File

@@ -3,25 +3,27 @@
#include "common.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. * @brief Represents a single button on an inline keyboard.
*/ */
struct tgbot_inlinekeyboardbutton_t { typedef struct tgbot_inlinekeyboardbutton {
char text[200]; /**< Text of the button. If empty, the button is ignored. */ char text[TEXT_SIZE]; /**< Text of the button. If empty, the button is ignored. */
char url[200]; /**< (Optional) URL to be opened when the button is pressed. */ char url[URL_SIZE]; /**< (Optional) URL to be opened when the button is pressed. */
char callback_data[64]; /**< (Optional) Data sent to the bot 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;
typedef struct tgbot_inlinekeyboardbutton_t tgbot_inlinekeyboardbutton;
/** /**
* @brief Represents an inline keyboard. * @brief Represents an inline keyboard.
*/ */
struct tgbot_inlinekeyboard_t { typedef struct tgbot_inlinekeyboard {
size_t rows; /**< Number of rows in the keyboard. */ size_t rows; /**< Number of rows in the keyboard. */
size_t columns; /**< Number of columns per row. */ size_t columns; /**< Number of columns per row. */
struct tgbot_inlinekeyboardbutton_t *buttons; /**< Array of buttons. */ tgbot_inlinekeyboardbutton_s *buttons; /**< Array of buttons. */
}; } tgbot_inlinekeyboard_s;
typedef struct tgbot_inlinekeyboard_t tgbot_inlinekeyboard;
/** /**
* @brief Allocates a new inline keyboard. * @brief Allocates a new inline keyboard.
@@ -31,7 +33,7 @@ typedef struct tgbot_inlinekeyboard_t tgbot_inlinekeyboard;
* *
* @return The pointer to the keyboard or NULL on allocation failure. * @return The pointer to the keyboard or NULL on allocation failure.
*/ */
tgbot_inlinekeyboard *tgbot_new_inlinekeyboard(size_t rows, size_t columns); 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. * @brief Adds or updates a button at the specified position in the keyboard.
@@ -45,7 +47,8 @@ tgbot_inlinekeyboard *tgbot_new_inlinekeyboard(size_t rows, size_t columns);
* *
* @return TGBOT_OK on success. * @return TGBOT_OK on success.
*/ */
tgbot_rc tgbot_inlinekeyboard_button(tgbot_inlinekeyboard *keyboard, size_t row, size_t column, const char *text, const char *url, const char *callback_data); 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. * @brief Returns a pointer to the keyboard's button.
@@ -56,13 +59,13 @@ tgbot_rc tgbot_inlinekeyboard_button(tgbot_inlinekeyboard *keyboard, size_t row,
* *
* @return Pointer to the button, or NULL if the position is invalid. * @return Pointer to the button, or NULL if the position is invalid.
*/ */
tgbot_inlinekeyboardbutton *tgbot_inlinekeyboard_button_at(tgbot_inlinekeyboard *keyboard, size_t row, size_t column); 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. * @brief Frees all memory associated with the given inline keyboard.
* *
* @param[in,out] keyboard Pointer to the keyboard structure to deallocate. * @param[in,out] keyboard Pointer to the keyboard structure to deallocate.
*/ */
void tgbot_destroy_inlinekeyboard(tgbot_inlinekeyboard *keyboard); void tgbot_inlinekb_free(tgbot_inlinekeyboard_s *keyboard);
#endif #endif

View File

@@ -1,4 +1,9 @@
project('tgbot', 'c', version: '0.1') project(
'tgbot',
'c',
version: '0.1',
default_options: ['warning_level=3', 'c_std=c18'],
)
sources = [] sources = []
subdir('src') subdir('src')
@@ -20,11 +25,15 @@ tgbot_lib = library(
install: true, install: true,
) )
# Example (windows) tgbot_dep = declare_dependency(
link_with: tgbot_lib,
include_directories: inc_dir,
dependencies: deps,
)
# Example
executable( executable(
'example', 'example',
'examples/echobot/echobot.c', 'examples/echobot/echobot.c',
dependencies: deps, dependencies: tgbot_dep,
link_with: tgbot_lib,
include_directories: inc_dir,
) )

View File

@@ -2,7 +2,7 @@
#include "json.h" #include "json.h"
json_object *tgbot_json_builder(tgbot_json_option *options, size_t optionslen) { json_object *json_builder(tgbot_json_option *options, size_t optionslen) {
json_object *rjson = json_object_new_object(); json_object *rjson = json_object_new_object();
for (size_t i = 0; i < optionslen; ++i) { for (size_t i = 0; i < optionslen; ++i) {
@@ -17,7 +17,7 @@ json_object *tgbot_json_builder(tgbot_json_option *options, size_t optionslen) {
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) { } else if (options[i].type == tgbot_opt_inlinekeyboard) {
if (options[i].value != NULL) { if (options[i].value != NULL) {
json_object *reply_markup = tgbot_new_inlinekeyboardmarkup((tgbot_inlinekeyboard *)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, "reply_markup", reply_markup);
} }
} else if (options[i].type == tgbot_opt_null) { } else if (options[i].type == tgbot_opt_null) {
@@ -28,14 +28,14 @@ json_object *tgbot_json_builder(tgbot_json_option *options, size_t optionslen) {
return rjson; return rjson;
} }
json_object *tgbot_new_inlinekeyboardmarkup(tgbot_inlinekeyboard *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();
json_object *inline_keyboard_array = json_object_new_array(); json_object *inline_keyboard_array = json_object_new_array();
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();
for (size_t j = 0; j < keyboard->columns; ++j) { for (size_t j = 0; j < keyboard->columns; ++j) {
tgbot_inlinekeyboardbutton *kbbutton = tgbot_inlinekeyboard_button_at(keyboard, i, j); tgbot_inlinekeyboardbutton_s *kbbutton = tgbot_inlinekb_button_at(keyboard, i, j);
if (strcmp(kbbutton->text, "") == 0) { if (strcmp(kbbutton->text, "") == 0) {
continue; continue;
} }

View File

@@ -1,14 +1,40 @@
#include <curl/curl.h> #include <curl/curl.h>
#include <curl/easy.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include "common.h"
#include "json.h" #include "json.h"
#include "json_object.h"
#include "methods.h" #include "methods.h"
tgbot_rc tgbot_get_update(tgbot *bot, tgbot_update *update, Callback cbq_handler) { static size_t write_callback(void *ptr, size_t size, size_t nmemb, char *userdata) {
size_t real_size = size * nmemb;
struct memory_buffer *mem = (struct memory_buffer *)userdata;
char *tmp = realloc(mem->data, mem->size + real_size + 1);
if (!tmp) {
return 0;
}
mem->data = tmp;
memcpy(&(mem->data[mem->size]), ptr, real_size);
mem->size += real_size;
mem->data[mem->size] = '\0';
return real_size;
}
static size_t discard_callback(char *ptr, size_t size, size_t nmemb, void *userdata) {
(void)userdata;
(void)ptr;
return size * nmemb;
}
tgbot_rc tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_handler) {
char url[1024]; char url[1024];
/* Clear Update */ memset(update, 0, sizeof(tgbot_update_s));
memset(update, 0, sizeof(tgbot_update));
int limit = 1; int limit = 1;
int timeout = 30; int timeout = 30;
@@ -17,7 +43,7 @@ tgbot_rc tgbot_get_update(tgbot *bot, tgbot_update *update, Callback cbq_handler
{"limit", &limit, tgbot_opt_int}, {"limit", &limit, tgbot_opt_int},
{"timeout", &timeout, tgbot_opt_int}, {"timeout", &timeout, tgbot_opt_int},
}; };
json_object *rjson = tgbot_json_builder(options, 3); json_object *rjson = json_builder(options, 3);
snprintf(url, sizeof(url), "%sgetUpdates", bot->api); snprintf(url, sizeof(url), "%sgetUpdates", bot->api);
@@ -25,8 +51,10 @@ tgbot_rc tgbot_get_update(tgbot *bot, tgbot_update *update, Callback cbq_handler
tgbot_rc ret = tgbot_request(url, &mb, rjson); tgbot_rc ret = tgbot_request(url, &mb, rjson);
json_object_put(rjson); json_object_put(rjson);
if (ret != TGBOT_OK) { if (ret != TGBOT_OK) {
if (mb) {
free(mb->data); free(mb->data);
free(mb); free(mb);
}
return TGBOT_GETUPDATES_ERROR; return TGBOT_GETUPDATES_ERROR;
} }
@@ -57,7 +85,7 @@ tgbot_rc tgbot_get_update(tgbot *bot, tgbot_update *update, Callback cbq_handler
if (message) { if (message) {
tgbot_parse_message(bot, update, result); tgbot_parse_message(bot, update, result);
} else if (cbq_handler != NULL) { } else if (cbq_handler != NULL) {
tgbot_cbquery query; tgbot_cbquery_s query;
tgbot_parse_cbquery(bot, &query, result, cbq_handler); tgbot_parse_cbquery(bot, &query, result, cbq_handler);
} }
@@ -66,7 +94,7 @@ tgbot_rc tgbot_get_update(tgbot *bot, tgbot_update *update, Callback cbq_handler
return TGBOT_OK; return TGBOT_OK;
} }
tgbot_rc tgbot_parse_message(tgbot *bot, tgbot_update *update, json_object *result) { 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"); json_object *update_id = json_object_object_get(result, "update_id");
bot->offset = json_object_get_int(update_id) + 1; bot->offset = json_object_get_int(update_id) + 1;
update->update_id = json_object_get_int(update_id); update->update_id = json_object_get_int(update_id);
@@ -110,13 +138,13 @@ tgbot_rc tgbot_parse_message(tgbot *bot, tgbot_update *update, json_object *resu
json_object *text = json_object_object_get(message, "text"); json_object *text = json_object_object_get(message, "text");
if (text) { if (text) {
strncpy(update->text, json_object_get_string(text), sizeof(update->text) - 1); snprintf(update->text, sizeof(update->text), "%s", json_object_get_string(text));
} }
return TGBOT_OK; return TGBOT_OK;
} }
tgbot_rc tgbot_parse_cbquery(tgbot *bot, tgbot_cbquery *query, json_object *result, Callback cbq_handler) { 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"); json_object *update_id = json_object_object_get(result, "update_id");
bot->offset = json_object_get_int(update_id) + 1; bot->offset = json_object_get_int(update_id) + 1;
query->update_id = json_object_get_int(update_id); query->update_id = json_object_get_int(update_id);
@@ -164,22 +192,6 @@ tgbot_rc tgbot_parse_cbquery(tgbot *bot, tgbot_cbquery *query, json_object *resu
return TGBOT_OK; return TGBOT_OK;
} }
size_t write_callback(void *ptr, size_t size, size_t nmemb, char **userdata) {
size_t real_size = size * nmemb;
struct memory_buffer *mem = (struct memory_buffer *)userdata;
mem->data = realloc(mem->data, mem->size + real_size + 1);
memcpy(&(mem->data[mem->size]), ptr, real_size);
mem->size += real_size;
mem->data[mem->size] = '\0';
return real_size;
}
size_t discard_callback(char *ptr, size_t size, size_t nmemb, void *userdata) {
return size * nmemb;
}
tgbot_rc tgbot_request(const char *url, struct memory_buffer **mb, json_object *json) { 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) {
@@ -217,6 +229,7 @@ tgbot_rc tgbot_request(const char *url, struct memory_buffer **mb, json_object *
curl_slist_free_all(headers); curl_slist_free_all(headers);
if (res != CURLE_OK) { if (res != CURLE_OK) {
curl_easy_cleanup(curl);
if (mb != NULL && *mb) { if (mb != NULL && *mb) {
free((*mb)->data); free((*mb)->data);
free(*mb); free(*mb);
@@ -231,7 +244,7 @@ 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 *bot, tgbot_me *me) { tgbot_rc tgbot_get_me(tgbot_s *bot, tgbot_me_s *me) {
char url[1024]; char url[1024];
snprintf(url, sizeof(url), "%sgetMe", bot->api); snprintf(url, sizeof(url), "%sgetMe", bot->api);
@@ -266,7 +279,8 @@ tgbot_rc tgbot_get_me(tgbot *bot, tgbot_me *me) {
return TGBOT_OK; return TGBOT_OK;
} }
tgbot_rc tgbot_send_message(tgbot *bot, int64_t chat_id, const char *text, const char *parse_mode, tgbot_inlinekeyboard *keyboard) { tgbot_rc tgbot_send_message(tgbot_s *bot, int64_t chat_id, const char *text, const char *parse_mode,
tgbot_inlinekeyboard_s *keyboard) {
char url[1024]; char url[1024];
tgbot_json_option options[4] = { tgbot_json_option options[4] = {
@@ -275,7 +289,7 @@ tgbot_rc tgbot_send_message(tgbot *bot, int64_t chat_id, const char *text, const
{"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 = tgbot_json_builder(options, 4); json_object *rjson = json_builder(options, 4);
snprintf(url, sizeof(url), "%ssendMessage", bot->api); snprintf(url, sizeof(url), "%ssendMessage", bot->api);
@@ -289,7 +303,8 @@ tgbot_rc tgbot_send_message(tgbot *bot, int64_t chat_id, const char *text, const
return TGBOT_OK; return TGBOT_OK;
} }
tgbot_rc tgbot_edit_message_text(tgbot *bot, int64_t chat_id, long message_id, const char *text, tgbot_inlinekeyboard *keyboard) { tgbot_rc tgbot_edit_message_text(tgbot_s *bot, int64_t chat_id, long message_id, const char *text,
tgbot_inlinekeyboard_s *keyboard) {
char url[1024]; char url[1024];
tgbot_json_option options[4] = { tgbot_json_option options[4] = {
@@ -298,7 +313,7 @@ tgbot_rc tgbot_edit_message_text(tgbot *bot, int64_t chat_id, long message_id, c
{"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 = tgbot_json_builder(options, 4); json_object *rjson = json_builder(options, 4);
snprintf(url, sizeof(url), "%seditMessageText", bot->api); snprintf(url, sizeof(url), "%seditMessageText", bot->api);
@@ -312,14 +327,14 @@ tgbot_rc tgbot_edit_message_text(tgbot *bot, int64_t chat_id, long message_id, c
return TGBOT_OK; return TGBOT_OK;
} }
tgbot_rc tgbot_send_dice(tgbot *bot, int64_t chat_id, const char *emoji) { tgbot_rc tgbot_send_dice(tgbot_s *bot, int64_t chat_id, const char *emoji) {
char url[1024]; char url[1024];
tgbot_json_option 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 = tgbot_json_builder(options, 2); json_object *rjson = json_builder(options, 2);
snprintf(url, sizeof(url), "%ssendDice", bot->api); snprintf(url, sizeof(url), "%ssendDice", bot->api);

View File

@@ -2,19 +2,26 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include "tgbot.h" #include "tgbot.h"
tgbot_rc tgbot_init(tgbot *bot, char *token) { tgbot_s *tgbot_new(char *token) {
tgbot_s *bot = malloc(sizeof(tgbot_s));
if (!bot) {
return NULL;
}
curl_global_init(CURL_GLOBAL_DEFAULT); curl_global_init(CURL_GLOBAL_DEFAULT);
snprintf(bot->api, sizeof(bot->api), "https://api.telegram.org/bot%s/", token); snprintf(bot->api, sizeof(bot->api), "https://api.telegram.org/bot%s/", token);
snprintf(bot->token, sizeof(bot->token), "%s", token); snprintf(bot->token, sizeof(bot->token), "%s", token);
bot->offset = 0; bot->offset = 0;
return TGBOT_OK; return bot;
} }
void tgbot_destroy(tgbot *bot) { void tgbot_free(tgbot_s *bot) {
curl_global_cleanup(); curl_global_cleanup();
free(bot);
} }

View File

@@ -3,31 +3,38 @@
#include "types.h" #include "types.h"
tgbot_inlinekeyboard *tgbot_new_inlinekeyboard(size_t rows, size_t columns) { tgbot_inlinekeyboard_s *tgbot_inlinekb_new(size_t rows, size_t columns) {
tgbot_inlinekeyboard *keyboard = (tgbot_inlinekeyboard *)malloc(sizeof(tgbot_inlinekeyboard)); tgbot_inlinekeyboard_s *keyboard = (tgbot_inlinekeyboard_s *)malloc(sizeof(tgbot_inlinekeyboard_s));
if (!keyboard) if (!keyboard) {
return NULL; return NULL;
}
keyboard->rows = rows; keyboard->rows = rows;
keyboard->columns = columns; keyboard->columns = columns;
keyboard->buttons = (tgbot_inlinekeyboardbutton *)malloc(rows * columns * sizeof(tgbot_inlinekeyboardbutton)); keyboard->buttons = (tgbot_inlinekeyboardbutton_s *)malloc(rows * columns * sizeof(tgbot_inlinekeyboardbutton_s));
if (!keyboard->buttons) { if (!keyboard->buttons) {
free(keyboard); free(keyboard);
return NULL; return NULL;
} }
memset(keyboard->buttons, 0, rows * columns * sizeof(tgbot_inlinekeyboardbutton)); memset(keyboard->buttons, 0, rows * columns * sizeof(tgbot_inlinekeyboardbutton_s));
return keyboard; return keyboard;
} }
void tgbot_destroy_inlinekeyboard(tgbot_inlinekeyboard *keyboard) { void tgbot_inlinekb_free(tgbot_inlinekeyboard_s *keyboard) {
free(keyboard->buttons); free(keyboard->buttons);
free(keyboard); free(keyboard);
} }
tgbot_rc tgbot_inlinekeyboard_button(tgbot_inlinekeyboard *keyboard, size_t row, size_t column, const char *text, const char *url, const char *callback_data) { tgbot_inlinekeyboardbutton_s *tgbot_inlinekb_button_at(tgbot_inlinekeyboard_s *keyboard, size_t row, size_t column) {
tgbot_inlinekeyboardbutton *button = tgbot_inlinekeyboard_button_at(keyboard, row, column); 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) {
tgbot_inlinekeyboardbutton_s *button = tgbot_inlinekb_button_at(keyboard, row, column);
strncpy(button->text, text, sizeof(button->text) - 1); strncpy(button->text, text, sizeof(button->text) - 1);
strncpy(button->url, url, sizeof(button->url) - 1); strncpy(button->url, url, sizeof(button->url) - 1);
@@ -35,7 +42,3 @@ tgbot_rc tgbot_inlinekeyboard_button(tgbot_inlinekeyboard *keyboard, size_t row,
return TGBOT_OK; return TGBOT_OK;
} }
tgbot_inlinekeyboardbutton *tgbot_inlinekeyboard_button_at(tgbot_inlinekeyboard *keyboard, size_t row, size_t column) {
return &keyboard->buttons[row * keyboard->columns + column];
}