refactor: move common to internal and update public api
This commit is contained in:
@@ -3,7 +3,8 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <tgbot.h>
|
||||
#include <tgbot/methods.h>
|
||||
#include <tgbot/tgbot.h>
|
||||
|
||||
bool run = true;
|
||||
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
#include <stdlib.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."
|
||||
|
||||
@@ -40,12 +42,12 @@ void sighandler(int signum) {
|
||||
}
|
||||
|
||||
void parse_command(const tgbot_s *bot, const tgbot_update_s *update) {
|
||||
tgbot_rc ret;
|
||||
int ret;
|
||||
|
||||
if (strcmp("/start", update->text) == 0) {
|
||||
ret = tgbot_send_message(bot, update->chat_id, WELCOME_MSG, "Markdown", keyboard);
|
||||
|
||||
if (ret != TGBOT_OK) {
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "Failed to send message\n");
|
||||
}
|
||||
|
||||
@@ -54,7 +56,7 @@ void parse_command(const tgbot_s *bot, const tgbot_update_s *update) {
|
||||
|
||||
/* Echo the message */
|
||||
ret = tgbot_send_message(bot, update->chat_id, update->text, "Markdown", NULL);
|
||||
if (ret != TGBOT_OK) {
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "Failed to send message\n");
|
||||
}
|
||||
}
|
||||
@@ -100,8 +102,8 @@ int main(void) {
|
||||
|
||||
/* Main loop */
|
||||
while (run) {
|
||||
tgbot_rc ret = tgbot_get_update(bot, &update, callback_handler);
|
||||
if (ret != TGBOT_OK) {
|
||||
int ret = tgbot_get_update(bot, &update, callback_handler);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "tgbot_get_updates()\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
+5
-5
@@ -5,16 +5,16 @@
|
||||
#include <json-c/json.h>
|
||||
|
||||
/* Retrieve update */
|
||||
tgbot_rc tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_handler);
|
||||
int tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_handler);
|
||||
|
||||
/* Methods */
|
||||
tgbot_rc tgbot_get_me(const tgbot_s *bot, tgbot_me_s *me);
|
||||
tgbot_rc tgbot_send_message(const tgbot_s *bot, int64_t chat_id, const char *text, const char *parse_mode,
|
||||
int tgbot_get_me(const tgbot_s *bot, tgbot_me_s *me);
|
||||
int tgbot_send_message(const tgbot_s *bot, int64_t chat_id, const char *text, const char *parse_mode,
|
||||
tgbot_inlinekeyboard_s *reply_markup);
|
||||
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 */
|
||||
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);
|
||||
|
||||
#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
|
||||
#define TGBOT_MAIN_H
|
||||
|
||||
#include "common.h"
|
||||
#include "methods.h"
|
||||
#include "types.h"
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* @brief Create a new Bot object.
|
||||
@@ -21,4 +20,4 @@ tgbot_s *tgbot_new(const char *token);
|
||||
*/
|
||||
void tgbot_free(tgbot_s *bot);
|
||||
|
||||
#endif // TGBOT_H
|
||||
#endif
|
||||
|
||||
+56
-44
@@ -1,7 +1,62 @@
|
||||
#ifndef TGBOT_TYPES_H
|
||||
#define TGBOT_TYPES_H
|
||||
|
||||
#include "common.h"
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define FIRSTNAME_SIZE 256
|
||||
#define USERNAME_SIZE 32
|
||||
|
||||
#define CHAT_LASTNAME_SIZE 256
|
||||
#define CHAT_TYPE_SIZE 32
|
||||
#define CHAT_TEXT_SIZE 4096
|
||||
|
||||
/**
|
||||
* @brief Telegram bot struct.
|
||||
*/
|
||||
typedef struct tgbot tgbot_s;
|
||||
|
||||
/**
|
||||
* @brief A structure to represent Bot information got from getMe API.
|
||||
*/
|
||||
typedef struct tgbot_me {
|
||||
char first_name[FIRSTNAME_SIZE]; /**< Bot's first name. */
|
||||
char username[USERNAME_SIZE]; /**< Bot's username. */
|
||||
} tgbot_me_s;
|
||||
|
||||
/**
|
||||
* @brief A structure to represent Update object.
|
||||
*/
|
||||
typedef struct tgbot_update {
|
||||
int64_t update_id; /**< Update id. */
|
||||
long message_id; /**< Message id. */
|
||||
int64_t chat_id; /**< Chat id. */
|
||||
char chat_first_name[FIRSTNAME_SIZE]; /**< Chat first name. */
|
||||
char chat_last_name[CHAT_LASTNAME_SIZE]; /**< Chat last name. */
|
||||
char chat_username[USERNAME_SIZE]; /**< Chat username. */
|
||||
char chat_type[CHAT_TYPE_SIZE]; /**< Chat type (private/public). */
|
||||
int32_t date; /**< Date in unix timestamp. */
|
||||
char text[CHAT_TEXT_SIZE]; /**< Message text. */
|
||||
} tgbot_update_s;
|
||||
|
||||
/**
|
||||
* @brief A structure to represent CallbackQuery object.
|
||||
*/
|
||||
typedef struct tgbot_cbquery {
|
||||
int64_t update_id;
|
||||
long message_id;
|
||||
int64_t chat_id;
|
||||
char chat_username[USERNAME_SIZE];
|
||||
int32_t date;
|
||||
char text[CHAT_TEXT_SIZE];
|
||||
char chat_instance[128];
|
||||
char data[64]; /**> Callback data. */
|
||||
} tgbot_cbquery_s;
|
||||
|
||||
/**
|
||||
* @brief Callback function pointer.
|
||||
*/
|
||||
typedef void (*Callback)(tgbot_s *bot, tgbot_cbquery_s *query);
|
||||
|
||||
#define TEXT_SIZE 200
|
||||
#define URL_SIZE 200
|
||||
@@ -25,47 +80,4 @@ typedef struct tgbot_inlinekeyboard {
|
||||
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
|
||||
|
||||
+14
-3
@@ -15,9 +15,17 @@ json_c_dep = dependency('json-c')
|
||||
|
||||
deps = [curl_dep, json_c_dep]
|
||||
|
||||
install_headers('include/tgbot.h')
|
||||
install_headers(
|
||||
[
|
||||
'include/methods.h',
|
||||
'include/tg_types.h',
|
||||
'include/tgbot.h',
|
||||
'include/types.h',
|
||||
],
|
||||
subdir: 'tgbot',
|
||||
)
|
||||
|
||||
tgbot_lib = library(
|
||||
tgbot_lib = static_library(
|
||||
'tgbot',
|
||||
sources,
|
||||
dependencies: deps,
|
||||
@@ -32,8 +40,11 @@ tgbot_dep = declare_dependency(
|
||||
)
|
||||
|
||||
# Example
|
||||
executable(
|
||||
example = executable(
|
||||
'example',
|
||||
'examples/inlinekeyboard/inlinekeyboard.c',
|
||||
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
|
||||
#define TGBOT_JSON_H
|
||||
#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);
|
||||
@@ -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 <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();
|
||||
|
||||
+29
-33
@@ -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,10 +192,10 @@ 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,
|
||||
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},
|
||||
@@ -204,11 +204,10 @@ 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,
|
||||
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},
|
||||
@@ -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
@@ -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;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "internal/common.h"
|
||||
#include "tgbot.h"
|
||||
|
||||
tgbot_s *tgbot_new(const char *token) {
|
||||
|
||||
+2
-2
@@ -32,7 +32,7 @@ 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,
|
||||
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);
|
||||
|
||||
@@ -40,5 +40,5 @@ tgbot_rc tgbot_inlinekb_button(tgbot_inlinekeyboard_s *keyboard, size_t row, siz
|
||||
strncpy(button->url, url, sizeof(button->url) - 1);
|
||||
strncpy(button->callback_data, callback_data, sizeof(button->callback_data) - 1);
|
||||
|
||||
return TGBOT_OK;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user