refactor: move common to internal and update public api
This commit is contained in:
@@ -3,7 +3,8 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <tgbot.h>
|
#include <tgbot/methods.h>
|
||||||
|
#include <tgbot/tgbot.h>
|
||||||
|
|
||||||
bool run = true;
|
bool run = true;
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,9 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <tgbot.h>
|
#include <tgbot/methods.h>
|
||||||
|
#include <tgbot/tg_types.h>
|
||||||
|
#include <tgbot/tgbot.h>
|
||||||
|
|
||||||
#define WELCOME_MSG "Hi there! This bot is coded in C."
|
#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) {
|
void parse_command(const tgbot_s *bot, const tgbot_update_s *update) {
|
||||||
tgbot_rc ret;
|
int ret;
|
||||||
|
|
||||||
if (strcmp("/start", update->text) == 0) {
|
if (strcmp("/start", update->text) == 0) {
|
||||||
ret = tgbot_send_message(bot, update->chat_id, WELCOME_MSG, "Markdown", keyboard);
|
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");
|
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 */
|
/* Echo the message */
|
||||||
ret = tgbot_send_message(bot, update->chat_id, update->text, "Markdown", NULL);
|
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");
|
fprintf(stderr, "Failed to send message\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -100,8 +102,8 @@ int main(void) {
|
|||||||
|
|
||||||
/* Main loop */
|
/* Main loop */
|
||||||
while (run) {
|
while (run) {
|
||||||
tgbot_rc ret = tgbot_get_update(bot, &update, callback_handler);
|
int ret = tgbot_get_update(bot, &update, callback_handler);
|
||||||
if (ret != TGBOT_OK) {
|
if (ret != 0) {
|
||||||
fprintf(stderr, "tgbot_get_updates()\n");
|
fprintf(stderr, "tgbot_get_updates()\n");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,109 +0,0 @@
|
|||||||
#ifndef TGBOT_COMMON_H
|
|
||||||
#define TGBOT_COMMON_H
|
|
||||||
|
|
||||||
#include <curl/curl.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
#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
|
|
||||||
+7
-7
@@ -5,16 +5,16 @@
|
|||||||
#include <json-c/json.h>
|
#include <json-c/json.h>
|
||||||
|
|
||||||
/* Retrieve update */
|
/* 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 */
|
/* Methods */
|
||||||
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);
|
||||||
tgbot_rc tgbot_send_message(const tgbot_s *bot, int64_t chat_id, const char *text, const char *parse_mode,
|
int 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(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);
|
||||||
|
|
||||||
/* Updating Methods */
|
/* Updating Methods */
|
||||||
tgbot_rc tgbot_edit_message_text(const tgbot_s *bot, int64_t chat_id, long message_id, const char *text,
|
int 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
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
#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
|
|
||||||
@@ -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
|
||||||
+2
-3
@@ -1,9 +1,8 @@
|
|||||||
#ifndef TGBOT_MAIN_H
|
#ifndef TGBOT_MAIN_H
|
||||||
#define TGBOT_MAIN_H
|
#define TGBOT_MAIN_H
|
||||||
|
|
||||||
#include "common.h"
|
|
||||||
#include "methods.h"
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Create a new Bot object.
|
* @brief Create a new Bot object.
|
||||||
@@ -21,4 +20,4 @@ tgbot_s *tgbot_new(const char *token);
|
|||||||
*/
|
*/
|
||||||
void tgbot_free(tgbot_s *bot);
|
void tgbot_free(tgbot_s *bot);
|
||||||
|
|
||||||
#endif // TGBOT_H
|
#endif
|
||||||
|
|||||||
+83
-71
@@ -1,71 +1,83 @@
|
|||||||
#ifndef TGBOT_TYPES_H
|
#ifndef TGBOT_TYPES_H
|
||||||
#define TGBOT_TYPES_H
|
#define TGBOT_TYPES_H
|
||||||
|
|
||||||
#include "common.h"
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
#define TEXT_SIZE 200
|
|
||||||
#define URL_SIZE 200
|
#define FIRSTNAME_SIZE 256
|
||||||
#define CB_DATA_SIZE 64
|
#define USERNAME_SIZE 32
|
||||||
|
|
||||||
/**
|
#define CHAT_LASTNAME_SIZE 256
|
||||||
* @brief Represents a single button on an inline keyboard.
|
#define CHAT_TYPE_SIZE 32
|
||||||
*/
|
#define CHAT_TEXT_SIZE 4096
|
||||||
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. */
|
* @brief Telegram bot struct.
|
||||||
char callback_data[CB_DATA_SIZE]; /**< (Optional) Data sent to the bot when the button is pressed. */
|
*/
|
||||||
} tgbot_inlinekeyboardbutton_s;
|
typedef struct tgbot tgbot_s;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Represents an inline keyboard.
|
* @brief A structure to represent Bot information got from getMe API.
|
||||||
*/
|
*/
|
||||||
typedef struct tgbot_inlinekeyboard {
|
typedef struct tgbot_me {
|
||||||
size_t rows; /**< Number of rows in the keyboard. */
|
char first_name[FIRSTNAME_SIZE]; /**< Bot's first name. */
|
||||||
size_t columns; /**< Number of columns per row. */
|
char username[USERNAME_SIZE]; /**< Bot's username. */
|
||||||
tgbot_inlinekeyboardbutton_s *buttons; /**< Array of buttons. */
|
} tgbot_me_s;
|
||||||
} tgbot_inlinekeyboard_s;
|
|
||||||
|
/**
|
||||||
/**
|
* @brief A structure to represent Update object.
|
||||||
* @brief Allocates a new inline keyboard.
|
*/
|
||||||
*
|
typedef struct tgbot_update {
|
||||||
* @param[in] rows Number of rows in the keyboard.
|
int64_t update_id; /**< Update id. */
|
||||||
* @param[in] columns Number of columns in each row.
|
long message_id; /**< Message id. */
|
||||||
*
|
int64_t chat_id; /**< Chat id. */
|
||||||
* @return The pointer to the keyboard or NULL on allocation failure.
|
char chat_first_name[FIRSTNAME_SIZE]; /**< Chat first name. */
|
||||||
*/
|
char chat_last_name[CHAT_LASTNAME_SIZE]; /**< Chat last name. */
|
||||||
tgbot_inlinekeyboard_s *tgbot_inlinekb_new(size_t rows, size_t columns);
|
char chat_username[USERNAME_SIZE]; /**< Chat username. */
|
||||||
|
char chat_type[CHAT_TYPE_SIZE]; /**< Chat type (private/public). */
|
||||||
/**
|
int32_t date; /**< Date in unix timestamp. */
|
||||||
* @brief Adds or updates a button at the specified position in the keyboard.
|
char text[CHAT_TEXT_SIZE]; /**< Message text. */
|
||||||
*
|
} tgbot_update_s;
|
||||||
* @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).
|
* @brief A structure to represent CallbackQuery object.
|
||||||
* @param[in] text Display text for the button.
|
*/
|
||||||
* @param[in] url Optional URL for the button.
|
typedef struct tgbot_cbquery {
|
||||||
* @param[in] callback_data Optional callback data for the button.
|
int64_t update_id;
|
||||||
*
|
long message_id;
|
||||||
* @return TGBOT_OK on success.
|
int64_t chat_id;
|
||||||
*/
|
char chat_username[USERNAME_SIZE];
|
||||||
tgbot_rc tgbot_inlinekb_button(tgbot_inlinekeyboard_s *keyboard, size_t row, size_t column, const char *text,
|
int32_t date;
|
||||||
const char *url, const char *callback_data);
|
char text[CHAT_TEXT_SIZE];
|
||||||
|
char chat_instance[128];
|
||||||
/**
|
char data[64]; /**> Callback data. */
|
||||||
* @brief Returns a pointer to the keyboard's button.
|
} tgbot_cbquery_s;
|
||||||
*
|
|
||||||
* @param[in] keyboard Keyboard.
|
/**
|
||||||
* @param[in] row Row index of the button.
|
* @brief Callback function pointer.
|
||||||
* @param[in] column Column index of the button.
|
*/
|
||||||
*
|
typedef void (*Callback)(tgbot_s *bot, tgbot_cbquery_s *query);
|
||||||
* @return Pointer to the button, or NULL if the position is invalid.
|
|
||||||
*/
|
#define TEXT_SIZE 200
|
||||||
tgbot_inlinekeyboardbutton_s *tgbot_inlinekb_button_at(tgbot_inlinekeyboard_s *keyboard, size_t row, size_t column);
|
#define URL_SIZE 200
|
||||||
|
#define CB_DATA_SIZE 64
|
||||||
/**
|
|
||||||
* @brief Frees all memory associated with the given inline keyboard.
|
/**
|
||||||
*
|
* @brief Represents a single button on an inline keyboard.
|
||||||
* @param[in,out] keyboard Pointer to the keyboard structure to deallocate.
|
*/
|
||||||
*/
|
typedef struct tgbot_inlinekeyboardbutton {
|
||||||
void tgbot_inlinekb_free(tgbot_inlinekeyboard_s *keyboard);
|
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. */
|
||||||
#endif
|
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
|
||||||
|
|||||||
+29
-18
@@ -1,8 +1,8 @@
|
|||||||
project(
|
project(
|
||||||
'tgbot',
|
'tgbot',
|
||||||
'c',
|
'c',
|
||||||
version: '0.1',
|
version: '0.1',
|
||||||
default_options: ['warning_level=3', 'c_std=c18'],
|
default_options: ['warning_level=3', 'c_std=c18'],
|
||||||
)
|
)
|
||||||
|
|
||||||
sources = []
|
sources = []
|
||||||
@@ -15,25 +15,36 @@ json_c_dep = dependency('json-c')
|
|||||||
|
|
||||||
deps = [curl_dep, json_c_dep]
|
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_lib = static_library(
|
||||||
'tgbot',
|
'tgbot',
|
||||||
sources,
|
sources,
|
||||||
dependencies: deps,
|
dependencies: deps,
|
||||||
include_directories: inc_dir,
|
include_directories: inc_dir,
|
||||||
install: true,
|
install: true,
|
||||||
)
|
)
|
||||||
|
|
||||||
tgbot_dep = declare_dependency(
|
tgbot_dep = declare_dependency(
|
||||||
link_with: tgbot_lib,
|
link_with: tgbot_lib,
|
||||||
include_directories: inc_dir,
|
include_directories: inc_dir,
|
||||||
dependencies: deps,
|
dependencies: deps,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Example
|
# Example
|
||||||
executable(
|
example = executable(
|
||||||
'example',
|
'example',
|
||||||
'examples/inlinekeyboard/inlinekeyboard.c',
|
'examples/inlinekeyboard/inlinekeyboard.c',
|
||||||
dependencies: tgbot_dep,
|
dependencies: tgbot_dep,
|
||||||
|
build_by_default: false,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
test('example_inlinekeyboard', example)
|
||||||
@@ -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
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
#ifndef TGBOT_JSON_H
|
#ifndef TGBOT_INTERNAL_JSON_H
|
||||||
#define TGBOT_JSON_H
|
#define TGBOT_INTERNAL_JSON_H
|
||||||
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include <json-c/json.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_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);
|
||||||
@@ -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
@@ -1,7 +1,9 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.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 *json_builder(tgbot_option_s *options, size_t optionslen) {
|
||||||
json_object *rjson = json_object_new_object();
|
json_object *rjson = json_object_new_object();
|
||||||
|
|||||||
+31
-35
@@ -2,11 +2,11 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "common.h"
|
#include "internal/common.h"
|
||||||
#include "json.h"
|
#include "internal/json.h"
|
||||||
|
#include "internal/parse.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]))
|
#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;
|
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();
|
CURL *curl = curl_easy_init();
|
||||||
if (!curl) {
|
if (!curl) {
|
||||||
return TGBOT_REQUEST_ERROR;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *json_string = NULL;
|
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;
|
*mb = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return TGBOT_REQUEST_ERROR;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
curl_easy_cleanup(curl);
|
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};
|
char url[URL_LEN] = {0};
|
||||||
snprintf(url, sizeof(url), "%s%s", bot->api, method);
|
snprintf(url, sizeof(url), "%s%s", bot->api, method);
|
||||||
|
|
||||||
json_object *rjson = json_builder(options, optlen);
|
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);
|
json_object_put(rjson);
|
||||||
|
|
||||||
return ret;
|
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];
|
char url[URL_LEN];
|
||||||
|
|
||||||
memset(update, 0, sizeof(tgbot_update_s));
|
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);
|
snprintf(url, sizeof(url), "%sgetUpdates", bot->api);
|
||||||
|
|
||||||
struct memory_buffer *mb;
|
struct memory_buffer *mb = {0};
|
||||||
tgbot_rc ret = tgbot_request(url, &mb, rjson);
|
int ret = tgbot_request(url, &mb, rjson);
|
||||||
json_object_put(rjson);
|
json_object_put(rjson);
|
||||||
if (ret != TGBOT_OK) {
|
if (ret != 0) {
|
||||||
if (mb) {
|
if (mb) {
|
||||||
free(mb->data);
|
free(mb->data);
|
||||||
free(mb);
|
free(mb);
|
||||||
}
|
}
|
||||||
|
|
||||||
return TGBOT_GETUPDATES_ERROR;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
json_object *json = json_tokener_parse(mb->data);
|
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)) {
|
if (!json_object_is_type(ok, json_type_boolean) || !json_object_get_boolean(ok)) {
|
||||||
json_object_put(json);
|
json_object_put(json);
|
||||||
|
|
||||||
return TGBOT_TELEGRAM_OK_ERROR;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
const json_object *results = json_object_object_get(json, "result");
|
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) {
|
if (results_len == 0) {
|
||||||
json_object_put(json);
|
json_object_put(json);
|
||||||
|
|
||||||
return TGBOT_OK;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check if it is a Message or a CallbackQuery*/
|
/* 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);
|
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];
|
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;
|
||||||
tgbot_rc ret = tgbot_request(url, &mb, NULL);
|
int ret = tgbot_request(url, &mb, NULL);
|
||||||
if (ret != TGBOT_OK) {
|
if (ret != 0) {
|
||||||
free(mb->data);
|
free(mb->data);
|
||||||
free(mb);
|
free(mb);
|
||||||
|
|
||||||
return TGBOT_GETME_ERROR;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
json_object *json = json_tokener_parse(mb->data);
|
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)) {
|
if (!json_object_get_boolean(ok)) {
|
||||||
json_object_put(json);
|
json_object_put(json);
|
||||||
|
|
||||||
return TGBOT_GETME_ERROR;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
const json_object *result = json_object_object_get(json, "result");
|
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);
|
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,
|
int 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) {
|
||||||
tgbot_option_s options[4] = {
|
tgbot_option_s 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},
|
||||||
@@ -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},
|
{"reply_markup", keyboard, tgbot_opt_inlinekeyboard},
|
||||||
};
|
};
|
||||||
|
|
||||||
return tgbot_execute_method(bot, "sendMessage", options, opt_size(options)) == TGBOT_OK ? TGBOT_OK
|
return tgbot_execute_method(bot, "sendMessage", options, opt_size(options));
|
||||||
: TGBOT_SENDMESSAGE_ERROR;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tgbot_rc tgbot_edit_message_text(const tgbot_s *bot, int64_t chat_id, long message_id, const char *text,
|
int 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) {
|
||||||
tgbot_option_s options[4] = {
|
tgbot_option_s 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},
|
||||||
@@ -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},
|
{"reply_markup", keyboard, tgbot_opt_inlinekeyboard},
|
||||||
};
|
};
|
||||||
|
|
||||||
return tgbot_execute_method(bot, "editMessageText", options, opt_size(options)) == TGBOT_OK
|
return tgbot_execute_method(bot, "editMessageText", options, opt_size(options));
|
||||||
? TGBOT_OK
|
|
||||||
: TGBOT_EDITMESSAGETEXT_ERROR;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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] = {
|
tgbot_option_s 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},
|
||||||
};
|
};
|
||||||
|
|
||||||
return tgbot_execute_method(bot, "sendDice", options, opt_size(options)) == TGBOT_OK ? TGBOT_OK
|
return tgbot_execute_method(bot, "sendDice", options, opt_size(options));
|
||||||
: TGBOT_SENDDICE_ERROR;
|
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-5
@@ -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");
|
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);
|
||||||
@@ -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));
|
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");
|
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);
|
||||||
@@ -96,5 +97,5 @@ tgbot_rc tgbot_parse_cbquery(tgbot_s *bot, tgbot_cbquery_s *query, json_object *
|
|||||||
|
|
||||||
cbq_handler(bot, query);
|
cbq_handler(bot, query);
|
||||||
|
|
||||||
return TGBOT_OK;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "internal/common.h"
|
||||||
#include "tgbot.h"
|
#include "tgbot.h"
|
||||||
|
|
||||||
tgbot_s *tgbot_new(const char *token) {
|
tgbot_s *tgbot_new(const char *token) {
|
||||||
|
|||||||
+3
-3
@@ -32,13 +32,13 @@ tgbot_inlinekeyboardbutton_s *tgbot_inlinekb_button_at(tgbot_inlinekeyboard_s *k
|
|||||||
return &keyboard->buttons[row * keyboard->columns + 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,
|
int tgbot_inlinekb_button(tgbot_inlinekeyboard_s *keyboard, size_t row, size_t column, const char *text,
|
||||||
const char *url, const char *callback_data) {
|
const char *url, const char *callback_data) {
|
||||||
tgbot_inlinekeyboardbutton_s *button = tgbot_inlinekb_button_at(keyboard, row, column);
|
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);
|
||||||
strncpy(button->callback_data, callback_data, sizeof(button->callback_data) - 1);
|
strncpy(button->callback_data, callback_data, sizeof(button->callback_data) - 1);
|
||||||
|
|
||||||
return TGBOT_OK;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user