refactor: cleanup
This commit is contained in:
210
include/common.h
210
include/common.h
@@ -1,102 +1,108 @@
|
||||
#ifndef TGBOT_COMMON_H
|
||||
#define TGBOT_COMMON_H
|
||||
|
||||
#include <curl/curl.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* @brief A structure used to get curl response.
|
||||
*/
|
||||
struct memory_buffer {
|
||||
char *data;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief A structure to represent bot object.
|
||||
*/
|
||||
struct tgbot_t {
|
||||
char token[128]; /**< Bot token. */
|
||||
char api[512]; /**< Bot API url. */
|
||||
int32_t offset; /**< Bot offset. */
|
||||
};
|
||||
typedef struct tgbot_t tgbot;
|
||||
|
||||
/**
|
||||
* @brief A structure to represent Bot information got from getMe API.
|
||||
*/
|
||||
struct tgbot_me_t {
|
||||
char first_name[256]; /**< Bot's first name. */
|
||||
char username[32]; /**< Bot's username. */
|
||||
};
|
||||
typedef struct tgbot_me_t tgbot_me;
|
||||
|
||||
/**
|
||||
* @brief A structure to represent Update object.
|
||||
*/
|
||||
struct tgbot_update_t {
|
||||
int64_t update_id; /**< Update id. */
|
||||
long message_id; /**< Message id. */
|
||||
int64_t chat_id; /**< Chat id. */
|
||||
char chat_first_name[256]; /**< Chat first name. */
|
||||
char chat_last_name[256]; /**< Chat last name. */
|
||||
char chat_username[32]; /**< Chat username. */
|
||||
char chat_type[32]; /**< Chat type (private/public). */
|
||||
int32_t date; /**< Date in unix timestamp. */
|
||||
char text[4096]; /**< Message text. */
|
||||
};
|
||||
typedef struct tgbot_update_t tgbot_update;
|
||||
|
||||
/**
|
||||
* @brief A structure to represent CallbackQuery object.
|
||||
*/
|
||||
struct tgbot_cbquery_t {
|
||||
int64_t update_id;
|
||||
long message_id;
|
||||
int64_t chat_id;
|
||||
char chat_username[32];
|
||||
int32_t date;
|
||||
char text[4096];
|
||||
char chat_instance[128];
|
||||
char data[64]; /**> Callback data. */
|
||||
};
|
||||
typedef struct tgbot_cbquery_t tgbot_cbquery;
|
||||
|
||||
/**
|
||||
* @brief Callback function pointer.
|
||||
*/
|
||||
typedef void (*Callback)(tgbot *bot, tgbot_cbquery *query);
|
||||
|
||||
/**
|
||||
* @brief An enum to represent error codes.
|
||||
*/
|
||||
enum tgbot_rc_t {
|
||||
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_t tgbot_rc;
|
||||
|
||||
enum tgbot_json_opt_type_t {
|
||||
tgbot_opt_int,
|
||||
tgbot_opt_int64,
|
||||
tgbot_opt_string,
|
||||
tgbot_opt_inlinekeyboard,
|
||||
tgbot_opt_null,
|
||||
};
|
||||
typedef enum tgbot_json_opt_type_t tgbot_json_opt_type;
|
||||
|
||||
struct tgbot_json_option_t {
|
||||
char key[32];
|
||||
void *value;
|
||||
tgbot_json_opt_type type;
|
||||
};
|
||||
typedef struct tgbot_json_option_t tgbot_json_option;
|
||||
|
||||
#endif
|
||||
#ifndef TGBOT_COMMON_H
|
||||
#define TGBOT_COMMON_H
|
||||
|
||||
#include <curl/curl.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.
|
||||
*/
|
||||
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_t {
|
||||
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_t tgbot_rc;
|
||||
|
||||
enum tgbot_json_opt_type_t {
|
||||
tgbot_opt_int,
|
||||
tgbot_opt_int64,
|
||||
tgbot_opt_string,
|
||||
tgbot_opt_inlinekeyboard,
|
||||
tgbot_opt_null,
|
||||
};
|
||||
typedef enum tgbot_json_opt_type_t tgbot_json_opt_type;
|
||||
|
||||
struct tgbot_json_option_t {
|
||||
char key[32];
|
||||
void *value;
|
||||
tgbot_json_opt_type type;
|
||||
};
|
||||
typedef struct tgbot_json_option_t tgbot_json_option;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
#ifndef TGBOT_JSON_H
|
||||
#define TGBOT_JSON_H
|
||||
|
||||
#include "types.h"
|
||||
#include <json-c/json.h>
|
||||
|
||||
json_object *tgbot_json_builder(tgbot_json_option *options, size_t optionslen);
|
||||
json_object *tgbot_new_inlinekeyboardmarkup(tgbot_inlinekeyboard *keyboard);
|
||||
|
||||
#endif
|
||||
#ifndef TGBOT_JSON_H
|
||||
#define TGBOT_JSON_H
|
||||
|
||||
#include "types.h"
|
||||
#include <json-c/json.h>
|
||||
|
||||
json_object *json_builder(tgbot_json_option *options, size_t optionslen);
|
||||
|
||||
json_object *json_ikb_new(tgbot_inlinekeyboard_s *keyboard);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
#ifndef TGBOT_METHODS_H
|
||||
#define TGBOT_METHODS_H
|
||||
|
||||
#include "types.h"
|
||||
#include <json-c/json.h>
|
||||
|
||||
tgbot_rc tgbot_get_update(tgbot *bot, tgbot_update *update, Callback cbq_handler);
|
||||
tgbot_rc tgbot_parse_message(tgbot *bot, tgbot_update *update, json_object *result);
|
||||
tgbot_rc tgbot_parse_cbquery(tgbot *bot, tgbot_cbquery *query, json_object *result, Callback query_handler);
|
||||
|
||||
/* 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);
|
||||
|
||||
/* Methods */
|
||||
tgbot_rc tgbot_get_me(tgbot *bot, tgbot_me *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_dice(tgbot *bot, int64_t chat_id, const char *emoji);
|
||||
|
||||
/* Updating Methods */
|
||||
tgbot_rc tgbot_edit_message_text(tgbot *bot, int64_t chat_id, long message_id, const char *text, tgbot_inlinekeyboard *keyboard);
|
||||
|
||||
#endif
|
||||
#ifndef TGBOT_METHODS_H
|
||||
#define TGBOT_METHODS_H
|
||||
|
||||
#include "types.h"
|
||||
#include <json-c/json.h>
|
||||
|
||||
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 */
|
||||
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);
|
||||
|
||||
/* Methods */
|
||||
tgbot_rc tgbot_get_me(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_inlinekeyboard_s *reply_markup);
|
||||
tgbot_rc tgbot_send_dice(tgbot_s *bot, int64_t chat_id, const char *emoji);
|
||||
|
||||
/* Updating Methods */
|
||||
tgbot_rc tgbot_edit_message_text(tgbot_s *bot, int64_t chat_id, long message_id, const char *text, tgbot_inlinekeyboard_s *keyboard);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,25 +1,24 @@
|
||||
#ifndef TGBOT_MAIN_H
|
||||
#define TGBOT_MAIN_H
|
||||
|
||||
#include "common.h"
|
||||
#include "methods.h"
|
||||
#include "types.h"
|
||||
|
||||
/**
|
||||
* @brief Initializes the Bot object.
|
||||
*
|
||||
* @param[out] bot The Bot object.
|
||||
* @param[in] token The Bot token (obtained from @BotFather).
|
||||
*
|
||||
* @return TGBOT_OK on success.
|
||||
*/
|
||||
tgbot_rc tgbot_init(tgbot *bot, char *token);
|
||||
|
||||
/**
|
||||
* @brief Cleans the memory.
|
||||
*
|
||||
* @param[out] bot The Bot object.
|
||||
*/
|
||||
void tgbot_destroy(tgbot *bot);
|
||||
|
||||
#endif // TGBOT_H
|
||||
#ifndef TGBOT_MAIN_H
|
||||
#define TGBOT_MAIN_H
|
||||
|
||||
#include "common.h"
|
||||
#include "methods.h"
|
||||
#include "types.h"
|
||||
|
||||
/**
|
||||
* @brief Create a new Bot object.
|
||||
*
|
||||
* @param[in] token The Bot token (obtained from @BotFather).
|
||||
*
|
||||
* @return Bot pointer.
|
||||
*/
|
||||
tgbot_s *tgbot_new(char *token);
|
||||
|
||||
/**
|
||||
* @brief Cleans the memory.
|
||||
*
|
||||
* @param[out] bot The Bot object.
|
||||
*/
|
||||
void tgbot_free(tgbot_s *bot);
|
||||
|
||||
#endif // TGBOT_H
|
||||
|
||||
138
include/types.h
138
include/types.h
@@ -1,68 +1,70 @@
|
||||
#ifndef TGBOT_TYPES_H
|
||||
#define TGBOT_TYPES_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
/**
|
||||
* @brief Represents a single button on an inline keyboard.
|
||||
*/
|
||||
struct tgbot_inlinekeyboardbutton_t {
|
||||
char text[200]; /**< Text of the button. If empty, the button is ignored. */
|
||||
char url[200]; /**< (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. */
|
||||
};
|
||||
typedef struct tgbot_inlinekeyboardbutton_t tgbot_inlinekeyboardbutton;
|
||||
|
||||
/**
|
||||
* @brief Represents an inline keyboard.
|
||||
*/
|
||||
struct tgbot_inlinekeyboard_t {
|
||||
size_t rows; /**< Number of rows in the keyboard. */
|
||||
size_t columns; /**< Number of columns per row. */
|
||||
struct tgbot_inlinekeyboardbutton_t *buttons; /**< Array of buttons. */
|
||||
};
|
||||
typedef struct tgbot_inlinekeyboard_t tgbot_inlinekeyboard;
|
||||
|
||||
/**
|
||||
* @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 *tgbot_new_inlinekeyboard(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_inlinekeyboard_button(tgbot_inlinekeyboard *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 *tgbot_inlinekeyboard_button_at(tgbot_inlinekeyboard *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_destroy_inlinekeyboard(tgbot_inlinekeyboard *keyboard);
|
||||
|
||||
#endif
|
||||
#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
|
||||
|
||||
Reference in New Issue
Block a user