From f4ca5a717949b4ac0f1706a94cf6f5f6a0e03c57 Mon Sep 17 00:00:00 2001 From: Elmurod Talipov Date: Sun, 13 Nov 2022 00:13:53 +0900 Subject: [PATCH 1/5] Refactor code for readability Signed-off-by: Elmurod Talipov --- CMakeLists.txt | 2 +- include/telebot-core.h | 849 ++++++++++++++------------- include/telebot-parser.h | 2 +- include/telebot-private.h | 36 ++ include/telebot-types.h | 2 +- src/telebot-core.c | 1152 ++++++++++++++----------------------- src/telebot-parser.c | 11 +- src/telebot.c | 1067 +++++++++++++++++----------------- 8 files changed, 1451 insertions(+), 1670 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ca33532..cdf0897 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +CMAKE_MINIMUM_REQUIRED(VERSION 3.2) PROJECT(telebot C) SET(PROJECT_DESCRIPTION "Telegram Bot API") diff --git a/include/telebot-core.h b/include/telebot-core.h index cec6ef2..98cf0d7 100644 --- a/include/telebot-core.h +++ b/include/telebot-core.h @@ -23,7 +23,8 @@ #include #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif /** @@ -45,21 +46,36 @@ extern "C" { */ /** - * @brief This object represents a core telebot handler. + * @brief Telebot core handler opaque type. */ -typedef struct telebot_core_handler { - char *token; /**< Telegam bot token */ - char *proxy_addr; - char *proxy_auth; -} telebot_core_handler_t; +typedef struct telebot_core_handler *telebot_core_handler_t; /** - * @brief This object represents a telegram bot response. + * @brief Telebot core respone opaque type. */ -typedef struct telebot_core_response { - size_t size; /**< Telegam bot response size */ - char *data; /**< Telegam bot response object */ -} telebot_core_response_t; +typedef struct telebot_core_response *telebot_core_response_t; + +/** + * @brief Get response error code. + * @param[in] response Response to get its error code. + * @return on For successful responses TELEBOT_ERROR_NONE is returned, + * otherwise a negative error value @see #telebot_error_e + */ +telebot_error_e telebot_core_get_response_code(telebot_core_response_t response); + +/** + * @brief Get response data. + * @param[in] response Response to get its data. + * @return on For successful responses null terminated string is returned, + * otherwise NULL. + */ +const char *telebot_core_get_response_data(telebot_core_response_t response); + +/** + * @brief Release response data obtained with telebot core methods. + * @param[in] response Response to release. + */ +void telebot_core_put_response(telebot_core_response_t response); /** * @brief Start function to use telebot core APIs. @@ -67,12 +83,12 @@ typedef struct telebot_core_response { * This function must be used first to call, and it creates handler that is used * as input to other functions in telebot core interface. This call MUST have * corresponding call to #telebot_core_destroy when operation is complete. - * @param core_h[in,out] A pointer to a handler, which will be allocated and created. + * @param core_h[out] A pointer to a handler, which will be allocated and created. * Obtained handler MUST be released with #telebot_core_destroy() * @param token[in] Telegram bot token to use. * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. */ -telebot_error_e telebot_core_create(telebot_core_handler_t **core_h, const char *token); +telebot_error_e telebot_core_create(telebot_core_handler_t *core_h, const char *token); /** * @brief Final function to use telebot core APIs @@ -84,7 +100,7 @@ telebot_error_e telebot_core_create(telebot_core_handler_t **core_h, const char * @param[in,out] core_h The A pointer to telebot core handler created with #telebot_core_create(). * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. */ -telebot_error_e telebot_core_destroy(telebot_core_handler_t **core_h); +telebot_error_e telebot_core_destroy(telebot_core_handler_t *core_h); /** * @brief Set proxy address to use telebot behind proxy @@ -94,7 +110,7 @@ telebot_error_e telebot_core_destroy(telebot_core_handler_t **core_h); * @param[in] auth Proxy authorization informatio. * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. */ -telebot_error_e telebot_core_set_proxy(telebot_core_handler_t *core_h, const char *addr, +telebot_error_e telebot_core_set_proxy(telebot_core_handler_t core_h, const char *addr, const char *auth); /** @@ -104,7 +120,7 @@ telebot_error_e telebot_core_set_proxy(telebot_core_handler_t *core_h, const cha * @param[out] addr Current proxy address or NULL, MUST be freed after use. * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. */ -telebot_error_e telebot_core_get_proxy(telebot_core_handler_t *core_h, char **addr); +telebot_error_e telebot_core_get_proxy(telebot_core_handler_t core_h, char **addr); /** * @brief Receive incoming updates (long polling). It will not work if an outgoing @@ -122,12 +138,13 @@ telebot_error_e telebot_core_get_proxy(telebot_core_handler_t *core_h, char **ad * short polling. * @param[in] allowed_updates List the types of update you want your bot to receive. * Specify an empty list to receive all updates regardless of type (default). - * @param[out] response Response data, MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains updates, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_get_updates(telebot_core_handler_t *core_h, int offset, - int limit, int timeout, const char *allowed_updates, - telebot_core_response_t *response); +telebot_core_response_t telebot_core_get_updates(telebot_core_handler_t core_h, + int offset, int limit, int timeout, const char *allowed_updates); /** * @brief Specify a url and receive incoming updates via an outgoing webhook. @@ -145,39 +162,45 @@ telebot_error_e telebot_core_get_updates(telebot_core_handler_t *core_h, int off * @param[in] allowed_updates List the types of updates you want your bot to * receive. For example, specify ["message", "edited_channel_post", * "callback_query"] to only receive updates of these types. - * @param[out] response Response data, MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the result (true/false), + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_set_webhook(telebot_core_handler_t *core_h, const char *url, - const char *certificate, int max_connections, const char *allowed_updates, - telebot_core_response_t *response); +telebot_core_response_t telebot_core_set_webhook(telebot_core_handler_t core_h, + const char *url, const char *certificate, int max_connections, + const char *allowed_updates); /** * @brief Remove webhook integration if you decide to switch back to getUpdates. * @param[in] core_h The telebot core handler created with #telebot_core_create(). - * @param[out] response Response data, MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the result (true/false), + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_delete_webhook(telebot_core_handler_t *core_h, - telebot_core_response_t *response); +telebot_core_response_t telebot_core_delete_webhook(telebot_core_handler_t core_h); /** * @brief Get current webhook status. * @param[in] core_h The telebot core handler created with #telebot_core_create(). - * @param[out] response Response data, MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains webhook information, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_get_webhook_info(telebot_core_handler_t *core_h, - telebot_core_response_t *response); +telebot_core_response_t telebot_core_get_webhook_info(telebot_core_handler_t core_h); /** * @brief Get basic information about the bot. * @param[in] core_h The telebot core handler created with #telebot_core_create(). * @param[out] response Response data, MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains bot information, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_get_me(telebot_core_handler_t *core_h, - telebot_core_response_t *response); +telebot_core_response_t telebot_core_get_me(telebot_core_handler_t core_h); /** * @brief Send text messages. @@ -193,14 +216,15 @@ telebot_error_e telebot_core_get_me(telebot_core_handler_t *core_h, * @param[in] reply_to_message_id If the message is a reply, ID of the original message. * @param[in] reply_markup Additional interface options. An object for a custom * reply keyboard, instructions to hide keyboard or to force a reply from the user. - * @param[out] response Response data that contains the sent message on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the sent message, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_send_message(telebot_core_handler_t *core_h, +telebot_core_response_t telebot_core_send_message(telebot_core_handler_t core_h, long long int chat_id, const char *text, const char *parse_mode, - bool disable_web_page_preview, bool disable_notification, int reply_to_message_id, - const char *reply_markup, telebot_core_response_t *response); + bool disable_web_page_preview, bool disable_notification, + int reply_to_message_id, const char *reply_markup); /** * @brief Forward messages of any kind. @@ -212,13 +236,14 @@ telebot_error_e telebot_core_send_message(telebot_core_handler_t *core_h, * @param[in] disable_notification Sends the message silently. Users will receive a * notification with no sound. * @param[in] message_id Unique message identifier. - * @param[out] response Response data that contains the sent message on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the sent message, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_forward_message(telebot_core_handler_t *core_h, +telebot_core_response_t telebot_core_forward_message(telebot_core_handler_t core_h, long long int chat_id, long long int from_chat_id, bool disable_notification, - int message_id, telebot_core_response_t *response); + int message_id); /** * @brief Send photos. @@ -237,14 +262,15 @@ telebot_error_e telebot_core_forward_message(telebot_core_handler_t *core_h, * @param[in] reply_markup Additional interface options. An object for a custom * reply keyboard, instructions to hide keyboard or to force a reply from * the user. - * @param[out] response Response data that contains the sent message on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the sent message, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_send_photo(telebot_core_handler_t *core_h, +telebot_core_response_t telebot_core_send_photo(telebot_core_handler_t core_h, long long int chat_id, const char *photo, bool is_file, const char *caption, const char *parse_mode, bool disable_notification, int reply_to_message_id, - const char *reply_markup, telebot_core_response_t *response); + const char *reply_markup); /** * @brief Send audio files. if you want Telegram clients to display them in the @@ -275,15 +301,16 @@ telebot_error_e telebot_core_send_photo(telebot_core_handler_t *core_h, * @param[in] reply_to_message_id If the message is a reply, ID of the original message. * @param[in] reply_markup Additional interface options. An object for a custom reply * keyboard, instructions to hide keyboard or to force a reply from the user. - * @param[out] response Response data that contains the sent message on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the sent message, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_send_audio(telebot_core_handler_t *core_h, +telebot_core_response_t telebot_core_send_audio(telebot_core_handler_t core_h, long long int chat_id, const char *audio, bool is_file, const char *caption, const char *parse_mode, int duration, const char *performer, const char *title, const char *thumb, bool disable_notification, int reply_to_message_id, - const char *reply_markup, telebot_core_response_t *response); + const char *reply_markup); /** * @brief Send general files. @@ -305,15 +332,15 @@ telebot_error_e telebot_core_send_audio(telebot_core_handler_t *core_h, * @param[in] reply_to_message_id If the message is a reply, ID of the original message. * @param[in] reply_markup Additional interface options. An object for a custom reply * keyboard, instructions to hide keyboard or to force a reply from the user. - * @param[out] response Response data that contains the sent message on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the sent message, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_send_document(telebot_core_handler_t *core_h, +telebot_core_response_t telebot_core_send_document(telebot_core_handler_t core_h, long long int chat_id, const char *document, bool is_file, const char *thumb, const char *caption, const char *parse_mode, bool disable_notification, - int reply_to_message_id, const char *reply_markup, - telebot_core_response_t *response); + int reply_to_message_id, const char *reply_markup); /** * @brief Send video files, Telegram clients support mp4 videos (other formats @@ -340,16 +367,16 @@ telebot_error_e telebot_core_send_document(telebot_core_handler_t *core_h, * @param[in] reply_to_message_id If the message is a reply, ID of the original message. * @param[in] reply_markup Additional interface options. An object for a custom reply * keyboard, instructions to hide keyboard or to force a reply from the user. - * @param[out] response Response data that contains the sent message on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the sent message, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_send_video(telebot_core_handler_t *core_h, +telebot_core_response_t telebot_core_send_video(telebot_core_handler_t core_h, long long int chat_id, const char *video, bool is_file, int duration, int width, int height, const char *thumb, const char *caption, const char *parse_mode, bool supports_streaming, bool disable_notification, - int reply_to_message_id, const char *reply_markup, - telebot_core_response_t *response); + int reply_to_message_id, const char *reply_markup); /** * @brief Send animation files (GIF or H.264/MPEG-4 AVC without sound). @@ -373,15 +400,16 @@ telebot_error_e telebot_core_send_video(telebot_core_handler_t *core_h, * @param[in] reply_to_message_id If the message is a reply, ID of the original message. * @param[in] reply_markup Additional interface options. An object for a custom reply * keyboard, instructions to hide keyboard or to force a reply from the user. - * @param[out] response Response data that contains the sent message on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the sent message, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_send_animation(telebot_core_handler_t *core_h, +telebot_core_response_t telebot_core_send_animation(telebot_core_handler_t core_h, long long int chat_id, const char *animation, bool is_file, int duration, int width, int height, const char *thumb, const char *caption, const char *parse_mode, bool disable_notification, int reply_to_message_id, - const char *reply_markup, telebot_core_response_t *response); + const char *reply_markup); /** * @brief Send audio files, if you want Telegram clients to display the file as @@ -401,15 +429,15 @@ telebot_error_e telebot_core_send_animation(telebot_core_handler_t *core_h, * @param[in] reply_to_message_id If the message is a reply, ID of the original message. * @param[in] reply_markup Additional interface options. An object for a custom reply * keyboard, instructions to hide keyboard or to force a reply from the user. - * @param[out] response Response data that contains the sent message on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the sent message, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_send_voice(telebot_core_handler_t *core_h, +telebot_core_response_t telebot_core_send_voice(telebot_core_handler_t core_h, long long int chat_id, const char *voice, bool is_file, const char *caption, const char *parse_mode, int duration, bool disable_notification, - int reply_to_message_id, const char *reply_markup, - telebot_core_response_t *response); + int reply_to_message_id, const char *reply_markup); /** * @brief Send video messages. As of v.4.0, Telegram clients support rounded square @@ -431,14 +459,15 @@ telebot_error_e telebot_core_send_voice(telebot_core_handler_t *core_h, * @param[in] reply_to_message_id If the message is a reply, ID of the original message. * @param[in] reply_markup Additional interface options. An object for a custom reply * keyboard, instructions to hide keyboard or to force a reply from the user. - * @param[out] response Response data that contains the sent message on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the sent message, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_send_video_note(telebot_core_handler_t *core_h, +telebot_core_response_t telebot_core_send_video_note(telebot_core_handler_t core_h, long long int chat_id, char *video_note, bool is_file, int duration, int length, const char *thumb, bool disable_notification, int reply_to_message_id, - const char *reply_markup, telebot_core_response_t *response); + const char *reply_markup); /** * @brief Send point on the map. @@ -454,14 +483,14 @@ telebot_error_e telebot_core_send_video_note(telebot_core_handler_t *core_h, * @param[in] reply_to_message_id If the message is a reply, ID of the original message. * @param[in] reply_markup Additional interface options. An object for a custom reply * keyboard, instructions to hide keyboard or to force a reply from the user. - * @param[out] response Response data that contains the sent message on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the sent message, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_send_location(telebot_core_handler_t *core_h, +telebot_core_response_t telebot_core_send_location(telebot_core_handler_t core_h, long long int chat_id, float latitude, float longitude, int live_period, - bool disable_notification, int reply_to_message_id, const char *reply_markup, - telebot_core_response_t *response); + bool disable_notification, int reply_to_message_id, const char *reply_markup); /** * @brief Edit live location messages sent by the bot or via the bot (for inline @@ -477,14 +506,14 @@ telebot_error_e telebot_core_send_location(telebot_core_handler_t *core_h, * @param[in] latitude Latitude of location. * @param[in] longitude Longitude of location. * @param[in] reply_markup A JSON-serialized object for a new inline keyboard. - * @param[out] response Response data that contains the sent message on success. - * Otherwise, True is returned. It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the result (true/false), + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_edit_message_live_location(telebot_core_handler_t *core_h, +telebot_core_response_t telebot_core_edit_message_live_location(telebot_core_handler_t core_h, long long int chat_id, int message_id, const char *inline_message_id, - float latitude, float longitude, const char *reply_markup, - telebot_core_response_t *response); + float latitude, float longitude, const char *reply_markup); /** * @brief Stop updating a live location message sent by the bot or via the bot @@ -497,13 +526,14 @@ telebot_error_e telebot_core_edit_message_live_location(telebot_core_handler_t * * @param[in] inline_message_id Required if chat_id and message_id are not * specified. Identifier of the inline message. * @param[in] reply_markup A JSON-serialized object for a new inline keyboard. - * @param[out] response Response data that contains the sent message on success. - * Otherwise, True is returned. It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the result (true/false), + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_stop_message_live_location(telebot_core_handler_t *core_h, +telebot_core_response_t telebot_core_stop_message_live_location(telebot_core_handler_t core_h, long long int chat_id, int message_id, char *inline_message_id, - const char *reply_markup, telebot_core_response_t *response); + const char *reply_markup); /** * @brief Send information about a venue. @@ -522,15 +552,15 @@ telebot_error_e telebot_core_stop_message_live_location(telebot_core_handler_t * * @param[in] reply_markup Additional interface options. A JSON-serialized * object for an inline keyboard, custom reply keyboard, instructions to remove * reply keyboard or to force a reply from the user. - * @param[out] response Response data that contains the sent message on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains sent messageß, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_send_venue(telebot_core_handler_t *core_h, +telebot_core_response_t telebot_core_send_venue(telebot_core_handler_t core_h, long long int chat_id, float latitude, float longitude, const char *title, const char *address, const char *foursquare_id, const char *foursquare_type, - bool disable_notification, int reply_to_message_id, const char *reply_markup, - telebot_core_response_t *response); + bool disable_notification, int reply_to_message_id, const char *reply_markup); /** * @brief Send phone contacts. @@ -547,15 +577,15 @@ telebot_error_e telebot_core_send_venue(telebot_core_handler_t *core_h, * @param[in] reply_markup Additional interface options. A JSON-serialized * object for an inline keyboard, custom reply keyboard, instructions to remove * reply keyboard or to force a reply from the user. - * @param[out] response Response data that contains the sent message on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains sent message, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_send_contact(telebot_core_handler_t *core_h, +telebot_core_response_t telebot_core_send_contact(telebot_core_handler_t core_h, long long int chat_id, const char *phone_number, const char *first_name, const char *last_name, const char *vcard, bool disable_notification, - int reply_to_message_id, const char *reply_markup, - telebot_core_response_t *response); + int reply_to_message_id, const char *reply_markup); /** * @brief Send a native poll. @@ -579,16 +609,16 @@ telebot_error_e telebot_core_send_contact(telebot_core_handler_t *core_h, * @param[in] reply_markup Additional interface options. A JSON-serialized * object for an inline keyboard, custom reply keyboard, instructions to remove * reply keyboard or to force a reply from the user. - * @param[out] response Response data that contains the sent message on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains sent message, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_send_poll(telebot_core_handler_t *core_h, +telebot_core_response_t telebot_core_send_poll(telebot_core_handler_t core_h, long long int chat_id, const char *question, const char *options, bool is_anonymous, const char *type, bool allows_multiple_answers, int correct_option_id, bool is_closed, bool disable_notification, - int reply_to_message_id, const char *reply_markup, - telebot_core_response_t *response); + int reply_to_message_id, const char *reply_markup); /** * @brief Send a dice, which will have a random value from 1 to 6. @@ -601,153 +631,157 @@ telebot_error_e telebot_core_send_poll(telebot_core_handler_t *core_h, * @param[in] reply_markup Additional interface options. A JSON-serialized * object for an inline keyboard, custom reply keyboard, instructions to remove * reply keyboard or to force a reply from the user. - * @param[out] response Response data that contains the sent message on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains sent message, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_send_dice(telebot_core_handler_t *core_h, +telebot_core_response_t telebot_core_send_dice(telebot_core_handler_t core_h, long long int chat_id, bool disable_notification, - int reply_to_message_id, const char *reply_markup, - telebot_core_response_t *response); - -/** - * @brief Tell the user that something is happening on the bot's side. The status - * is set for 5 seconds or less (when a message arrives from your bot, Telegram - * clients clear its typing status). Example: The ImageBot needs some time to process - * a request and upload the image. Instead of sending a text message along the lines - * of "Retrieving image, please wait…", the bot may use #telebot_core_send_chat_action() - * with action = upload_photo. The user will see a "sending photo" status for the bot. - * It is only recommended to use when a response from the bot will take a noticeable - * amount of time to arrive. - * @param[in] core_h The telebot core handler created with #telebot_core_create(). - * @param[in] chat_id Unique identifier for the target chat or username of the - * target channel (in the format \@channelusername) - * @param[in] action Type of action to broadcast. Choose one, depending on what the - * user is about to receive: typing for text messages, upload_photo for photos, - * record_video or upload_video for videos, record_audio or upload_audio for - * audio files, upload_document for general files, find_location for location - * data. - * @param[out] response Response data that contains "True" on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. - */ -telebot_error_e telebot_core_send_chat_action(telebot_core_handler_t *core_h, - long long int chat_id, const char *action, telebot_core_response_t *response); + int reply_to_message_id, const char *reply_markup); /** - * @brief Get user profile pictures object - * @param[in] core_h The telebot core handler created with #telebot_core_create(). - * @param[in] user_id Unique identifier of the target user. - * @param[in] offset Sequential number of the first photo to be returned. By default, - * all photos are returned. - * @param[in] limit Limits the number of photos to be retrieved. Values between - * 1—100 are accepted. Defaults to 100. - * @param[out] response Response data that contains user's profile photos on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. - */ -telebot_error_e telebot_core_get_user_profile_photos(telebot_core_handler_t *core_h, - int user_id, int offset, int limit, telebot_core_response_t *response); +* @brief Tell the user that something is happening on the bot's side. The status +* is set for 5 seconds or less (when a message arrives from your bot, Telegram +* clients clear its typing status). Example: The ImageBot needs some time to process +* a request and upload the image. Instead of sending a text message along the lines +* of "Retrieving image, please wait…", the bot may use #telebot_core_send_chat_action() +* with action = upload_photo. The user will see a "sending photo" status for the bot. +* It is only recommended to use when a response from the bot will take a noticeable +* amount of time to arrive. +* @param[in] core_h The telebot core handler created with #telebot_core_create(). +* @param[in] chat_id Unique identifier for the target chat or username of the +* target channel (in the format \@channelusername) +* @param[in] action Type of action to broadcast. Choose one, depending on what the +* user is about to receive: typing for text messages, upload_photo for photos, +* record_video or upload_video for videos, record_audio or upload_audio for +* audio files, upload_document for general files, find_location for location +* data. +* @return #telebot_core_response_t response that contains sent message, +* which MUST be released with #telebot_core_put_response(), or null if allocation fails. +* Response code should be checked with #teleobot_core_get_response_code(), +* before getting data with #telebot_core_get_response_data(). +*/ +telebot_core_response_t telebot_core_send_chat_action(telebot_core_handler_t core_h, + long long int chat_id, const char *action); /** - * @brief Get basic info about a file and prepare it for - * downloading. For the moment, bots can download files of up to 20MB in size. - * @param[in] core_h The telebot core handler created with #telebot_core_create(). - * @param[in] file_id File identifier to get info about. - * @param[out] response Response data that contains file object on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. - */ -telebot_error_e telebot_core_get_file(telebot_core_handler_t *core_h, const char *file_id, - telebot_core_response_t *response); +* @brief Get user profile pictures object +* @param[in] core_h The telebot core handler created with #telebot_core_create(). +* @param[in] user_id Unique identifier of the target user. +* @param[in] offset Sequential number of the first photo to be returned. By default, +* all photos are returned. +* @param[in] limit Limits the number of photos to be retrieved. Values between +* 1—100 are accepted. Defaults to 100. +* @return #telebot_core_response_t response that contains user profile photos, +* which MUST be released with #telebot_core_put_response(), or null if allocation fails. +* Response code should be checked with #teleobot_core_get_response_code(), +* before getting data with #telebot_core_get_response_data(). +*/ +telebot_core_response_t telebot_core_get_user_profile_photos(telebot_core_handler_t core_h, + int user_id, int offset, int limit); /** - * @brief Download file using file_path obtained with - * telebot_core_get_file(). It is guaranteed that the link will be valid for - * at least 1 hour. When the link expires, a new one can be requested by - * calling telebot_core_get_file() again. - * @param[in] core_h The telebot core handler created with #telebot_core_create(). - * @param[in] file_path A file path take from the response of telebot_core_get_file() - * @param[in] out_file Full path to download and save file. - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative - * error value. No response, i.e., core_h->resp_data contains nothing. - */ -telebot_error_e telebot_core_download_file(telebot_core_handler_t *core_h, +* @brief Get basic info about a file and prepare it for +* downloading. For the moment, bots can download files of up to 20MB in size. +* @param[in] core_h The telebot core handler created with #telebot_core_create(). +* @param[in] file_id File identifier to get info about. +* @return #telebot_core_response_t response that contains file object, +* which MUST be released with #telebot_core_put_response(), or null if allocation fails. +* Response code should be checked with #teleobot_core_get_response_code(), +* before getting data with #telebot_core_get_response_data(). +*/ +telebot_core_response_t telebot_core_get_file(telebot_core_handler_t core_h, const char *file_id); + +/** +* @brief Download file using file_path obtained with +* telebot_core_get_file(). It is guaranteed that the link will be valid for +* at least 1 hour. When the link expires, a new one can be requested by +* calling telebot_core_get_file() again. +* @param[in] core_h The telebot core handler created with #telebot_core_create(). +* @param[in] file_path A file path take from the response of telebot_core_get_file() +* @param[in] out_file Full path to download and save file. +* @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative +* error value. No response, i.e., core_h->resp_data contains nothing. +*/ +telebot_error_e telebot_core_download_file(telebot_core_handler_t core_h, const char *file_path, const char *out_file); /** - * @brief Kick a user from a group, a supergroup or a channel. In the case of - * supergroups and channels, the user will not be able to return to the group on - * their own using invite links, etc., unless unbanned first. The bot must be an - * administrator in the chat for this to work and must have the appropriate admin - * rights. - * @param[in] core_h The telebot core handler created with #telebot_core_create(). - * @param[in] chat_id Unique identifier for the target chat or username of the - * target channel (in the format \@channelusername) - * @param[in] user_id Unique identifier of the target user - * @param[in] until_date Date when the user will be unbanned, unix time. If user is - * banned for more than 366 days or less than 30 seconds from the current time - * they are considered to be banned forever. - * @param[out] response Response data that contains "True" on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. - */ -telebot_error_e telebot_core_kick_chat_member(telebot_core_handler_t *core_h, - long long int chat_id, int user_id, long until_date, - telebot_core_response_t *response); +* @brief Kick a user from a group, a supergroup or a channel. In the case of +* supergroups and channels, the user will not be able to return to the group on +* their own using invite links, etc., unless unbanned first. The bot must be an +* administrator in the chat for this to work and must have the appropriate admin +* rights. +* @param[in] core_h The telebot core handler created with #telebot_core_create(). +* @param[in] chat_id Unique identifier for the target chat or username of the +* target channel (in the format \@channelusername) +* @param[in] user_id Unique identifier of the target user +* @param[in] until_date Date when the user will be unbanned, unix time. If user is +* banned for more than 366 days or less than 30 seconds from the current time +* they are considered to be banned forever. +* @return #telebot_core_response_t response that contains the result (true/false), +* which MUST be released with #telebot_core_put_response(), or null if allocation fails. +* Response code should be checked with #teleobot_core_get_response_code(), +* before getting data with #telebot_core_get_response_data(). +*/ +telebot_core_response_t telebot_core_kick_chat_member(telebot_core_handler_t core_h, + long long int chat_id, int user_id, long until_date); /** - * @brief Unban a previously kicked user in a supergroup or channel. The user will - * not return to the group or channel automatically, but will be able to join via - * link, etc. The bot must be an administrator for this to work. - * @param[in] core_h The telebot core handler created with #telebot_core_create(). - * @param[in] chat_id Unique identifier for the target chat or username of the - * target channel (in the format \@channelusername) - * @param[in] user_id Unique identifier of the target user - * @param[out] response Response data that contains "True" on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. - */ -telebot_error_e telebot_core_unban_chat_member(telebot_core_handler_t *core_h, - long long int chat_id, int user_id, telebot_core_response_t *response); +* @brief Unban a previously kicked user in a supergroup or channel. The user will +* not return to the group or channel automatically, but will be able to join via +* link, etc. The bot must be an administrator for this to work. +* @param[in] core_h The telebot core handler created with #telebot_core_create(). +* @param[in] chat_id Unique identifier for the target chat or username of the +* target channel (in the format \@channelusername) +* @param[in] user_id Unique identifier of the target user +* @return #telebot_core_response_t response that contains the result (true/false), +* which MUST be released with #telebot_core_put_response(), or null if allocation fails. +* Response code should be checked with #teleobot_core_get_response_code(), +* before getting data with #telebot_core_get_response_data(). +*/ +telebot_core_response_t telebot_core_unban_chat_member(telebot_core_handler_t core_h, + long long int chat_id, int user_id); /** - * @brief Restrict a user in a supergroup. The bot must be an administrator in - * the supergroup for this to work and must have the appropriate admin rights. - * Pass true for all boolean parameters to lift restrictions from a user. - * @param[in] core_h The telebot core handler created with #telebot_core_create(). - * @param[in] chat_id Unique identifier for the target chat or username of the - * target channel (in the format \@channelusername) - * @param[in] user_id Unique identifier of the target user. - * @param[in] until_date Date when restrictions will be lifted for the user, unix - * time. If user is restricted for more than 366 days or less than 30 seconds - * from the current time, they are considered to be restricted forever. - * @param[in] can_send_messages Pass true, if the user is allowed to send text - * messages, contacts, locations and venues - * @param[in] can_send_media_messages Pass true, if the user is allowed to send - * audios, documents, photos, videos, video notes and voice notes, - * implies can_send_messages - * @param[in can_send_polls Pass true, if the user is allowed to send polls, - * implies can_send_messages - * @param[in] can_send_other_messages Pass true, if the user can send animations, - * games, stickers and use inline bots, implies can_send_media_messages - * @param[in] can_add_web_page_previews Pass true, if the user may add web page - * previews to their messages, implies can_send_media_messages. - * @param[in] can_change_info Pass true, if the user is allowed to change the - * chat title, photo and other settings. Ignored in public supergroups. - * @param[in] can_invite_users Pass true, if the user is allowed to invite - * new users to the chat. - * @param[in] can_pin_messages Pass true, if the user is allowed to pin messages. - * Ignored in public supergroups - * @param[out] response Response data that contains "True" on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. - */ -telebot_error_e telebot_core_restrict_chat_member(telebot_core_handler_t *core_h, +* @brief Restrict a user in a supergroup. The bot must be an administrator in +* the supergroup for this to work and must have the appropriate admin rights. +* Pass true for all boolean parameters to lift restrictions from a user. +* @param[in] core_h The telebot core handler created with #telebot_core_create(). +* @param[in] chat_id Unique identifier for the target chat or username of the +* target channel (in the format \@channelusername) +* @param[in] user_id Unique identifier of the target user. +* @param[in] until_date Date when restrictions will be lifted for the user, unix +* time. If user is restricted for more than 366 days or less than 30 seconds +* from the current time, they are considered to be restricted forever. +* @param[in] can_send_messages Pass true, if the user is allowed to send text +* messages, contacts, locations and venues +* @param[in] can_send_media_messages Pass true, if the user is allowed to send +* audios, documents, photos, videos, video notes and voice notes, +* implies can_send_messages +* @param[in can_send_polls Pass true, if the user is allowed to send polls, +* implies can_send_messages +* @param[in] can_send_other_messages Pass true, if the user can send animations, +* games, stickers and use inline bots, implies can_send_media_messages +* @param[in] can_add_web_page_previews Pass true, if the user may add web page +* previews to their messages, implies can_send_media_messages. +* @param[in] can_change_info Pass true, if the user is allowed to change the +* chat title, photo and other settings. Ignored in public supergroups. +* @param[in] can_invite_users Pass true, if the user is allowed to invite +* new users to the chat. +* @param[in] can_pin_messages Pass true, if the user is allowed to pin messages. +* Ignored in public supergroups +* @return #telebot_core_response_t response that contains the result (true/false), +* which MUST be released with #telebot_core_put_response(), or null if allocation fails. +* Response code should be checked with #teleobot_core_get_response_code(), +* before getting data with #telebot_core_get_response_data(). +*/ +telebot_core_response_t telebot_core_restrict_chat_member(telebot_core_handler_t core_h, long long int chat_id, int user_id, long until_date, bool can_send_messages, bool can_send_media_messages, bool can_send_polls, bool can_send_other_messages, bool can_add_web_page_previews, bool can_change_info, bool can_invite_users, - bool can_pin_messages, telebot_core_response_t *response); + bool can_pin_messages); /** * @brief Promote or demote a user in a supergroup or a channel. The bot must be @@ -775,15 +809,15 @@ telebot_error_e telebot_core_restrict_chat_member(telebot_core_handler_t *core_h * administrators with a subset of his own privileges or demote administrators * that he has promoted, directly or indirectly (promoted by administrators * that were appointed by him). - * @param[out] response Response data that contains "True" on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the result (true/false), + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_promote_chat_member(telebot_core_handler_t *core_h, +telebot_core_response_t telebot_core_promote_chat_member(telebot_core_handler_t core_h, long long int chat_id, int user_id, bool can_change_info, bool can_post_messages, bool can_edit_messages, bool can_delete_messages, bool can_invite_users, - bool can_restrict_members, bool can_pin_messages, bool can_promote_members, - telebot_core_response_t *response); + bool can_restrict_members, bool can_pin_messages, bool can_promote_members); /** * @brief Export an invite link to a supergroup or a channel. The bot must be @@ -795,13 +829,13 @@ telebot_error_e telebot_core_promote_chat_member(telebot_core_handler_t *core_h, * @param[in] user_id Unique identifier of the target user. * @param[in] custom_title New custom title for the administrator; 0-16 characters, * emoji are not allowed. - * @param[out] response Response data that contains "True" on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the result (true/false), + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_set_chat_admin_custom_title(telebot_core_handler_t *core_h, - long long int chat_id, int user_id, const char *custom_title, - telebot_core_response_t *response); +telebot_core_response_t telebot_core_set_chat_admin_custom_title(telebot_core_handler_t core_h, + long long int chat_id, int user_id, const char *custom_title); /** * @brief Set default chat permissions for all members. The bot must be an administrator @@ -827,15 +861,15 @@ telebot_error_e telebot_core_set_chat_admin_custom_title(telebot_core_handler_t * new users to the chat. * @param[in] can_pin_messages Pass true, if the user is allowed to pin messages. * Ignored in public supergroups - * @param[out] response Response data that contains "True" on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the result (true/false), + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_set_chat_permissions(telebot_core_handler_t *core_h, +telebot_core_response_t telebot_core_set_chat_permissions(telebot_core_handler_t core_h, long long int chat_id, bool can_send_messages, bool can_send_media_messages, bool can_send_polls, bool can_send_other_messages, bool can_add_web_page_previews, - bool can_change_info, bool can_invite_users, bool can_pin_messages, - telebot_core_response_t *response); + bool can_change_info, bool can_invite_users, bool can_pin_messages); /** * @brief Generate a new invite link for a chat; any previously generated link is @@ -844,12 +878,13 @@ telebot_error_e telebot_core_set_chat_permissions(telebot_core_handler_t *core_h * @param[in] core_h The telebot core handler created with #telebot_core_create(). * @param[in] chat_id Unique identifier for the target chat or username of the * target channel (in the format \@channelusername). - * @param[out] response Response data that contains the new invite link on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains new invite link, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_export_chat_invite_link(telebot_core_handler_t *core_h, - long long int chat_id, telebot_core_response_t *response); +telebot_core_response_t telebot_core_export_chat_invite_link(telebot_core_handler_t core_h, + long long int chat_id); /** * @brief Set a new profile photo for the chat. Photos can't be changed for private @@ -859,12 +894,13 @@ telebot_error_e telebot_core_export_chat_invite_link(telebot_core_handler_t *cor * @param[in] chat_id Unique identifier for the target chat or username of the * target channel (in the format \@channelusername). * @param[in] photo New chat photo file path. - * @param[out] response Response data that contains "True" on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the result (true/false), + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_set_chat_photo(telebot_core_handler_t *core_h, - long long int chat_id, const char *photo, telebot_core_response_t *response); +telebot_core_response_t telebot_core_set_chat_photo(telebot_core_handler_t core_h, + long long int chat_id, const char *photo); /** * @brief Delete a chat photo. Photos can't be changed for private chats. The bot @@ -873,12 +909,13 @@ telebot_error_e telebot_core_set_chat_photo(telebot_core_handler_t *core_h, * @param[in] core_h The telebot core handler created with #telebot_core_create(). * @param[in] chat_id Unique identifier for the target chat or username of the * target channel (in the format \@channelusername). - * @param[out] response Response data that contains "True" on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the result (true/false), + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_delete_chat_photo(telebot_core_handler_t *core_h, - long long int chat_id, telebot_core_response_t *response); +telebot_core_response_t telebot_core_delete_chat_photo(telebot_core_handler_t core_h, + long long int chat_id); /** * @brief Change the title of a chat. Titles can't be @@ -888,12 +925,13 @@ telebot_error_e telebot_core_delete_chat_photo(telebot_core_handler_t *core_h, * @param[in] chat_id Unique identifier for the target chat or username of the * target channel (in the format \@channelusername). * @param[in] title New chat title, 1-255 characters. - * @param[out] response Response data that contains "True" on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the result (true/false), + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_set_chat_title(telebot_core_handler_t *core_h, - long long int chat_id, const char *title, telebot_core_response_t *response); +telebot_core_response_t telebot_core_set_chat_title(telebot_core_handler_t core_h, + long long int chat_id, const char *title); /** * @brief Change the description of a supergroup or a channel. The bot must be @@ -903,13 +941,13 @@ telebot_error_e telebot_core_set_chat_title(telebot_core_handler_t *core_h, * @param[in] chat_id Unique identifier for the target chat or username of the * target channel (in the format \@channelusername). * @param[in] description New chat description, 0-255 characters. - * @param[out] response Response data that contains "True" on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the result (true/false), + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_set_chat_description(telebot_core_handler_t *core_h, - long long int chat_id, const char *description, - telebot_core_response_t *response); +telebot_core_response_t telebot_core_set_chat_description(telebot_core_handler_t core_h, + long long int chat_id, const char *description); /** * @brief Pin a message in a supergroup or a channel. The bot must be an administrator @@ -922,13 +960,13 @@ telebot_error_e telebot_core_set_chat_description(telebot_core_handler_t *core_h * @param[in] disable_notification Pass True, if it is not necessary to send * a notification to all chat members about the new pinned message. * Notifications are always disabled in channels. - * @param[out] response Response data that contains "True" on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the result (true/false), + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_pin_chat_message(telebot_core_handler_t *core_h, - long long int chat_id, int message_id, bool disable_notification, - telebot_core_response_t *response); +telebot_core_response_t telebot_core_pin_chat_message(telebot_core_handler_t core_h, + long long int chat_id, int message_id, bool disable_notification); /** * @brief Unpin a message in a supergroup or a channel. The bot must be an administrator @@ -937,24 +975,25 @@ telebot_error_e telebot_core_pin_chat_message(telebot_core_handler_t *core_h, * @param[in] core_h The telebot core handler created with #telebot_core_create(). * @param[in] chat_id Unique identifier for the target chat or username of the * target channel (in the format \@channelusername). - * @param[out] response Response data that contains "True" on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the result (true/false), + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_unpin_chat_message(telebot_core_handler_t *core_h, - long long int chat_id, telebot_core_response_t *response); +telebot_core_response_t telebot_core_unpin_chat_message(telebot_core_handler_t core_h, + long long int chat_id); /** * @brief Leave a group, supergroup or channel. * @param[in] core_h The telebot core handler created with #telebot_core_create(). * @param[in] chat_id Unique identifier for the target chat or username of the * target channel (in the format \@channelusername). - * @param[out] response Response data that contains "True" on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the result (true/false), + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_leave_chat(telebot_core_handler_t *core_h, - long long int chat_id, telebot_core_response_t *response); +telebot_core_response_t telebot_core_leave_chat(telebot_core_handler_t core_h, long long int chat_id); /** * @brief Get up to date information about the chat (current name of the user for @@ -962,12 +1001,13 @@ telebot_error_e telebot_core_leave_chat(telebot_core_handler_t *core_h, * @param[in] core_h The telebot core handler created with #telebot_core_create(). * @param[in] chat_id Unique identifier for the target chat or username of the * target channel (in the format \@channelusername). - * @param[out] response Response data that contains a Chat object on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains chat object, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_get_chat(telebot_core_handler_t *core_h, - long long int chat_id, telebot_core_response_t *response); +telebot_core_response_t telebot_core_get_chat(telebot_core_handler_t core_h, + long long int chat_id); /** * @brief Get a list of administrators in a chat. Response contains an array of @@ -977,24 +1017,26 @@ telebot_error_e telebot_core_get_chat(telebot_core_handler_t *core_h, * @param[in] core_h The telebot core handler created with #telebot_core_create(). * @param[in] chat_id Unique identifier for the target chat or username of the * target channel (in the format \@channelusername). - * @param[out] response Response data that contains a array of chat members on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains an array of chat members, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_get_chat_admins(telebot_core_handler_t *core_h, - long long int chat_id, telebot_core_response_t *response); +telebot_core_response_t telebot_core_get_chat_admins(telebot_core_handler_t core_h, + long long int chat_id); /** * @brief Get the number of members in a chat. * @param[in] core_h The telebot core handler created with #telebot_core_create(). * @param[in] chat_id Unique identifier for the target chat or username of the * target channel (in the format \@channelusername). - * @param[out] response Response data that contains number of chat members on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the number of chat members, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_get_chat_members_count(telebot_core_handler_t *core_h, - long long int chat_id, telebot_core_response_t *response); +telebot_core_response_t telebot_core_get_chat_members_count(telebot_core_handler_t core_h, + long long int chat_id); /** * @brief Get information about a member of a chat. @@ -1002,12 +1044,13 @@ telebot_error_e telebot_core_get_chat_members_count(telebot_core_handler_t *core * @param[in] chat_id Unique identifier for the target chat or username of the * target channel (in the format \@channelusername). * @param[in] user_id Unique identifier of the target user. - * @param[out] response Response data that contains a chat member on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains a chat member, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_get_chat_member(telebot_core_handler_t *core_h, - long long int chat_id, int user_id, telebot_core_response_t *response); +telebot_core_response_t telebot_core_get_chat_member(telebot_core_handler_t core_h, + long long int chat_id, int user_id); /** * @brief Set a new group sticker set for a supergroup. The bot must be an administrator @@ -1019,13 +1062,13 @@ telebot_error_e telebot_core_get_chat_member(telebot_core_handler_t *core_h, * target channel (in the format \@channelusername). * @param[in] sticker_set_name Name of the sticker set to be set as the group * sticker set. - * @param[out] response Response data that contains "True" on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the result (true/false), + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_set_chat_sticker_set(telebot_core_handler_t *core_h, - long long int chat_id, const char *sticker_set_name, - telebot_core_response_t *response); +telebot_core_response_t telebot_core_set_chat_sticker_set(telebot_core_handler_t core_h, + long long int chat_id, const char *sticker_set_name); /** * @brief Delete a group sticker set from a supergroup. The bot must be an administrator @@ -1035,12 +1078,13 @@ telebot_error_e telebot_core_set_chat_sticker_set(telebot_core_handler_t *core_h * @param[in] core_h The telebot core handler created with #telebot_core_create(). * @param[in] chat_id Unique identifier for the target chat or username of the * target channel (in the format \@channelusername). - * @param[out] response Response data that contains "True" on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the result (true/false), + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_delete_chat_sticker_set(telebot_core_handler_t *core_h, - long long int chat_id, telebot_core_response_t *response); +telebot_core_response_t telebot_core_delete_chat_sticker_set(telebot_core_handler_t core_h, + long long int chat_id); /** * @brief Send answers to callback queries sent from inline keyboards. @@ -1059,35 +1103,37 @@ telebot_error_e telebot_core_delete_chat_sticker_set(telebot_core_handler_t *cor * @param[in] cache_time The maximum amount of time in seconds that the result of * the callback query may be cached client-side. Telegram apps will support * caching starting in version 3.14. - * @param[out] response Response data that contains "True" on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the result (true/false), + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_answer_callback_query(telebot_core_handler_t *core_h, +telebot_core_response_t telebot_core_answer_callback_query(telebot_core_handler_t core_h, const char *callback_query_id, const char *text, bool show_alert, - const char *url, int cache_time, telebot_core_response_t *response); + const char *url, int cache_time); /** * @brief Change the list of the bot's commands. * @param[in] core_h The telebot core handler created with #telebot_core_create(). * @param[in] commands A JSON-serialized list of bot commands to be set as the * list of the bot's commands. At most 100 commands can be specified. - * @param[out] response Response data that contains "True" on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the result (true/false), + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_set_my_commands(telebot_core_handler_t *core_h, - const char *commands, telebot_core_response_t *response); +telebot_core_response_t telebot_core_set_my_commands(telebot_core_handler_t core_h, + const char *commands); /** * @brief Get the current list of the bot's commands.. * @param[in] core_h The telebot core handler created with #telebot_core_create(). - * @param[out] response Response data that contains "True" on success. - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains an array of bot commands, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_get_my_commands(telebot_core_handler_t *core_h, - telebot_core_response_t *response); +telebot_core_response_t telebot_core_get_my_commands(telebot_core_handler_t core_h); /** * @brief Edit text and game messages sent by the bot or via the bot (for inline bots). @@ -1103,14 +1149,15 @@ telebot_error_e telebot_core_get_my_commands(telebot_core_handler_t *core_h, * bold, italic, fixed-width text or inline URLs in your bot's message. * @param[in] disable_web_page_priview Disables link previews for links in this message. * @param[in] reply_markup A JSON-serialized object for an inline keyboard. - * @param[out] response Response data that contains the message sent on succes, - * otherwise "True". It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the message edited, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_edit_message_text(telebot_core_handler_t *core_h, +telebot_core_response_t telebot_core_edit_message_text(telebot_core_handler_t core_h, long long int chat_id, int message_id, const char *inline_message_id, const char *text, const char *parse_mode, bool disable_web_page_preview, - const char *reply_markup, telebot_core_response_t *response); + const char *reply_markup); /** * @brief Edit captions of messages sent by the bot or via the bot (for inline bots). @@ -1123,14 +1170,14 @@ telebot_error_e telebot_core_edit_message_text(telebot_core_handler_t *core_h, * specified. Identifier of the inline message. * @param[in] caption New caption of the message. * @param[in] reply_markup A JSON-serialized object for an inline keyboard. - * @param[out] response Response data that contains the message sent on succes, - * otherwise "True". It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the message edited, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ -telebot_error_e telebot_core_edit_message_caption(telebot_core_handler_t *core_h, +telebot_core_response_t telebot_core_edit_message_caption(telebot_core_handler_t core_h, long long int chat_id, int message_id, const char *inline_message_id, - const char *caption, const char *parse_mode, const char *reply_markup, - telebot_core_response_t *response); + const char *caption, const char *parse_mode, const char *reply_markup); /** * @brief Edit only the reply markup of messages sent by the bot or via the @@ -1143,14 +1190,14 @@ telebot_error_e telebot_core_edit_message_caption(telebot_core_handler_t *core_h * @param[in] inline_message_id Required if chat_id and message_id are not * specified. Identifier of the inline message. * @param[in] reply_markup A JSON-serialized object for an inline keyboard. - * @param[out] response Response data that contains the message sent on succes, - * otherwise "True". It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the message edited, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ - -telebot_error_e telebot_core_edit_message_reply_markup(telebot_core_handler_t *core_h, +telebot_core_response_t telebot_core_edit_message_reply_markup(telebot_core_handler_t core_h, long long int chat_id, int message_id, const char *inline_message_id, - const char *reply_markup, telebot_core_response_t *response); + const char *reply_markup); /** * @brief Stop a poll which was sent by the bot. @@ -1162,42 +1209,34 @@ telebot_error_e telebot_core_edit_message_reply_markup(telebot_core_handler_t *c * @param[in] inline_message_id Required if chat_id and message_id are not * specified. Identifier of the inline message. * @param[in] reply_markup A JSON-serialized object for an inline keyboard. - * @param[out] response Response data that contains the message sent on succes, - * otherwise "True". It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. + * @return #telebot_core_response_t response that contains the stopped poll, + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). */ - -telebot_error_e telebot_core_stop_poll(telebot_core_handler_t *core_h, - long long int chat_id, int message_id, const char *reply_markup, - telebot_core_response_t *response); - +telebot_core_response_t telebot_core_stop_poll(telebot_core_handler_t core_h, + long long int chat_id, int message_id, const char *reply_markup); /** * @brief Delete a message, including service messages, with the following limitations: - * - A message can only be deleted if it was sent less than 48 hours ago. - * - Bots can delete outgoing messages in groups and supergroups. - * - Bots granted can_post_messages permissions can delete outgoing messages - * in channels. - * - If the bot is an administrator of a group, it can delete any message there. - * - If the bot has can_delete_messages permission in a supergroup or a channel, - * it can delete any message there. - * @param[in] core_h The telebot core handler created with #telebot_core_create(). - * @param[in] chat_id Unique identifier for the target chat or username of the - * target message_id Message identifier to be deleted. - * @param[out] response Response data that contains "True" on succes, - * It MUST be freed with #telebot_core_put_response(). - * @return on Success, TELEBOT_ERROR_NONE is returned, otherwise a negative error value. - */ -telebot_error_e telebot_core_delete_message(telebot_core_handler_t *core_h, - long long int chat_id, int message_id, telebot_core_response_t *response); - - -/** - * @brief Release response data obtained with telebot core methods. - * @param[in] response Pointer to response to release. - */ -void telebot_core_put_response(telebot_core_response_t *response); + * - A message can only be deleted if it was sent less than 48 hours ago. + * - Bots can delete outgoing messages in groups and supergroups. + * - Bots granted can_post_messages permissions can delete outgoing messages + * in channels. + * - If the bot is an administrator of a group, it can delete any message there. + * - If the bot has can_delete_messages permission in a supergroup or a channel, + * it can delete any message there. + * @param[in] core_h The telebot core handler created with #telebot_core_create(). + * @param[in] chat_id Unique identifier for the target chat or username of the + * target message_id Message identifier to be deleted. + * @return #telebot_core_response_t response that contains the result (true/false), + * which MUST be released with #telebot_core_put_response(), or null if allocation fails. + * Response code should be checked with #teleobot_core_get_response_code(), + * before getting data with #telebot_core_get_response_data(). + */ +telebot_core_response_t telebot_core_delete_message(telebot_core_handler_t core_h, + long long int chat_id, int message_id); /** * @} // end of APIs diff --git a/include/telebot-parser.h b/include/telebot-parser.h index 52bedcc..a99b891 100644 --- a/include/telebot-parser.h +++ b/include/telebot-parser.h @@ -19,7 +19,7 @@ #ifndef __TELEBOT_PARSER_H__ #define __TELEBOT_PARSER_H__ -struct json_object *telebot_parser_str_to_obj(char *data); +struct json_object *telebot_parser_str_to_obj(const char *data); /** Get update from Json Object */ telebot_error_e telebot_parser_get_updates(struct json_object *obj, telebot_update_t **updates, int *count); diff --git a/include/telebot-private.h b/include/telebot-private.h index b3748e0..c139325 100644 --- a/include/telebot-private.h +++ b/include/telebot-private.h @@ -16,6 +16,8 @@ * limitations under the License. */ +#include + #ifndef __TELEBOT_PRIVATE_H__ #define __TELEBOT_PRIVATE_H__ @@ -93,15 +95,49 @@ #define DBG(x, ...) #endif +#define CHECK_ARG_NULL(PARAM) \ + if (PARAM == NULL) \ + { \ + ERR("Argument '%s' is null)", #PARAM); \ + return telebot_core_get_error_response(TELEBOT_ERROR_INVALID_PARAMETER); \ + } + +#define CHECK_ARG_CONDITION(CONDITION, MESSAGE) \ + if (CONDITION) \ + { \ + ERR(MESSAGE); \ + return telebot_core_get_error_response(TELEBOT_ERROR_INVALID_PARAMETER); \ + } + typedef enum { TELEBOT_MIME_TYPE_DATA = 0, TELEBOT_MIME_TYPE_FILE, TELEBOT_MIME_TYPE_MAX, } telebot_core_mime_e; + typedef struct { telebot_core_mime_e type; const char *name; char data[TELEBOT_BUFFER_PAGE]; } telebot_core_mime_t; + +/** + * @brief This object represents core handler. + */ +struct telebot_core_handler { + char *token; /**< Telegam bot token */ + char *proxy_addr; /**< Proxy address (optional) */ + char *proxy_auth; /**< Proxy authentication (optional) */ +}; + +/** + * @brief This object represents a telegram bot response. + */ +struct telebot_core_response { + telebot_error_e ret; /**< Telegram bot response code */ + size_t size; /**< Telegam bot response size */ + char *data; /**< Telegam bot response object */ +}; + #endif /* __TELEBOT_PRIVATE_H__ */ diff --git a/include/telebot-types.h b/include/telebot-types.h index 5ba16a9..6001d2d 100644 --- a/include/telebot-types.h +++ b/include/telebot-types.h @@ -1293,7 +1293,7 @@ typedef struct telebot_webhook_info { /** * @brief This is opaque object to represent a telebot handler. */ -typedef struct telebot_handler_s *telebot_handler_t; +typedef struct telebot_handler *telebot_handler_t; /** * @} // end of APIs diff --git a/src/telebot-core.c b/src/telebot-core.c index e2b562b..d3f22ea 100644 --- a/src/telebot-core.c +++ b/src/telebot-core.c @@ -16,152 +16,54 @@ * limitations under the License. */ -#include -#include -#include -#include #include #include -#include +#include +#include +#include #include #include +#include +#include -void telebot_core_put_response(telebot_core_response_t *response) +telebot_error_e telebot_core_get_response_code(telebot_core_response_t response) { if (response) - { - TELEBOT_SAFE_FZCNT(response->data, response->size); - } + return response->ret; + else + // response is NULL, probably could not allocate memory + return TELEBOT_ERROR_OUT_OF_MEMORY; } -static size_t write_data_cb(void *contents, size_t size, size_t nmemb, void *userp) +const char *telebot_core_get_response_data(telebot_core_response_t response) { - telebot_core_response_t *resp = (telebot_core_response_t *)userp; - size_t r_size = size * nmemb; - - char *data = (char *)realloc(resp->data, resp->size + r_size + 1); - if (data == NULL) - { - ERR("Failed to allocate memory, size:%u", (unsigned int)r_size); - TELEBOT_SAFE_FZCNT(resp->data, resp->size); - return 0; - } - memcpy((data + resp->size), contents, r_size); - resp->data = data; - resp->size += r_size; - resp->data[resp->size] = 0; - - return r_size; + if (response) + return response->data; + else + return NULL; } -static telebot_error_e telebot_core_curl_perform(telebot_core_handler_t *core_h, - const char *method, telebot_core_mime_t mimes[], size_t size, - telebot_core_response_t *resp) +void telebot_core_put_response(telebot_core_response_t response) { - CURLcode res; - CURL *curl_h = NULL; - curl_mime *mime = NULL; - long resp_code = 0L; - int ret = TELEBOT_ERROR_NONE; - - if (resp == NULL) - { - return TELEBOT_ERROR_INVALID_PARAMETER; - } - resp->data = (char *)malloc(1); - resp->size = 0; - - curl_h = curl_easy_init(); - if (curl_h == NULL) - { - ERR("Failed to init curl"); - ret = TELEBOT_ERROR_OUT_OF_MEMORY; - goto finish; - } - - char URL[TELEBOT_URL_SIZE]; - snprintf(URL, TELEBOT_URL_SIZE, "%s/bot%s/%s", TELEBOT_API_URL, core_h->token, method); - curl_easy_setopt(curl_h, CURLOPT_URL, URL); - curl_easy_setopt(curl_h, CURLOPT_WRITEFUNCTION, write_data_cb); - curl_easy_setopt(curl_h, CURLOPT_WRITEDATA, resp); - - if (core_h->proxy_addr != NULL) - { - curl_easy_setopt(curl_h, CURLOPT_PROXY, core_h->proxy_addr); - if (core_h->proxy_auth != NULL) - { - curl_easy_setopt(curl_h, CURLOPT_PROXYAUTH, CURLAUTH_ANY); - curl_easy_setopt(curl_h, CURLOPT_PROXYUSERPWD, core_h->proxy_auth); - } - } - - if (size > 0) - { - mime = curl_mime_init(curl_h); - if (mime == NULL) - { - ERR("Failed to create mime"); - ret = TELEBOT_ERROR_OUT_OF_MEMORY; - goto finish; - } - for (int index = 0; index < size; index++) - { - curl_mimepart *part = curl_mime_addpart(mime); - if (part == NULL) - { - ERR("Failed to create mime part"); - ret = TELEBOT_ERROR_OUT_OF_MEMORY; - goto finish; - } - curl_mime_name(part, mimes[index].name); - if (mimes[index].type == TELEBOT_MIME_TYPE_FILE) - curl_mime_filedata(part, mimes[index].data); - else - curl_mime_data(part, mimes[index].data, CURL_ZERO_TERMINATED); - } - - curl_easy_setopt(curl_h, CURLOPT_MIMEPOST, mime); - } - - res = curl_easy_perform(curl_h); - if (res != CURLE_OK) - { - ERR("Failed to curl_easy_perform\nError: %s (%d)", curl_easy_strerror(res), res); - ret = TELEBOT_ERROR_OPERATION_FAILED; - goto finish; - } - - curl_easy_getinfo(curl_h, CURLINFO_RESPONSE_CODE, &resp_code); - if (resp_code != 200L) + if (response) { - ERR("Wrong HTTP response received, response: %ld", resp_code); - ret = TELEBOT_ERROR_OPERATION_FAILED; - goto finish; + TELEBOT_SAFE_FZCNT(response->data, response->size); + TELEBOT_SAFE_FREE(response); } - - DBG("Response: %s", resp->data); - -finish: - if (ret != TELEBOT_ERROR_NONE) - TELEBOT_SAFE_FZCNT(resp->data, resp->size); - if (mime) - curl_mime_free(mime); - if (curl_h) - curl_easy_cleanup(curl_h); - - return ret; } -telebot_error_e telebot_core_create(telebot_core_handler_t **core_h, const char *token) +telebot_error_e +telebot_core_create(telebot_core_handler_t *core_h, const char *token) { - if ((token == NULL) || (core_h == NULL)) + if ((core_h == NULL) || (token == NULL)) { - ERR("Token(0x%p) or core_h(0x%p) is NULL", token, core_h); + ERR("Either pointer for core handler (%p) or token (%p) is null", core_h, token); return TELEBOT_ERROR_INVALID_PARAMETER; } + *core_h = NULL; - telebot_core_handler_t *_core_h = malloc(sizeof(telebot_core_handler_t)); + telebot_core_handler_t _core_h = malloc(sizeof(telebot_core_handler_t)); if (_core_h == NULL) { ERR("Failed to allocate memory"); @@ -184,13 +86,14 @@ telebot_error_e telebot_core_create(telebot_core_handler_t **core_h, const char return TELEBOT_ERROR_NONE; } -telebot_error_e telebot_core_destroy(telebot_core_handler_t **core_h) +telebot_error_e +telebot_core_destroy(telebot_core_handler_t *core_h) { curl_global_cleanup(); if ((core_h == NULL) || (*core_h == NULL)) { - ERR("Handler is NULL"); + ERR("Core handler is null"); return TELEBOT_ERROR_INVALID_PARAMETER; } @@ -216,8 +119,8 @@ telebot_error_e telebot_core_destroy(telebot_core_handler_t **core_h) return TELEBOT_ERROR_NONE; } -telebot_error_e telebot_core_set_proxy(telebot_core_handler_t *core_h, - const char *addr, const char *auth) +telebot_error_e +telebot_core_set_proxy(telebot_core_handler_t core_h, const char *addr, const char *auth) { if ((addr == NULL) || (core_h == NULL)) { @@ -245,7 +148,8 @@ telebot_error_e telebot_core_set_proxy(telebot_core_handler_t *core_h, return TELEBOT_ERROR_NONE; } -telebot_error_e telebot_core_get_proxy(telebot_core_handler_t *core_h, char **addr) +telebot_error_e +telebot_core_get_proxy(telebot_core_handler_t core_h, char **addr) { if ((addr == NULL) || (core_h == NULL)) { @@ -260,16 +164,143 @@ telebot_error_e telebot_core_get_proxy(telebot_core_handler_t *core_h, char **ad return TELEBOT_ERROR_NONE; } -telebot_error_e telebot_core_get_updates(telebot_core_handler_t *core_h, - int offset, int limit, int timeout, const char *allowed_updates, - telebot_core_response_t *response) +static size_t write_data_cb(void *contents, size_t size, size_t nmemb, void *userp) { - if ((core_h == NULL) || (core_h->token == NULL)) + telebot_core_response_t resp = (telebot_core_response_t)userp; + size_t r_size = size * nmemb; + + char *data = (char *)realloc(resp->data, resp->size + r_size + 1); + if (data == NULL) { - ERR("Handler or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; + ERR("Failed to allocate memory, size:%u", (unsigned int)r_size); + TELEBOT_SAFE_FZCNT(resp->data, resp->size); + return 0; } + memcpy((data + resp->size), contents, r_size); + resp->data = data; + resp->size += r_size; + resp->data[resp->size] = 0; + return r_size; +} + +static telebot_core_response_t +telebot_core_curl_perform(telebot_core_handler_t core_h, const char *method, telebot_core_mime_t mimes[], size_t size) +{ + CURLcode res; + CURL *curl_h = NULL; + curl_mime *mime = NULL; + long resp_code = 0L; + + telebot_core_response_t resp = calloc(1, sizeof(struct telebot_core_response)); + if (resp == NULL) + { + ERR("Failed to allocate memory for response"); + return NULL; + } + + if (core_h == NULL) + { + ERR("Core handler is NULL"); + resp->ret = TELEBOT_ERROR_INVALID_PARAMETER; + return resp; + } + + if (core_h->token == NULL) + { + ERR("Token is NULL, this should not happen"); + resp->ret = TELEBOT_ERROR_OPERATION_FAILED; + return resp; + } + + resp->data = (char *)malloc(1); + resp->size = 0; + resp->ret = TELEBOT_ERROR_NONE; + + curl_h = curl_easy_init(); + if (curl_h == NULL) + { + ERR("Failed to init curl"); + resp->ret = TELEBOT_ERROR_OUT_OF_MEMORY; + goto finish; + } + + char URL[TELEBOT_URL_SIZE]; + snprintf(URL, TELEBOT_URL_SIZE, "%s/bot%s/%s", TELEBOT_API_URL, core_h->token, method); + curl_easy_setopt(curl_h, CURLOPT_URL, URL); + curl_easy_setopt(curl_h, CURLOPT_WRITEFUNCTION, write_data_cb); + curl_easy_setopt(curl_h, CURLOPT_WRITEDATA, resp); + + if (core_h->proxy_addr != NULL) + { + curl_easy_setopt(curl_h, CURLOPT_PROXY, core_h->proxy_addr); + if (core_h->proxy_auth != NULL) + { + curl_easy_setopt(curl_h, CURLOPT_PROXYAUTH, CURLAUTH_ANY); + curl_easy_setopt(curl_h, CURLOPT_PROXYUSERPWD, core_h->proxy_auth); + } + } + + if (size > 0) + { + mime = curl_mime_init(curl_h); + if (mime == NULL) + { + ERR("Failed to create mime"); + resp->ret = TELEBOT_ERROR_OUT_OF_MEMORY; + goto finish; + } + for (int index = 0; index < size; index++) + { + curl_mimepart *part = curl_mime_addpart(mime); + if (part == NULL) + { + ERR("Failed to create mime part"); + resp->ret = TELEBOT_ERROR_OUT_OF_MEMORY; + goto finish; + } + curl_mime_name(part, mimes[index].name); + if (mimes[index].type == TELEBOT_MIME_TYPE_FILE) + curl_mime_filedata(part, mimes[index].data); + else + curl_mime_data(part, mimes[index].data, CURL_ZERO_TERMINATED); + } + + curl_easy_setopt(curl_h, CURLOPT_MIMEPOST, mime); + } + + res = curl_easy_perform(curl_h); + if (res != CURLE_OK) + { + ERR("Failed to curl_easy_perform\nError: %s (%d)", curl_easy_strerror(res), res); + resp->ret = TELEBOT_ERROR_OPERATION_FAILED; + goto finish; + } + + curl_easy_getinfo(curl_h, CURLINFO_RESPONSE_CODE, &resp_code); + if (resp_code != 200L) + { + ERR("Wrong HTTP response received, response: %ld", resp_code); + resp->ret = TELEBOT_ERROR_OPERATION_FAILED; + goto finish; + } + + DBG("Response: %s", resp->data); + +finish: + if (resp->ret != TELEBOT_ERROR_NONE) + TELEBOT_SAFE_FZCNT(resp->data, resp->size); + if (mime) + curl_mime_free(mime); + if (curl_h) + curl_easy_cleanup(curl_h); + + return resp; +} + +telebot_core_response_t +telebot_core_get_updates(telebot_core_handler_t core_h, int offset, int limit, int timeout, const char *allowed_updates) +{ if (limit > TELEBOT_UPDATE_COUNT_MAX_LIMIT) limit = TELEBOT_UPDATE_COUNT_MAX_LIMIT; @@ -298,18 +329,23 @@ telebot_error_e telebot_core_get_updates(telebot_core_handler_t *core_h, ++index; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_UPDATES, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_UPDATES, mimes, index); } -telebot_error_e telebot_core_set_webhook(telebot_core_handler_t *core_h, const char *url, - const char *certificate, int max_connections, const char *allowed_updates, - telebot_core_response_t *response) +static telebot_core_response_t telebot_core_get_error_response(telebot_error_e ret) { - if ((core_h == NULL) || (core_h->token == NULL) || (url == NULL)) - { - ERR("Handler, token, or url is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + telebot_core_response_t resp = calloc(1, sizeof(struct telebot_core_response)); + if (resp) + resp->ret = ret; + + return resp; +} + +telebot_core_response_t +telebot_core_set_webhook(telebot_core_handler_t core_h, const char *url, const char *certificate, int max_connections, + const char *allowed_updates) +{ + CHECK_ARG_NULL(url); int index = 0; telebot_core_mime_t mimes[4]; // number of arguments @@ -338,55 +374,33 @@ telebot_error_e telebot_core_set_webhook(telebot_core_handler_t *core_h, const c ++index; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_WEBHOOK, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_WEBHOOK, mimes, index); } -telebot_error_e telebot_core_delete_webhook(telebot_core_handler_t *core_h, - telebot_core_response_t *response) +telebot_core_response_t +telebot_core_delete_webhook(telebot_core_handler_t core_h) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_DELETE_WEBHOOK, NULL, 0, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_DELETE_WEBHOOK, NULL, 0); } -telebot_error_e telebot_core_get_webhook_info(telebot_core_handler_t *core_h, - telebot_core_response_t *response) +telebot_core_response_t +telebot_core_get_webhook_info(telebot_core_handler_t core_h) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_WEBHOOK_INFO, NULL, 0, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_WEBHOOK_INFO, NULL, 0); } -telebot_error_e telebot_core_get_me(telebot_core_handler_t *core_h, - telebot_core_response_t *response) +telebot_core_response_t +telebot_core_get_me(telebot_core_handler_t core_h) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_ME, NULL, 0, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_ME, NULL, 0); } -telebot_error_e telebot_core_send_message(telebot_core_handler_t *core_h, - long long int chat_id, const char *text, const char *parse_mode, - bool disable_web_page_preview, bool disable_notification, int reply_to_message_id, - const char *reply_markup, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_send_message(telebot_core_handler_t core_h, long long int chat_id, const char *text, const char *parse_mode, + bool disable_web_page_preview, bool disable_notification, int reply_to_message_id, + const char *reply_markup) { - if ((core_h == NULL) || (core_h->token == NULL) || (text == NULL)) - { - ERR("Handler, token or text is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_NULL(text); int index = 0; telebot_core_mime_t mimes[7]; // number of arguments @@ -434,23 +448,17 @@ telebot_error_e telebot_core_send_message(telebot_core_handler_t *core_h, ++index; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_MESSAGE, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_MESSAGE, mimes, index); } -telebot_error_e telebot_core_forward_message(telebot_core_handler_t *core_h, - long long int chat_id, long long int from_chat_id, bool disable_notification, - int message_id, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_forward_message(telebot_core_handler_t core_h, long long int chat_id, long long int from_chat_id, + bool disable_notification, int message_id) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler or token is NULL."); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - if (message_id <= 0) { ERR("Valid message_id is required."); - return TELEBOT_ERROR_INVALID_PARAMETER; + return telebot_core_get_error_response(TELEBOT_ERROR_INVALID_PARAMETER); } int index = 0; @@ -475,19 +483,15 @@ telebot_error_e telebot_core_forward_message(telebot_core_handler_t *core_h, snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", message_id); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_FORWARD_MESSAGE, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_FORWARD_MESSAGE, mimes, index); } -telebot_error_e telebot_core_send_photo(telebot_core_handler_t *core_h, - long long int chat_id, const char *photo, bool is_file, const char *caption, - const char *parse_mode, bool disable_notification, int reply_to_message_id, - const char *reply_markup, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_send_photo(telebot_core_handler_t core_h, long long int chat_id, const char *photo, bool is_file, + const char *caption, const char *parse_mode, bool disable_notification, + int reply_to_message_id, const char *reply_markup) { - if ((core_h == NULL) || (core_h->token == NULL) || (photo == NULL)) - { - ERR("Handler, token or photo is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_NULL(photo); int index = 0; telebot_core_mime_t mimes[7]; // number of arguments @@ -537,20 +541,16 @@ telebot_error_e telebot_core_send_photo(telebot_core_handler_t *core_h, ++index; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_PHOTO, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_PHOTO, mimes, index); } -telebot_error_e telebot_core_send_audio(telebot_core_handler_t *core_h, - long long int chat_id, const char *audio, bool is_file, const char *caption, - const char *parse_mode, int duration, const char *performer, const char *title, - const char *thumb, bool disable_notification, int reply_to_message_id, - const char *reply_markup, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_send_audio(telebot_core_handler_t core_h, long long int chat_id, const char *audio, bool is_file, + const char *caption, const char *parse_mode, int duration, const char *performer, + const char *title, const char *thumb, bool disable_notification, int reply_to_message_id, + const char *reply_markup) { - if ((core_h == NULL) || (core_h->token == NULL) || (audio == NULL)) - { - ERR("Handler, token or audio is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_NULL(audio); int index = 0; telebot_core_mime_t mimes[11]; // number of arguments @@ -632,21 +632,15 @@ telebot_error_e telebot_core_send_audio(telebot_core_handler_t *core_h, ++index; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_AUDIO, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_AUDIO, mimes, index); } -telebot_error_e telebot_core_send_document(telebot_core_handler_t *core_h, - long long int chat_id, const char *document, bool is_file, const char *thumb, - const char *caption, const char *parse_mode, bool disable_notification, - int reply_to_message_id, const char *reply_markup, - telebot_core_response_t *response) - +telebot_core_response_t +telebot_core_send_document(telebot_core_handler_t core_h, long long int chat_id, const char *document, bool is_file, + const char *thumb, const char *caption, const char *parse_mode, bool disable_notification, + int reply_to_message_id, const char *reply_markup) { - if ((core_h == NULL) || (core_h->token == NULL) || (document == NULL)) - { - ERR("Handler, token or document is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_NULL(document); int index = 0; telebot_core_mime_t mimes[8]; // number of arguments @@ -704,21 +698,16 @@ telebot_error_e telebot_core_send_document(telebot_core_handler_t *core_h, ++index; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_DOCUMENT, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_DOCUMENT, mimes, index); } -telebot_error_e telebot_core_send_video(telebot_core_handler_t *core_h, - long long int chat_id, const char *video, bool is_file, int duration, - int width, int height, const char *thumb, const char *caption, - const char *parse_mode, bool supports_streaming, bool disable_notification, - int reply_to_message_id, const char *reply_markup, - telebot_core_response_t *response) +telebot_core_response_t +telebot_core_send_video(telebot_core_handler_t core_h, long long int chat_id, const char *video, bool is_file, + int duration, int width, int height, const char *thumb, const char *caption, + const char *parse_mode, bool supports_streaming, bool disable_notification, + int reply_to_message_id, const char *reply_markup) { - if ((core_h == NULL) || (core_h->token == NULL) || (video == NULL)) - { - ERR("Handler, token or document is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_NULL(video); int index = 0; telebot_core_mime_t mimes[12]; // number of arguments @@ -805,20 +794,16 @@ telebot_error_e telebot_core_send_video(telebot_core_handler_t *core_h, ++index; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_VIDEO, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_VIDEO, mimes, index); } -telebot_error_e telebot_core_send_animation(telebot_core_handler_t *core_h, - long long int chat_id, const char *animation, bool is_file, int duration, - int width, int height, const char *thumb, const char *caption, - const char *parse_mode, bool disable_notification, int reply_to_message_id, - const char *reply_markup, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_send_animation(telebot_core_handler_t core_h, long long int chat_id, const char *animation, bool is_file, + int duration, int width, int height, const char *thumb, const char *caption, + const char *parse_mode, bool disable_notification, int reply_to_message_id, + const char *reply_markup) { - if ((core_h == NULL) || (core_h->token == NULL) || (animation == NULL)) - { - ERR("Handler, token or document is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_NULL(animation); int index = 0; telebot_core_mime_t mimes[11]; // number of arguments @@ -900,20 +885,15 @@ telebot_error_e telebot_core_send_animation(telebot_core_handler_t *core_h, ++index; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_ANIMATION, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_ANIMATION, mimes, index); } -telebot_error_e telebot_core_send_voice(telebot_core_handler_t *core_h, - long long int chat_id, const char *voice, bool is_file, const char *caption, - const char *parse_mode, int duration, bool disable_notification, - int reply_to_message_id, const char *reply_markup, - telebot_core_response_t *response) +telebot_core_response_t +telebot_core_send_voice(telebot_core_handler_t core_h, long long int chat_id, const char *voice, bool is_file, + const char *caption, const char *parse_mode, int duration, bool disable_notification, + int reply_to_message_id, const char *reply_markup) { - if ((core_h == NULL) || (core_h->token == NULL) || (voice == NULL)) - { - ERR("Handler, token or voice is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_NULL(voice); int index = 0; telebot_core_mime_t mimes[8]; // number of arguments @@ -971,19 +951,15 @@ telebot_error_e telebot_core_send_voice(telebot_core_handler_t *core_h, ++index; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_VOICE, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_VOICE, mimes, index); } -telebot_error_e telebot_core_send_video_note(telebot_core_handler_t *core_h, - long long int chat_id, char *video_note, bool is_file, int duration, int length, - const char *thumb, bool disable_notification, int reply_to_message_id, - const char *reply_markup, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_send_video_note(telebot_core_handler_t core_h, long long int chat_id, char *video_note, bool is_file, + int duration, int length, const char *thumb, bool disable_notification, + int reply_to_message_id, const char *reply_markup) { - if ((core_h == NULL) || (core_h->token == NULL) || (video_note == NULL)) - { - ERR("Handler, token or video_note is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_NULL(video_note); int index = 0; telebot_core_mime_t mimes[8]; // number of arguments @@ -1042,20 +1018,14 @@ telebot_error_e telebot_core_send_video_note(telebot_core_handler_t *core_h, ++index; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_VIDEO_NOTE, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_VIDEO_NOTE, mimes, index); } -telebot_error_e telebot_core_send_location(telebot_core_handler_t *core_h, - long long int chat_id, float latitude, float longitude, int live_period, - bool disable_notification, int reply_to_message_id, const char *reply_markup, - telebot_core_response_t *response) +telebot_core_response_t +telebot_core_send_location(telebot_core_handler_t core_h, long long int chat_id, float latitude, float longitude, + int live_period, bool disable_notification, int reply_to_message_id, + const char *reply_markup) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - int index = 0; telebot_core_mime_t mimes[7]; // number of arguments mimes[index].name = "chat_id"; @@ -1102,20 +1072,14 @@ telebot_error_e telebot_core_send_location(telebot_core_handler_t *core_h, ++index; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_LOCATION, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_LOCATION, mimes, index); } -telebot_error_e telebot_core_edit_message_live_location(telebot_core_handler_t *core_h, - long long int chat_id, int message_id, const char *inline_message_id, - float latitude, float longitude, const char *reply_markup, - telebot_core_response_t *response) +telebot_core_response_t +telebot_core_edit_message_live_location(telebot_core_handler_t core_h, long long int chat_id, int message_id, + const char *inline_message_id, float latitude, float longitude, + const char *reply_markup) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - int index = 0; telebot_core_mime_t mimes[6]; // number of arguments mimes[index].name = "chat_id"; @@ -1157,19 +1121,13 @@ telebot_error_e telebot_core_edit_message_live_location(telebot_core_handler_t * ++index; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_EDIT_MESSAGE_LIVE_LOCATION, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_EDIT_MESSAGE_LIVE_LOCATION, mimes, index); } -telebot_error_e telebot_core_stop_message_live_location(telebot_core_handler_t *core_h, - long long int chat_id, int message_id, char *inline_message_id, - const char *reply_markup, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_stop_message_live_location(telebot_core_handler_t core_h, long long int chat_id, int message_id, + char *inline_message_id, const char *reply_markup) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - int index = 0; telebot_core_mime_t mimes[4]; // number of arguments mimes[index].name = "chat_id"; @@ -1201,21 +1159,16 @@ telebot_error_e telebot_core_stop_message_live_location(telebot_core_handler_t * ++index; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_STOP_MESSAGE_LIVE_LOCATION, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_STOP_MESSAGE_LIVE_LOCATION, mimes, index); } -telebot_error_e telebot_core_send_venue(telebot_core_handler_t *core_h, - long long int chat_id, float latitude, float longitude, const char *title, - const char *address, const char *foursquare_id, const char *foursquare_type, - bool disable_notification, int reply_to_message_id, const char *reply_markup, - telebot_core_response_t *response) +telebot_core_response_t +telebot_core_send_venue(telebot_core_handler_t core_h, long long int chat_id, float latitude, float longitude, + const char *title, const char *address, const char *foursquare_id, const char *foursquare_type, + bool disable_notification, int reply_to_message_id, const char *reply_markup) { - if ((core_h == NULL) || (core_h->token == NULL) || (title == NULL) || - (address == NULL)) - { - ERR("Handler, token, title, or address is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_NULL(title); + CHECK_ARG_NULL(address); int index = 0; telebot_core_mime_t mimes[10]; // number of arguments @@ -1281,20 +1234,16 @@ telebot_error_e telebot_core_send_venue(telebot_core_handler_t *core_h, ++index; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_VENUE, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_VENUE, mimes, index); } -telebot_error_e telebot_core_send_contact(telebot_core_handler_t *core_h, - long long int chat_id, const char *phone_number, const char *first_name, - const char *last_name, const char *vcard, bool disable_notification, - int reply_to_message_id, const char *reply_markup, - telebot_core_response_t *response) +telebot_core_response_t +telebot_core_send_contact(telebot_core_handler_t core_h, long long int chat_id, const char *phone_number, + const char *first_name, const char *last_name, const char *vcard, bool disable_notification, + int reply_to_message_id, const char *reply_markup) { - if ((core_h == NULL) || (core_h->token == NULL) || (phone_number == NULL) || (first_name == NULL)) - { - ERR("Handler, token, phone_number, or first_name is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_NULL(phone_number); + CHECK_ARG_NULL(first_name); int index = 0; telebot_core_mime_t mimes[8]; // number of arguments @@ -1350,21 +1299,16 @@ telebot_error_e telebot_core_send_contact(telebot_core_handler_t *core_h, ++index; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_CONTACT, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_CONTACT, mimes, index); } -telebot_error_e telebot_core_send_poll(telebot_core_handler_t *core_h, - long long int chat_id, const char *question, const char *options, - bool is_anonymous, const char *type, bool allows_multiple_answers, - int correct_option_id, bool is_closed, bool disable_notification, - int reply_to_message_id, const char *reply_markup, - telebot_core_response_t *response) +telebot_core_response_t +telebot_core_send_poll(telebot_core_handler_t core_h, long long int chat_id, const char *question, const char *options, + bool is_anonymous, const char *type, bool allows_multiple_answers, int correct_option_id, + bool is_closed, bool disable_notification, int reply_to_message_id, const char *reply_markup) { - if ((core_h == NULL) || (core_h->token == NULL) || (question == NULL) || (options == NULL)) - { - ERR("Handler, token, phone_number, or first_name is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_NULL(question); + CHECK_ARG_NULL(options); int index = 0; telebot_core_mime_t mimes[11]; // number of arguments @@ -1435,19 +1379,13 @@ telebot_error_e telebot_core_send_poll(telebot_core_handler_t *core_h, ++index; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_POLL, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_POLL, mimes, index); } -telebot_error_e telebot_core_send_dice(telebot_core_handler_t *core_h, - long long int chat_id, bool disable_notification, int reply_to_message_id, - const char *reply_markup, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_send_dice(telebot_core_handler_t core_h, long long int chat_id, bool disable_notification, + int reply_to_message_id, const char *reply_markup) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler, token, phone_number, or first_name is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - int index = 0; telebot_core_mime_t mimes[4]; // number of arguments mimes[index].name = "chat_id"; @@ -1476,17 +1414,13 @@ telebot_error_e telebot_core_send_dice(telebot_core_handler_t *core_h, ++index; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_DICE, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_DICE, mimes, index); } -telebot_error_e telebot_core_send_chat_action(telebot_core_handler_t *core_h, - long long int chat_id, const char *action, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_send_chat_action(telebot_core_handler_t core_h, long long int chat_id, const char *action) { - if ((core_h == NULL) || (core_h->token == NULL) || (action == NULL)) - { - ERR("Handler, token or action is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_NULL(action); int index = 0; telebot_core_mime_t mimes[2]; // number of arguments @@ -1500,23 +1434,13 @@ telebot_error_e telebot_core_send_chat_action(telebot_core_handler_t *core_h, snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", action); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_CHAT_ACTION, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_CHAT_ACTION, mimes, index); } -telebot_error_e telebot_core_get_user_profile_photos(telebot_core_handler_t *core_h, - int user_id, int offset, int limit, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_get_user_profile_photos(telebot_core_handler_t core_h, int user_id, int offset, int limit) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - - if (user_id <= 0) - { - ERR("Invalid value of user_id"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_CONDITION((user_id <= 0), "Invalid value of user_id"); int index = 0; telebot_core_mime_t mimes[3]; // number of arguments @@ -1535,17 +1459,13 @@ telebot_error_e telebot_core_get_user_profile_photos(telebot_core_handler_t *cor snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", limit); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_USER_PHOTOS, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_USER_PHOTOS, mimes, index); } -telebot_error_e telebot_core_get_file(telebot_core_handler_t *core_h, const char *file_id, - telebot_core_response_t *response) +telebot_core_response_t +telebot_core_get_file(telebot_core_handler_t core_h, const char *file_id) { - if ((core_h == NULL) || (core_h->token == NULL) || (file_id == NULL)) - { - ERR("Handler, token or file_id is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_NULL(file_id); int index = 0; telebot_core_mime_t mimes[1]; // number of arguments @@ -1554,32 +1474,31 @@ telebot_error_e telebot_core_get_file(telebot_core_handler_t *core_h, const char snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", file_id); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_FILE, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_FILE, mimes, index); } -static size_t write_file_cb(void *contents, size_t size, size_t nmemb, - void *userp) +static size_t +write_file_cb(void *contents, size_t size, size_t nmemb, void *userp) { size_t written = fwrite(contents, size, nmemb, (FILE *)userp); return written; } -telebot_error_e telebot_core_download_file(telebot_core_handler_t *core_h, - const char *file_path, const char *out_file) +telebot_error_e +telebot_core_download_file(telebot_core_handler_t core_h, const char *file_path, const char *out_file) { int ret = TELEBOT_ERROR_NONE; - if ((core_h == NULL) || (core_h->token == NULL) || (file_path == NULL) || - (out_file == NULL)) + if ((core_h == NULL) || (core_h->token == NULL) || (file_path == NULL) || (out_file == NULL)) return TELEBOT_ERROR_INVALID_PARAMETER; - CURL *curl_h = NULL; + CURL *curl_h = NULL; CURLcode res; long resp_code = 0L; FILE *fp = fopen(out_file, "w"); if (fp == NULL) { - ret = TELEBOT_ERROR_INVALID_PARAMETER; + ret = TELEBOT_ERROR_OPERATION_FAILED; goto finish; } @@ -1626,20 +1545,10 @@ telebot_error_e telebot_core_download_file(telebot_core_handler_t *core_h, return ret; } -telebot_error_e telebot_core_kick_chat_member(telebot_core_handler_t *core_h, - long long int chat_id, int user_id, long until_date, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_kick_chat_member(telebot_core_handler_t core_h, long long int chat_id, int user_id, long until_date) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler, or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - - if (user_id <= 0) - { - ERR("Valid user_id is required"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_CONDITION((user_id <= 0), "Valid user_id is required"); int index = 0; telebot_core_mime_t mimes[3]; // number of arguments @@ -1661,23 +1570,13 @@ telebot_error_e telebot_core_kick_chat_member(telebot_core_handler_t *core_h, ++index; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_KICK_CHAT_MEMBER, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_KICK_CHAT_MEMBER, mimes, index); } -telebot_error_e telebot_core_unban_chat_member(telebot_core_handler_t *core_h, - long long int chat_id, int user_id, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_unban_chat_member(telebot_core_handler_t core_h, long long int chat_id, int user_id) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler, or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - - if (user_id <= 0) - { - ERR("Valid user_id is required"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_CONDITION((user_id <= 0), "Valid user_id is required"); int index = 0; telebot_core_mime_t mimes[2]; // number of arguments @@ -1691,26 +1590,16 @@ telebot_error_e telebot_core_unban_chat_member(telebot_core_handler_t *core_h, snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", user_id); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_UNBAN_CHAT_MEMBER, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_UNBAN_CHAT_MEMBER, mimes, index); } -telebot_error_e telebot_core_restrict_chat_member(telebot_core_handler_t *core_h, - long long int chat_id, int user_id, long until_date, bool can_send_messages, - bool can_send_media_messages, bool can_send_polls, bool can_send_other_messages, - bool can_add_web_page_previews, bool can_change_info, bool can_invite_users, - bool can_pin_messages, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_restrict_chat_member(telebot_core_handler_t core_h, long long int chat_id, int user_id, long until_date, + bool can_send_messages, bool can_send_media_messages, bool can_send_polls, + bool can_send_other_messages, bool can_add_web_page_previews, bool can_change_info, + bool can_invite_users, bool can_pin_messages) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler, or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - - if (user_id <= 0) - { - ERR("Valid user_id is required"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_CONDITION((user_id <= 0), "Valid user_id is required"); int index = 0; telebot_core_mime_t mimes[11]; // number of arguments @@ -1772,26 +1661,16 @@ telebot_error_e telebot_core_restrict_chat_member(telebot_core_handler_t *core_h snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_invite_users ? "true" : "false")); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_RESTRICT_CHAT_MEMBER, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_RESTRICT_CHAT_MEMBER, mimes, index); } -telebot_error_e telebot_core_promote_chat_member(telebot_core_handler_t *core_h, - long long int chat_id, int user_id, bool can_change_info, bool can_post_messages, - bool can_edit_messages, bool can_delete_messages, bool can_invite_users, - bool can_restrict_members, bool can_pin_messages, bool can_promote_members, - telebot_core_response_t *response) +telebot_core_response_t +telebot_core_promote_chat_member(telebot_core_handler_t core_h, long long int chat_id, int user_id, bool can_change_info, + bool can_post_messages, bool can_edit_messages, bool can_delete_messages, + bool can_invite_users, bool can_restrict_members, bool can_pin_messages, + bool can_promote_members) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler, or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - - if (user_id <= 0) - { - ERR("Valid user_id is required"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_CONDITION((user_id <= 0), "Valid user_id is required"); int index = 0; telebot_core_mime_t mimes[10]; // number of arguments @@ -1845,24 +1724,14 @@ telebot_error_e telebot_core_promote_chat_member(telebot_core_handler_t *core_h, snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_promote_members ? "true" : "false")); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_PROMOTE_CHAT_MEMBER, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_PROMOTE_CHAT_MEMBER, mimes, index); } -telebot_error_e telebot_core_set_chat_admin_custom_title(telebot_core_handler_t *core_h, - long long int chat_id, int user_id, const char *custom_title, - telebot_core_response_t *response) +telebot_core_response_t +telebot_core_set_chat_admin_custom_title(telebot_core_handler_t core_h, long long int chat_id, int user_id, + const char *custom_title) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler, or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - - if (user_id <= 0) - { - ERR("Valid user_id is required"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_CONDITION((user_id <= 0), "Valid user_id is required"); int index = 0; telebot_core_mime_t mimes[3]; // number of arguments @@ -1881,21 +1750,15 @@ telebot_error_e telebot_core_set_chat_admin_custom_title(telebot_core_handler_t snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", custom_title); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_ADMIN_TITLE, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_ADMIN_TITLE, mimes, index); } -telebot_error_e telebot_core_set_chat_permissions(telebot_core_handler_t *core_h, - long long int chat_id, bool can_send_messages, bool can_send_media_messages, - bool can_send_polls, bool can_send_other_messages, bool can_add_web_page_previews, - bool can_change_info, bool can_invite_users, bool can_pin_messages, - telebot_core_response_t *response) +telebot_core_response_t +telebot_core_set_chat_permissions(telebot_core_handler_t core_h, long long int chat_id, bool can_send_messages, + bool can_send_media_messages, bool can_send_polls, bool can_send_other_messages, + bool can_add_web_page_previews, bool can_change_info, bool can_invite_users, + bool can_pin_messages) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler, or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - int index = 0; telebot_core_mime_t mimes[9]; // number of arguments mimes[index].name = "chat_id"; @@ -1943,18 +1806,12 @@ telebot_error_e telebot_core_set_chat_permissions(telebot_core_handler_t *core_h snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_invite_users ? "true" : "false")); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_PERMISSIONS, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_PERMISSIONS, mimes, index); } -telebot_error_e telebot_core_export_chat_invite_link(telebot_core_handler_t *core_h, - long long int chat_id, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_export_chat_invite_link(telebot_core_handler_t core_h, long long int chat_id) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler, or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - int index = 0; telebot_core_mime_t mimes[1]; // number of arguments mimes[index].name = "chat_id"; @@ -1962,23 +1819,13 @@ telebot_error_e telebot_core_export_chat_invite_link(telebot_core_handler_t *cor snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_EXPORT_CHAT_INVITE_LINK, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_EXPORT_CHAT_INVITE_LINK, mimes, index); } -telebot_error_e telebot_core_set_chat_photo(telebot_core_handler_t *core_h, - long long int chat_id, const char *photo, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_set_chat_photo(telebot_core_handler_t core_h, long long int chat_id, const char *photo) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler, or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - - if (photo == NULL) - { - ERR("Valid photo is required"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_NULL(photo); int index = 0; telebot_core_mime_t mimes[2]; // number of arguments @@ -1992,17 +1839,13 @@ telebot_error_e telebot_core_set_chat_photo(telebot_core_handler_t *core_h, snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", photo); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_PHOTO, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_PHOTO, mimes, index); } -telebot_error_e telebot_core_delete_chat_photo(telebot_core_handler_t *core_h, - long long int chat_id, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_delete_chat_photo(telebot_core_handler_t core_h, long long int chat_id) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler, or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_CONDITION((chat_id <= 0), "Invalid chat id"); int index = 0; telebot_core_mime_t mimes[1]; // number of arguments @@ -2011,23 +1854,14 @@ telebot_error_e telebot_core_delete_chat_photo(telebot_core_handler_t *core_h, snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_DELETE_CHAT_PHOTO, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_DELETE_CHAT_PHOTO, mimes, index); } -telebot_error_e telebot_core_set_chat_title(telebot_core_handler_t *core_h, - long long int chat_id, const char *title, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_set_chat_title(telebot_core_handler_t core_h, long long int chat_id, const char *title) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler, or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - - if ((title == NULL) || (strlen(title) > 255)) - { - ERR("Valid title is required"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_CONDITION((chat_id <= 0), "Valid chat id is required"); + CHECK_ARG_CONDITION((title == NULL) || (strlen(title) > 255), "Valid title is required"); int index = 0; telebot_core_mime_t mimes[2]; // number of arguments @@ -2041,23 +1875,15 @@ telebot_error_e telebot_core_set_chat_title(telebot_core_handler_t *core_h, snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", title); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_TITLE, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_TITLE, mimes, index); } -telebot_error_e telebot_core_set_chat_description(telebot_core_handler_t *core_h, - long long int chat_id, const char *description, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_set_chat_description(telebot_core_handler_t core_h, long long int chat_id, const char *description) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler, or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - - if ((description == NULL) || (strlen(description) > 255)) - { - ERR("Valid title is required"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_CONDITION((chat_id <= 0), "Valid chat id is required"); + CHECK_ARG_CONDITION((description == NULL) || (strlen(description) > 255), + "Valid description is required"); int index = 0; telebot_core_mime_t mimes[2]; // number of arguments @@ -2071,24 +1897,15 @@ telebot_error_e telebot_core_set_chat_description(telebot_core_handler_t *core_h snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", description); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_DESCRIPTION, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_DESCRIPTION, mimes, index); } -telebot_error_e telebot_core_pin_chat_message(telebot_core_handler_t *core_h, - long long int chat_id, int message_id, bool disable_notification, - telebot_core_response_t *response) +telebot_core_response_t +telebot_core_pin_chat_message(telebot_core_handler_t core_h, long long int chat_id, int message_id, + bool disable_notification) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler, or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - - if (message_id <= 0) - { - ERR("Valid message_id is required"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_CONDITION((chat_id <= 0), "Valid chat id is required"); + CHECK_ARG_CONDITION((message_id <= 0), "Valid message_id is required"); int index = 0; telebot_core_mime_t mimes[3]; // number of arguments @@ -2107,17 +1924,13 @@ telebot_error_e telebot_core_pin_chat_message(telebot_core_handler_t *core_h, snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (disable_notification ? "True" : "False")); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_PIN_CHAT_MESSAGE, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_PIN_CHAT_MESSAGE, mimes, index); } -telebot_error_e telebot_core_unpin_chat_message(telebot_core_handler_t *core_h, - long long int chat_id, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_unpin_chat_message(telebot_core_handler_t core_h, long long int chat_id) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler, or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_CONDITION((chat_id <= 0), "Valid chat id is required"); int index = 0; telebot_core_mime_t mimes[1]; // number of arguments @@ -2126,17 +1939,13 @@ telebot_error_e telebot_core_unpin_chat_message(telebot_core_handler_t *core_h, snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_UNPIN_CHAT_MESSAGE, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_UNPIN_CHAT_MESSAGE, mimes, index); } -telebot_error_e telebot_core_leave_chat(telebot_core_handler_t *core_h, - long long int chat_id, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_leave_chat(telebot_core_handler_t core_h, long long int chat_id) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler, or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_CONDITION((chat_id <= 0), "Valid chat id is required"); int index = 0; telebot_core_mime_t mimes[1]; // number of arguments @@ -2145,17 +1954,13 @@ telebot_error_e telebot_core_leave_chat(telebot_core_handler_t *core_h, snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_LEAVE_CHAT, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_LEAVE_CHAT, mimes, index); } -telebot_error_e telebot_core_get_chat(telebot_core_handler_t *core_h, - long long int chat_id, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_get_chat(telebot_core_handler_t core_h, long long int chat_id) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler, or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_CONDITION((chat_id <= 0), "Valid chat id is required"); int index = 0; telebot_core_mime_t mimes[1]; // number of arguments @@ -2164,17 +1969,13 @@ telebot_error_e telebot_core_get_chat(telebot_core_handler_t *core_h, snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_CHAT, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_CHAT, mimes, index); } -telebot_error_e telebot_core_get_chat_admins(telebot_core_handler_t *core_h, - long long int chat_id, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_get_chat_admins(telebot_core_handler_t core_h, long long int chat_id) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler, or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_CONDITION((chat_id <= 0), "Valid chat id is required"); int index = 0; telebot_core_mime_t mimes[1]; // number of arguments @@ -2183,17 +1984,13 @@ telebot_error_e telebot_core_get_chat_admins(telebot_core_handler_t *core_h, snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_CHAT_ADMINS, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_CHAT_ADMINS, mimes, index); } -telebot_error_e telebot_core_get_chat_members_count(telebot_core_handler_t *core_h, - long long int chat_id, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_get_chat_members_count(telebot_core_handler_t core_h, long long int chat_id) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler, or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_CONDITION((chat_id <= 0), "Valid chat id is required"); int index = 0; telebot_core_mime_t mimes[1]; // number of arguments @@ -2202,23 +1999,14 @@ telebot_error_e telebot_core_get_chat_members_count(telebot_core_handler_t *core snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_CHAT_MEMBERS_COUNT, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_CHAT_MEMBERS_COUNT, mimes, index); } -telebot_error_e telebot_core_get_chat_member(telebot_core_handler_t *core_h, - long long int chat_id, int user_id, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_get_chat_member(telebot_core_handler_t core_h, long long int chat_id, int user_id) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler, or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - - if (user_id <= 0) - { - ERR("Valid user_id is required"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_CONDITION((chat_id <= 0), "Valid chat id is required"); + CHECK_ARG_CONDITION((user_id <= 0), "Valid user_id is required"); int index = 0; telebot_core_mime_t mimes[2]; // number of arguments @@ -2232,24 +2020,14 @@ telebot_error_e telebot_core_get_chat_member(telebot_core_handler_t *core_h, snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", user_id); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_CHAT_MEMBER, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_CHAT_MEMBER, mimes, index); } -telebot_error_e telebot_core_set_chat_sticker_set(telebot_core_handler_t *core_h, - long long int chat_id, const char *sticker_set_name, - telebot_core_response_t *response) +telebot_core_response_t +telebot_core_set_chat_sticker_set(telebot_core_handler_t core_h, long long int chat_id, const char *sticker_set_name) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler, or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - - if (sticker_set_name == NULL) - { - ERR("Valid sticker_set_name is required"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_CONDITION((chat_id <= 0), "Valid chat id is required"); + CHECK_ARG_NULL(sticker_set_name); int index = 0; telebot_core_mime_t mimes[2]; // number of arguments @@ -2263,17 +2041,13 @@ telebot_error_e telebot_core_set_chat_sticker_set(telebot_core_handler_t *core_h snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", sticker_set_name); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_STICKER_SET, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_STICKER_SET, mimes, index); } -telebot_error_e telebot_core_delete_chat_sticker_set(telebot_core_handler_t *core_h, - long long int chat_id, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_delete_chat_sticker_set(telebot_core_handler_t core_h, long long int chat_id) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler, or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_CONDITION((chat_id <= 0), "Valid chat id is required"); int index = 0; telebot_core_mime_t mimes[1]; // number of arguments @@ -2282,19 +2056,14 @@ telebot_error_e telebot_core_delete_chat_sticker_set(telebot_core_handler_t *cor snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_DEL_CHAT_STICKER_SET, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_DEL_CHAT_STICKER_SET, mimes, index); } -telebot_error_e telebot_core_answer_callback_query(telebot_core_handler_t *core_h, - const char *callback_query_id, const char *text, bool show_alert, - const char *url, int cache_time, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_answer_callback_query(telebot_core_handler_t core_h, const char *callback_query_id, const char *text, + bool show_alert, const char *url, int cache_time) { - if ((core_h == NULL) || (core_h->token == NULL) || - (callback_query_id == NULL)) - { - ERR("Handler, token or callback_query_id is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_NULL(callback_query_id); int index = 0; telebot_core_mime_t mimes[5]; // number of arguments @@ -2332,17 +2101,13 @@ telebot_error_e telebot_core_answer_callback_query(telebot_core_handler_t *core_ ++index; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_ANSWER_CALLBACK_QUERY, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_ANSWER_CALLBACK_QUERY, mimes, index); } -telebot_error_e telebot_core_set_my_commands(telebot_core_handler_t *core_h, - const char *commands, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_set_my_commands(telebot_core_handler_t core_h, const char *commands) { - if ((core_h == NULL) || (core_h->token == NULL) || (commands == NULL)) - { - ERR("Handler, token, or commands is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_NULL(commands); int index = 0; telebot_core_mime_t mimes[1]; // number of arguments @@ -2351,36 +2116,24 @@ telebot_error_e telebot_core_set_my_commands(telebot_core_handler_t *core_h, snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", commands); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_MY_COMMANDS, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_MY_COMMANDS, mimes, index); } -telebot_error_e telebot_core_get_my_commands(telebot_core_handler_t *core_h, - telebot_core_response_t *response) +telebot_core_response_t +telebot_core_get_my_commands(telebot_core_handler_t core_h) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler, or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_MY_COMMANDS, NULL, 0, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_MY_COMMANDS, NULL, 0); } -telebot_error_e telebot_core_edit_message_text(telebot_core_handler_t *core_h, - long long int chat_id, int message_id, const char *inline_message_id, - const char *text, const char *parse_mode, bool disable_web_page_preview, - const char *reply_markup, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_edit_message_text(telebot_core_handler_t core_h, long long int chat_id, int message_id, + const char *inline_message_id, const char *text, const char *parse_mode, + bool disable_web_page_preview, const char *reply_markup) { - if ((core_h == NULL) || (core_h->token == NULL) || (text == NULL)) - { - ERR("Handler, token, or text is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - if (((chat_id == 0) || (message_id <= 0)) && (inline_message_id == NULL)) { ERR("Either valid chat_id & message_id or inline_message_id required"); - return TELEBOT_ERROR_INVALID_PARAMETER; + return telebot_core_get_error_response(TELEBOT_ERROR_INVALID_PARAMETER); } int index = 0; @@ -2432,24 +2185,18 @@ telebot_error_e telebot_core_edit_message_text(telebot_core_handler_t *core_h, ++index; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_EDIT_MESSAGE_TEXT, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_EDIT_MESSAGE_TEXT, mimes, index); } -telebot_error_e telebot_core_edit_message_caption(telebot_core_handler_t *core_h, - long long int chat_id, int message_id, const char *inline_message_id, - const char *caption, const char *parse_mode, const char *reply_markup, - telebot_core_response_t *response) +telebot_core_response_t +telebot_core_edit_message_caption(telebot_core_handler_t core_h, long long int chat_id, int message_id, + const char *inline_message_id, const char *caption, const char *parse_mode, + const char *reply_markup) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - if (((chat_id == 0) || (message_id <= 0)) && (inline_message_id == NULL)) { ERR("Either valid chat_id & message_id or inline_message_id required"); - return TELEBOT_ERROR_INVALID_PARAMETER; + return telebot_core_get_error_response(TELEBOT_ERROR_INVALID_PARAMETER); } int index = 0; @@ -2496,23 +2243,17 @@ telebot_error_e telebot_core_edit_message_caption(telebot_core_handler_t *core_h ++index; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_EDIT_MESSAGE_CAPTION, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_EDIT_MESSAGE_CAPTION, mimes, index); } -telebot_error_e telebot_core_edit_message_reply_markup(telebot_core_handler_t *core_h, - long long int chat_id, int message_id, const char *inline_message_id, - const char *reply_markup, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_edit_message_reply_markup(telebot_core_handler_t core_h, long long int chat_id, int message_id, + const char *inline_message_id, const char *reply_markup) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - if (((chat_id == 0) || (message_id <= 0)) && (inline_message_id == NULL)) { ERR("Either valid chat_id & message_id or inline_message_id required"); - return TELEBOT_ERROR_INVALID_PARAMETER; + return telebot_core_get_error_response(TELEBOT_ERROR_INVALID_PARAMETER); } int index = 0; @@ -2546,24 +2287,14 @@ telebot_error_e telebot_core_edit_message_reply_markup(telebot_core_handler_t *c ++index; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_EDIT_MESSAGE_REPLY_MARKUP, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_EDIT_MESSAGE_REPLY_MARKUP, mimes, index); } -telebot_error_e telebot_core_stop_poll(telebot_core_handler_t *core_h, - long long int chat_id, int message_id, const char *reply_markup, - telebot_core_response_t *response) +telebot_core_response_t +telebot_core_stop_poll(telebot_core_handler_t core_h, long long int chat_id, int message_id, const char *reply_markup) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - - if ((chat_id == 0) || (message_id <= 0)) - { - ERR("Valid chat_id and message_id required"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_CONDITION((chat_id <= 0) || (message_id <= 0), + "Valid chat_id and message_id required"); int index = 0; telebot_core_mime_t mimes[3]; // number of arguments @@ -2585,23 +2316,14 @@ telebot_error_e telebot_core_stop_poll(telebot_core_handler_t *core_h, ++index; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_STOP_POLL, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_STOP_POLL, mimes, index); } -telebot_error_e telebot_core_delete_message(telebot_core_handler_t *core_h, - long long int chat_id, int message_id, telebot_core_response_t *response) +telebot_core_response_t +telebot_core_delete_message(telebot_core_handler_t core_h, long long int chat_id, int message_id) { - if ((core_h == NULL) || (core_h->token == NULL)) - { - ERR("Handler or token is NULL"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } - - if ((message_id) <= 0) - { - ERR("message_id is invalid"); - return TELEBOT_ERROR_INVALID_PARAMETER; - } + CHECK_ARG_CONDITION((chat_id <= 0) || (message_id <= 0), + "Valid chat_id and message_id required"); int index = 0; telebot_core_mime_t mimes[2]; // number of arguments @@ -2615,5 +2337,5 @@ telebot_error_e telebot_core_delete_message(telebot_core_handler_t *core_h, snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", message_id); ++index; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_DELETE_MESSAGE, mimes, index, response); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_DELETE_MESSAGE, mimes, index); } diff --git a/src/telebot-parser.c b/src/telebot-parser.c index de951b6..54a6015 100644 --- a/src/telebot-parser.c +++ b/src/telebot-parser.c @@ -22,10 +22,6 @@ #include #include #include -#include -#include -#include -#include #include static const char *telebot_update_type_str[TELEBOT_UPDATE_TYPE_MAX] = { @@ -38,9 +34,12 @@ static const char *telebot_update_type_str[TELEBOT_UPDATE_TYPE_MAX] = { static telebot_error_e telebot_parser_get_photos(struct json_object *obj, telebot_photo_t **photos, int *count); static telebot_error_e telebot_parser_get_users(struct json_object *obj, telebot_user_t **users, int *count); -struct json_object *telebot_parser_str_to_obj(char *data) +struct json_object *telebot_parser_str_to_obj(const char *data) { - return json_tokener_parse(data); + if (data) + return json_tokener_parse(data); + else + return NULL; } telebot_error_e telebot_parser_get_updates(struct json_object *obj, telebot_update_t **updates, int *count) diff --git a/src/telebot.c b/src/telebot.c index 37ede1e..ebded9c 100644 --- a/src/telebot.c +++ b/src/telebot.c @@ -25,24 +25,25 @@ #include #include #include -#include -#include #include +#include #include -typedef struct telebot_handler_s +/** + * @brief This object represents handler. + */ +struct telebot_handler { - telebot_core_handler_t *core_h; - int offset; -} telebot_hdata_t; + telebot_core_handler_t core_h; /**< Core handler */ + int offset; /**< Offset value to get updates */ +}; static const char *telebot_update_type_str[TELEBOT_UPDATE_TYPE_MAX] = { "message", "edited_message", "channel_post", "edited_channel_post", "inline_query", "chonse_inline_result", "callback_query", "shipping_query", "pre_checkout_query", - "poll", "poll_answer" -}; + "poll", "poll_answer"}; static void telebot_put_user(telebot_user_t *user); static void telebot_put_chat_photo(telebot_chat_photo_t *photo); @@ -64,16 +65,16 @@ static void telebot_put_location(telebot_location_t *location); static void telebot_put_venue(telebot_venue_t *venue); static void telebot_put_file(telebot_file_t *file); static void telebot_put_callback_query(telebot_callback_query_t *query); -//static void telebot_put_game(telebot_document_t *game); -//static void telebot_put_invoice(telebot_invoice_t *invoice); -//static void telebot_put_payment(telebot_successful_payment_t *payment); +// static void telebot_put_game(telebot_document_t *game); +// static void telebot_put_invoice(telebot_invoice_t *invoice); +// static void telebot_put_payment(telebot_successful_payment_t *payment); telebot_error_e telebot_create(telebot_handler_t *handle, char *token) { if ((token == NULL) || (handle == NULL)) return TELEBOT_ERROR_INVALID_PARAMETER; - telebot_hdata_t *_handle = (telebot_hdata_t *)malloc(sizeof(telebot_hdata_t)); + telebot_handler_t _handle = calloc(1, sizeof(struct telebot_handler)); if (_handle == NULL) { ERR("Failed to allocate memory"); @@ -95,12 +96,11 @@ telebot_error_e telebot_create(telebot_handler_t *handle, char *token) telebot_error_e telebot_destroy(telebot_handler_t handle) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; - telebot_core_destroy(&(_handle->core_h)); - TELEBOT_SAFE_FREE(_handle); + telebot_core_destroy(&(handle->core_h)); + TELEBOT_SAFE_FREE(handle); return TELEBOT_ERROR_NONE; } @@ -110,11 +110,10 @@ telebot_error_e telebot_set_proxy(telebot_handler_t handle, char *addr, char *au if (addr == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; - telebot_error_e ret = telebot_core_set_proxy(_handle->core_h, addr, auth); + telebot_error_e ret = telebot_core_set_proxy(handle->core_h, addr, auth); if (ret != TELEBOT_ERROR_NONE) return ret; @@ -126,21 +125,21 @@ telebot_error_e telebot_get_proxy(telebot_handler_t handle, char **addr) if (addr == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; - *addr = NULL; - - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; - return telebot_core_get_proxy(_handle->core_h, addr); + return telebot_core_get_proxy(handle->core_h, addr); } -telebot_error_e telebot_get_updates(telebot_handler_t handle, int offset, - int limit, int timeout, telebot_update_type_e allowed_updates[], - int allowed_updates_count, telebot_update_t **updates, int *count) +telebot_error_e telebot_get_updates(telebot_handler_t handle, int offset, int limit, int timeout, + telebot_update_type_e allowed_updates[], int allowed_updates_count, + telebot_update_t **updates, int *count) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + int ret = TELEBOT_ERROR_NONE; + telebot_core_response_t response; + struct json_object *obj = NULL; + + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if ((updates == NULL) || (count == NULL)) @@ -154,7 +153,8 @@ telebot_error_e telebot_get_updates(telebot_handler_t handle, int offset, if (allowed_updates_count > 0) { array = json_object_new_array(); - for (int i = 0; i < allowed_updates_count; i++) { + for (int i = 0; i < allowed_updates_count; i++) + { const char *item = telebot_update_type_str[allowed_updates[i]]; json_object_array_add(array, json_object_new_string(item)); } @@ -162,22 +162,23 @@ telebot_error_e telebot_get_updates(telebot_handler_t handle, int offset, DBG("Allowed updates: %s", str_allowed_updates); } - int _offset = offset != 0 ? offset : _handle->offset; + int _offset = offset != 0 ? offset : handle->offset; int _timeout = timeout > 0 ? timeout : 0; int _limit = TELEBOT_UPDATE_COUNT_MAX_LIMIT; if ((limit > 0) && (limit < TELEBOT_UPDATE_COUNT_MAX_LIMIT)) _limit = limit; - telebot_core_response_t response; - int ret = telebot_core_get_updates(_handle->core_h, _offset, _limit, _timeout, - str_allowed_updates, &response); - if (ret != TELEBOT_ERROR_NONE) { - if (array) json_object_put(array); - return ret; - } - if (array) json_object_put(array); + response = telebot_core_get_updates(handle->core_h, _offset, _limit, _timeout, str_allowed_updates); + if (array) + json_object_put(array); - struct json_object *obj = telebot_parser_str_to_obj(response.data); + ret = telebot_core_get_response_code(response); + if (ret != TELEBOT_ERROR_NONE) + goto finish; + + const char *response_data = telebot_core_get_response_data(response); + obj = telebot_parser_str_to_obj(response_data); + ; if (obj == NULL) { ret = TELEBOT_ERROR_OPERATION_FAILED; @@ -204,14 +205,15 @@ telebot_error_e telebot_get_updates(telebot_handler_t handle, int offset, telebot_update_t *ups = *updates; for (int index = 0; index < *count; index++) { - if (ups[index].update_id >= _handle->offset) - _handle->offset = ups[index].update_id + 1; + if (ups[index].update_id >= handle->offset) + handle->offset = ups[index].update_id + 1; } } finish: - if (obj) json_object_put(obj); - telebot_core_put_response(&response); + if (obj) + json_object_put(obj); + telebot_core_put_response(response); return ret; } @@ -257,19 +259,23 @@ telebot_error_e telebot_put_updates(telebot_update_t *updates, int count) telebot_error_e telebot_get_me(telebot_handler_t handle, telebot_user_t *me) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + int ret = TELEBOT_ERROR_NONE; + telebot_core_response_t response; + struct json_object *obj = NULL; + + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (me == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; - telebot_core_response_t response; - int ret = telebot_core_get_me(_handle->core_h, &response); + response = telebot_core_get_me(handle->core_h); + ret = telebot_core_get_response_code(response); if (ret != TELEBOT_ERROR_NONE) - return ret; + goto finish; - struct json_object *obj = telebot_parser_str_to_obj(response.data); + const char *response_data = telebot_core_get_response_data(response); + obj = telebot_parser_str_to_obj(response_data); if (obj == NULL) { ret = TELEBOT_ERROR_OPERATION_FAILED; @@ -293,11 +299,15 @@ telebot_error_e telebot_get_me(telebot_handler_t handle, telebot_user_t *me) ret = telebot_parser_get_user(result, me); finish: - if (ret) telebot_put_me(me); - if (obj) json_object_put(obj); - telebot_core_put_response(&response); + if (ret != TELEBOT_ERROR_NONE) + telebot_put_me(me); - return TELEBOT_ERROR_NONE; + if (obj) + json_object_put(obj); + + telebot_core_put_response(response); + + return ret; } telebot_error_e telebot_put_me(telebot_user_t *me) @@ -310,74 +320,66 @@ telebot_error_e telebot_put_me(telebot_user_t *me) return TELEBOT_ERROR_NONE; } -telebot_error_e telebot_set_webhook(telebot_handler_t handle, char *url, - char *certificate, int max_connections,telebot_update_type_e allowed_updates[], - int allowed_updates_count) +telebot_error_e telebot_set_webhook(telebot_handler_t handle, char *url, char *certificate, int max_connections, + telebot_update_type_e allowed_updates[], int allowed_updates_count) { - int i = 0; - char allowed_updates_str[TELEBOT_BUFFER_PAGE] = { - 0, - }; - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + telebot_core_response_t response; + + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (url == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; + char allowed_updates_str[TELEBOT_BUFFER_PAGE] = {}; if (allowed_updates_count > 0) { strncat(allowed_updates_str, "[", TELEBOT_BUFFER_BLOCK); - for (i = 0; i < allowed_updates_count; i++) + for (int index = 0; index < allowed_updates_count; index++) { - strncat(allowed_updates_str, telebot_update_type_str[allowed_updates[i]], + strncat(allowed_updates_str, telebot_update_type_str[allowed_updates[index]], TELEBOT_BUFFER_BLOCK); - if (i < (allowed_updates_count - 1)) //intermediate element + if (index < (allowed_updates_count - 1)) // intermediate element strncat(allowed_updates_str, ",", TELEBOT_BUFFER_BLOCK); } strncat(allowed_updates_str, "]", TELEBOT_BUFFER_BLOCK); } - telebot_core_response_t response; - int ret = telebot_core_set_webhook(_handle->core_h, url, certificate, - max_connections, allowed_updates_str, &response); - - telebot_core_put_response(&response); + response = telebot_core_set_webhook(handle->core_h, url, certificate, max_connections, allowed_updates_str); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_delete_webhook(telebot_handler_t handle) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; - telebot_core_response_t response; - int ret = telebot_core_delete_webhook(_handle->core_h, &response); - - telebot_core_put_response(&response); + telebot_core_response_t response = telebot_core_delete_webhook(handle->core_h); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_get_webhook_info(telebot_handler_t handle, telebot_webhook_info_t *info) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (info == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; - telebot_core_response_t response; - int ret = telebot_core_get_webhook_info(_handle->core_h, &response); + struct json_object *obj = NULL; + telebot_core_response_t response = telebot_core_get_webhook_info(handle->core_h); + int ret = telebot_core_get_response_code(response); if (ret != TELEBOT_ERROR_NONE) - { - return ret; - } + goto finish; - struct json_object *obj = telebot_parser_str_to_obj(response.data); + const char *response_data = telebot_core_get_response_data(response); + obj = telebot_parser_str_to_obj(response_data); if (obj == NULL) { ret = TELEBOT_ERROR_OPERATION_FAILED; @@ -401,9 +403,13 @@ telebot_error_e telebot_get_webhook_info(telebot_handler_t handle, telebot_webho ret = telebot_parser_get_webhook_info(result, info); finish: - if (ret) telebot_put_webhook_info(info); - if (obj) json_object_put(obj); - telebot_core_put_response(&response); + if (ret != TELEBOT_ERROR_NONE) + telebot_put_webhook_info(info); + + if (obj) + json_object_put(obj); + + telebot_core_put_response(response); return ret; } @@ -419,290 +425,282 @@ telebot_error_e telebot_put_webhook_info(telebot_webhook_info_t *info) return TELEBOT_ERROR_NONE; } -telebot_error_e telebot_send_message(telebot_handler_t handle, long long int chat_id, - const char *text, const char *parse_mode, bool disable_web_page_preview, - bool disable_notification, int reply_to_message_id, const char *reply_markup) +telebot_error_e telebot_send_message(telebot_handler_t handle, long long int chat_id, const char *text, + const char *parse_mode, bool disable_web_page_preview, bool disable_notification, + int reply_to_message_id, const char *reply_markup) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (text == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; - telebot_core_response_t response; - int ret = telebot_core_send_message(_handle->core_h, chat_id, text, parse_mode, - disable_web_page_preview, disable_notification, reply_to_message_id, - reply_markup, &response); - telebot_core_put_response(&response); + telebot_core_response_t response = telebot_core_send_message(handle->core_h, chat_id, text, parse_mode, + disable_web_page_preview, disable_notification, + reply_to_message_id, reply_markup); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } -telebot_error_e telebot_forward_message(telebot_handler_t handle, long long int chat_id, - long long int from_chat_id, bool disable_notification, int message_id) +telebot_error_e telebot_forward_message(telebot_handler_t handle, long long int chat_id, long long int from_chat_id, + bool disable_notification, int message_id) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (message_id <= 0) return TELEBOT_ERROR_INVALID_PARAMETER; - telebot_core_response_t response; - int ret = telebot_core_forward_message(_handle->core_h, chat_id, from_chat_id, - disable_notification, message_id, &response); - telebot_core_put_response(&response); + telebot_core_response_t response = telebot_core_forward_message(handle->core_h, chat_id, from_chat_id, + disable_notification, message_id); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } -telebot_error_e telebot_send_photo(telebot_handler_t handle, long long int chat_id, - const char *photo, bool is_file, const char *caption, const char *parse_mode, - bool disable_notification, int reply_to_message_id, const char *reply_markup) +telebot_error_e telebot_send_photo(telebot_handler_t handle, long long int chat_id, const char *photo, bool is_file, + const char *caption, const char *parse_mode, bool disable_notification, + int reply_to_message_id, const char *reply_markup) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + telebot_core_response_t response; + + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (photo == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; - telebot_core_response_t response; - int ret = telebot_core_send_photo(_handle->core_h, chat_id, photo, is_file, caption, - parse_mode, disable_notification, reply_to_message_id, reply_markup, &response); - telebot_core_put_response(&response); + response = telebot_core_send_photo(handle->core_h, chat_id, photo, is_file, caption, + parse_mode, disable_notification, reply_to_message_id, + reply_markup); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } -telebot_error_e telebot_send_audio(telebot_handler_t handle, long long int chat_id, - const char *audio, bool is_file, const char *caption, const char *parse_mode, - int duration, const char *performer, const char *title, const char *thumb, - bool disable_notification, int reply_to_message_id, const char *reply_markup) +telebot_error_e telebot_send_audio(telebot_handler_t handle, long long int chat_id, const char *audio, bool is_file, + const char *caption, const char *parse_mode, int duration, const char *performer, + const char *title, const char *thumb, bool disable_notification, + int reply_to_message_id, const char *reply_markup) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + telebot_core_response_t response; + + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (audio == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; - telebot_core_response_t response; - int ret = telebot_core_send_audio(_handle->core_h, chat_id, audio, is_file, caption, - parse_mode, duration, performer, title, thumb, disable_notification, - reply_to_message_id, reply_markup, &response); - telebot_core_put_response(&response); + response = telebot_core_send_audio(handle->core_h, chat_id, audio, is_file, caption, + parse_mode, duration, performer, title, thumb, + disable_notification, reply_to_message_id, reply_markup); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_send_document(telebot_handler_t handle, long long int chat_id, - const char *document, bool is_file, const char *thumb, const char *caption, - const char *parse_mode, bool disable_notification, int reply_to_message_id, - const char *reply_markup) + const char *document, bool is_file, const char *thumb, const char *caption, + const char *parse_mode, bool disable_notification, int reply_to_message_id, + const char *reply_markup) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + telebot_core_response_t response; + + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (document == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; - telebot_core_response_t response; - int ret = telebot_core_send_document(_handle->core_h, chat_id, document, is_file, - thumb, caption, parse_mode, disable_notification, reply_to_message_id, - reply_markup, &response); - telebot_core_put_response(&response); + response = telebot_core_send_document(handle->core_h, chat_id, document, is_file, thumb, caption, parse_mode, + disable_notification, reply_to_message_id, reply_markup); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } -telebot_error_e telebot_send_video(telebot_handler_t handle, long long int chat_id, - const char *video, bool is_file, int duration, int width, int height, - const char *thumb, const char *caption, const char *parse_mode, - bool supports_streaming, bool disable_notification, int reply_to_message_id, - const char *reply_markup) +telebot_error_e telebot_send_video(telebot_handler_t handle, long long int chat_id, const char *video, bool is_file, + int duration, int width, int height, const char *thumb, const char *caption, + const char *parse_mode, bool supports_streaming, bool disable_notification, + int reply_to_message_id, const char *reply_markup) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + telebot_core_response_t response; + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (video == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; - telebot_core_response_t response; - int ret = telebot_core_send_video(_handle->core_h, chat_id, video, is_file, duration, - width, height, thumb, caption, parse_mode, supports_streaming, disable_notification, - reply_to_message_id, reply_markup, &response); - telebot_core_put_response(&response); + response = telebot_core_send_video(handle->core_h, chat_id, video, is_file, duration, width, height, thumb, + caption, parse_mode, supports_streaming, disable_notification, + reply_to_message_id, reply_markup); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } -telebot_error_e telebot_send_animation(telebot_handler_t handle, long long int chat_id, - const char *animation, bool is_file, int duration, int width, int height, - const char *thumb, const char *caption, const char *parse_mode, - bool disable_notification, int reply_to_message_id, const char *reply_markup) +telebot_error_e telebot_send_animation(telebot_handler_t handle, long long int chat_id, const char *animation, + bool is_file, int duration, int width, int height, const char *thumb, + const char *caption, const char *parse_mode, bool disable_notification, + int reply_to_message_id, const char *reply_markup) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + telebot_core_response_t response; + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (animation == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; - telebot_core_response_t response; - int ret = telebot_core_send_animation(_handle->core_h, chat_id, animation, is_file, - duration, width, height, thumb, caption, parse_mode, disable_notification, - reply_to_message_id, reply_markup, &response); - if (ret != TELEBOT_ERROR_NONE) - return ret; - telebot_core_put_response(&response); + response = telebot_core_send_animation(handle->core_h, chat_id, animation, is_file, duration, width, height, + thumb, caption, parse_mode, disable_notification, reply_to_message_id, + reply_markup); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); - return TELEBOT_ERROR_NONE; + return ret; } -telebot_error_e telebot_send_voice(telebot_handler_t handle, long long int chat_id, - const char *voice, bool is_file, const char *caption, const char *parse_mode, - int duration, bool disable_notification, int reply_to_message_id, - const char *reply_markup) +telebot_error_e telebot_send_voice(telebot_handler_t handle, long long int chat_id, const char *voice, bool is_file, + const char *caption, const char *parse_mode, int duration, bool disable_notification, + int reply_to_message_id, const char *reply_markup) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + telebot_core_response_t response; + + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (voice == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; - telebot_core_response_t response; - int ret = telebot_core_send_voice(_handle->core_h, chat_id, voice, is_file, - caption, parse_mode, duration, disable_notification, reply_to_message_id, - reply_markup, &response); - if (ret != TELEBOT_ERROR_NONE) - return ret; - telebot_core_put_response(&response); + response = telebot_core_send_voice(handle->core_h, chat_id, voice, is_file, caption, parse_mode, duration, + disable_notification, reply_to_message_id, reply_markup); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); - return TELEBOT_ERROR_NONE; + return ret; } telebot_error_e telebot_send_video_note(telebot_handler_t handle, long long int chat_id, - char *video_note, bool is_file, int duration, int length, const char *thumb, - bool disable_notification, int reply_to_message_id, const char *reply_markup) + char *video_note, bool is_file, int duration, int length, const char *thumb, + bool disable_notification, int reply_to_message_id, const char *reply_markup) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + telebot_core_response_t response; + + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (video_note == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; - telebot_core_response_t response; - int ret = telebot_core_send_video_note(_handle->core_h, chat_id, video_note, - is_file, duration, length, thumb, disable_notification, reply_to_message_id, - reply_markup, &response); - telebot_core_put_response(&response); + response = telebot_core_send_video_note(handle->core_h, chat_id, video_note, is_file, duration, length, thumb, + disable_notification, reply_to_message_id, reply_markup); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_send_location(telebot_handler_t handle, long long int chat_id, - float latitude, float longitude, int live_period, bool disable_notification, - int reply_to_message_id, const char *reply_markup) + float latitude, float longitude, int live_period, bool disable_notification, + int reply_to_message_id, const char *reply_markup) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; telebot_core_response_t response; - int ret = telebot_core_send_location(_handle->core_h, chat_id, latitude, longitude, - live_period, disable_notification, reply_to_message_id, reply_markup, &response); - telebot_core_put_response(&response); + response = telebot_core_send_location(handle->core_h, chat_id, latitude, longitude, + live_period, disable_notification, reply_to_message_id, reply_markup); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_edit_message_live_location(telebot_handler_t handle, - long long int chat_id, int message_id, const char *inline_message_id, - float latitude, float longitude, const char *reply_markup) + long long int chat_id, int message_id, const char *inline_message_id, + float latitude, float longitude, const char *reply_markup) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; telebot_core_response_t response; - int ret = telebot_core_edit_message_live_location(_handle->core_h, chat_id, - message_id, inline_message_id, latitude, longitude, reply_markup, &response); - telebot_core_put_response(&response); + response = telebot_core_edit_message_live_location(handle->core_h, chat_id, + message_id, inline_message_id, latitude, longitude, reply_markup); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_stop_message_live_location(telebot_handler_t handle, - long long int chat_id, int message_id, char *inline_message_id, - const char *reply_markup) + long long int chat_id, int message_id, char *inline_message_id, + const char *reply_markup) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; telebot_core_response_t response; - int ret = telebot_core_stop_message_live_location(_handle->core_h, chat_id, - message_id, inline_message_id, reply_markup, &response); - telebot_core_put_response(&response); + response = telebot_core_stop_message_live_location(handle->core_h, chat_id, message_id, inline_message_id, reply_markup); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_send_venue(telebot_handler_t handle, long long int chat_id, - float latitude, float longitude, const char *title, const char *address, - const char *foursquare_id, const char *foursquare_type,bool disable_notification, - int reply_to_message_id, const char *reply_markup) + float latitude, float longitude, const char *title, const char *address, + const char *foursquare_id, const char *foursquare_type, bool disable_notification, + int reply_to_message_id, const char *reply_markup) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if ((title == NULL) || (address == NULL)) return TELEBOT_ERROR_INVALID_PARAMETER; telebot_core_response_t response; - int ret = telebot_core_send_venue(_handle->core_h, chat_id, latitude, longitude, - title, address, foursquare_id, foursquare_type, disable_notification, - reply_to_message_id, reply_markup, &response); - telebot_core_put_response(&response); + response = telebot_core_send_venue(handle->core_h, chat_id, latitude, longitude, title, address, foursquare_id, + foursquare_type, disable_notification, reply_to_message_id, reply_markup); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_send_contact(telebot_handler_t handle, long long int chat_id, - const char *phone_number, const char *first_name, const char *last_name, - const char *vcard, bool disable_notification, int reply_to_message_id, - const char *reply_markup) + const char *phone_number, const char *first_name, const char *last_name, + const char *vcard, bool disable_notification, int reply_to_message_id, + const char *reply_markup) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if ((phone_number == NULL) || (first_name == NULL)) return TELEBOT_ERROR_INVALID_PARAMETER; telebot_core_response_t response; - int ret = telebot_core_send_contact(_handle->core_h, chat_id, phone_number, - first_name, last_name, vcard, disable_notification, reply_to_message_id, - reply_markup, &response); - telebot_core_put_response(&response); + response = telebot_core_send_contact(handle->core_h, chat_id, phone_number, + first_name, last_name, vcard, disable_notification, reply_to_message_id, + reply_markup); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } -telebot_error_e telebot_send_poll(telebot_handler_t handle, long long int chat_id, - const char *question, const char **options, int count_options, bool is_anonymous, - const char *type, bool allows_multiple_answers, int correct_option_id, bool is_closed, - bool disable_notification, int reply_to_message_id, const char *reply_markup) +telebot_error_e telebot_send_poll(telebot_handler_t handle, long long int chat_id, const char *question, + const char **options, int count_options, bool is_anonymous, const char *type, + bool allows_multiple_answers, int correct_option_id, bool is_closed, + bool disable_notification, int reply_to_message_id, const char *reply_markup) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if ((question == NULL) || (options == NULL) || (count_options <= 0)) @@ -716,49 +714,48 @@ telebot_error_e telebot_send_poll(telebot_handler_t handle, long long int chat_i DBG("Poll options: %s", array_options); telebot_core_response_t response; - int ret = telebot_core_send_poll(_handle->core_h, chat_id, question, array_options, - is_anonymous, type, allows_multiple_answers, correct_option_id, is_closed, - disable_notification, reply_to_message_id, reply_markup, &response); - telebot_core_put_response(&response); + response = telebot_core_send_poll(handle->core_h, chat_id, question, array_options, is_anonymous, type, + allows_multiple_answers, correct_option_id, is_closed, disable_notification, + reply_to_message_id, reply_markup); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); json_object_put(array); return ret; } -telebot_error_e telebot_send_dice(telebot_handler_t handle, long long int chat_id, - bool disable_notification, int reply_to_message_id, const char *reply_markup) +telebot_error_e telebot_send_dice(telebot_handler_t handle, long long int chat_id, bool disable_notification, + int reply_to_message_id, const char *reply_markup) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; telebot_core_response_t response; - int ret = telebot_core_send_dice(_handle->core_h, chat_id, disable_notification, - reply_to_message_id, reply_markup, &response); - telebot_core_put_response(&response); + response = telebot_core_send_dice(handle->core_h, chat_id, disable_notification, + reply_to_message_id, reply_markup); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } -telebot_error_e telebot_send_chat_action(telebot_handler_t handle, long long int chat_id, - char *action) +telebot_error_e telebot_send_chat_action(telebot_handler_t handle, long long int chat_id, char *action) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; telebot_core_response_t response; - int ret = telebot_core_send_chat_action(_handle->core_h, chat_id, action, &response); - telebot_core_put_response(&response); + response = telebot_core_send_chat_action(handle->core_h, chat_id, action); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } -telebot_error_e telebot_get_user_profile_photos(telebot_handler_t handle, int user_id, - int offset, int limit, telebot_user_profile_photos_t *photos) +telebot_error_e telebot_get_user_profile_photos(telebot_handler_t handle, int user_id, int offset, int limit, + telebot_user_profile_photos_t *photos) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (photos == NULL) @@ -767,13 +764,15 @@ telebot_error_e telebot_get_user_profile_photos(telebot_handler_t handle, int us if ((limit <= 0) || (limit > TELEBOT_USER_PROFILE_PHOTOS_LIMIT)) limit = TELEBOT_USER_PROFILE_PHOTOS_LIMIT; + struct json_object *obj = NULL; telebot_core_response_t response; - int ret = telebot_core_get_user_profile_photos(_handle->core_h, user_id, offset, - limit, &response); + response = telebot_core_get_user_profile_photos(handle->core_h, user_id, offset, limit); + int ret = telebot_core_get_response_code(response); if (ret != TELEBOT_ERROR_NONE) - return ret; + goto finish; - struct json_object *obj = telebot_parser_str_to_obj(response.data); + const char *rdata = telebot_core_get_response_data(response); + obj = telebot_parser_str_to_obj(rdata); if (obj == NULL) { ret = TELEBOT_ERROR_OPERATION_FAILED; @@ -797,9 +796,14 @@ telebot_error_e telebot_get_user_profile_photos(telebot_handler_t handle, int us ret = telebot_parser_get_user_profile_photos(result, photos); finish: - if (ret) telebot_put_user_profile_photos(photos); - if (obj) json_object_put(obj); - telebot_core_put_response(&response); + if (ret != TELEBOT_ERROR_NONE) + telebot_put_user_profile_photos(photos); + + if (obj) + json_object_put(obj); + + telebot_core_put_response(response); + return ret; } @@ -822,19 +826,21 @@ telebot_error_e telebot_put_user_profile_photos(telebot_user_profile_photos_t *p telebot_error_e telebot_download_file(telebot_handler_t handle, const char *file_id, const char *path) { telebot_file_t file; - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (file_id == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; + struct json_object *obj = NULL; telebot_core_response_t response; - int ret = telebot_core_get_file(_handle->core_h, file_id, &response); + response = telebot_core_get_file(handle->core_h, file_id); + int ret = telebot_core_get_response_code(response); if (ret != TELEBOT_ERROR_NONE) - return ret; + goto finish; - struct json_object *obj = telebot_parser_str_to_obj(response.data); + const char *rdata = telebot_core_get_response_data(response); + obj = telebot_parser_str_to_obj(rdata); if (obj == NULL) { ret = TELEBOT_ERROR_OPERATION_FAILED; @@ -849,7 +855,8 @@ telebot_error_e telebot_download_file(telebot_handler_t handle, const char *file } struct json_object *result = NULL; - if (!json_object_object_get_ex(obj, "result", &result)) { + if (!json_object_object_get_ex(obj, "result", &result)) + { ret = TELEBOT_ERROR_OPERATION_FAILED; goto finish; } @@ -858,145 +865,140 @@ telebot_error_e telebot_download_file(telebot_handler_t handle, const char *file if (ret != TELEBOT_ERROR_NONE) goto finish; - if (file.file_path == NULL) { + if (file.file_path == NULL) + { ret = TELEBOT_ERROR_OPERATION_FAILED; goto finish; } - ret = telebot_core_download_file(_handle->core_h, file.file_path, path); + ret = telebot_core_download_file(handle->core_h, file.file_path, path); finish: telebot_put_file(&file); - if (obj) json_object_put(obj); - telebot_core_put_response(&response); + + if (obj) + json_object_put(obj); + + telebot_core_put_response(response); return ret; } -telebot_error_e telebot_kick_chat_member(telebot_handler_t handle, long long int chat_id, - int user_id, long until_date) +telebot_error_e telebot_kick_chat_member(telebot_handler_t handle, long long int chat_id, int user_id, long until_date) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; - telebot_core_response_t response; - int ret = telebot_core_kick_chat_member(_handle->core_h, chat_id, user_id, - until_date, &response); - telebot_core_put_response(&response); - + telebot_core_response_t response = telebot_core_kick_chat_member(handle->core_h, chat_id, user_id, until_date); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_unban_chat_member(telebot_handler_t handle, long long int chat_id, - int user_id) + int user_id) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; - telebot_core_response_t response; - int ret = telebot_core_unban_chat_member(_handle->core_h, chat_id, user_id, &response); - telebot_core_put_response(&response); - + telebot_core_response_t response = telebot_core_unban_chat_member(handle->core_h, chat_id, user_id); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } -telebot_error_e telebot_restrict_chat_member(telebot_handler_t handle, - long long int chat_id, int user_id, long until_date, bool can_send_messages, - bool can_send_media_messages, bool can_send_polls, bool can_send_other_messages, - bool can_add_web_page_previews, bool can_change_info, bool can_invite_users, - bool can_pin_messages) +telebot_error_e telebot_restrict_chat_member(telebot_handler_t handle, long long int chat_id, int user_id, + long until_date, bool can_send_messages, bool can_send_media_messages, + bool can_send_polls, bool can_send_other_messages, + bool can_add_web_page_previews, bool can_change_info, bool can_invite_users, + bool can_pin_messages) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; telebot_core_response_t response; - int ret = telebot_core_restrict_chat_member(_handle->core_h, chat_id, user_id, - until_date, can_send_messages, can_send_media_messages, can_send_polls, - can_send_other_messages, can_add_web_page_previews, can_change_info, - can_invite_users, can_pin_messages, &response); - telebot_core_put_response(&response); - + response = telebot_core_restrict_chat_member(handle->core_h, chat_id, user_id, + until_date, can_send_messages, can_send_media_messages, can_send_polls, + can_send_other_messages, can_add_web_page_previews, can_change_info, + can_invite_users, can_pin_messages); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_promote_chat_member(telebot_handler_t handle, - long long int chat_id, int user_id, bool can_change_info, bool can_post_messages, - bool can_edit_messages, bool can_delete_messages, bool can_invite_users, - bool can_restrict_members, bool can_pin_messages, bool can_promote_members) + long long int chat_id, int user_id, bool can_change_info, bool can_post_messages, + bool can_edit_messages, bool can_delete_messages, bool can_invite_users, + bool can_restrict_members, bool can_pin_messages, bool can_promote_members) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; - telebot_core_response_t response; - int ret = telebot_core_promote_chat_member(_handle->core_h, chat_id, user_id, - can_change_info, can_post_messages, can_edit_messages, can_delete_messages, - can_invite_users, can_restrict_members, can_pin_messages, can_promote_members, - &response); - telebot_core_put_response(&response); - + telebot_core_response_t response = telebot_core_promote_chat_member(handle->core_h, chat_id, user_id, + can_change_info, can_post_messages, + can_edit_messages, can_delete_messages, + can_invite_users, can_restrict_members, + can_pin_messages, can_promote_members); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_set_chat_admin_custom_title(telebot_handler_t handle, - long long int chat_id, int user_id, const char *custom_title) + long long int chat_id, int user_id, const char *custom_title) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (custom_title == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; telebot_core_response_t response; - int ret = telebot_core_set_chat_admin_custom_title(_handle->core_h, chat_id, - user_id, custom_title, &response); - telebot_core_put_response(&response); - + response = telebot_core_set_chat_admin_custom_title(handle->core_h, chat_id, + user_id, custom_title); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } - telebot_error_e telebot_set_chat_permissions(telebot_handler_t handle, - long long int chat_id, bool can_send_messages, bool can_send_media_messages, - bool can_send_polls, bool can_send_other_messages, bool can_add_web_page_previews, - bool can_change_info, bool can_invite_users, bool can_pin_messages) + long long int chat_id, bool can_send_messages, bool can_send_media_messages, + bool can_send_polls, bool can_send_other_messages, bool can_add_web_page_previews, + bool can_change_info, bool can_invite_users, bool can_pin_messages) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; telebot_core_response_t response; - int ret = telebot_core_set_chat_permissions(_handle->core_h, chat_id, - can_send_messages, can_send_media_messages, can_send_polls, - can_send_other_messages, can_add_web_page_previews, can_change_info, - can_invite_users, can_pin_messages, &response); - telebot_core_put_response(&response); + response = telebot_core_set_chat_permissions(handle->core_h, chat_id, + can_send_messages, can_send_media_messages, can_send_polls, + can_send_other_messages, can_add_web_page_previews, can_change_info, + can_invite_users, can_pin_messages); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_export_chat_invite_link(telebot_handler_t handle, - long long int chat_id, char **invite_link) + long long int chat_id, char **invite_link) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (invite_link == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; - *invite_link = NULL; - + struct json_object *obj = NULL; telebot_core_response_t response; - int ret = telebot_core_export_chat_invite_link(_handle->core_h, chat_id, &response); + response = telebot_core_export_chat_invite_link(handle->core_h, chat_id); + int ret = telebot_core_get_response_code(response); if (ret != TELEBOT_ERROR_NONE) - return ret; + goto finish; - struct json_object *obj = telebot_parser_str_to_obj(response.data); - if (obj == NULL) { + const char *rdata = telebot_core_get_response_data(response); + obj = telebot_parser_str_to_obj(rdata); + if (obj == NULL) + { ret = TELEBOT_ERROR_OPERATION_FAILED; goto finish; } @@ -1023,131 +1025,121 @@ telebot_error_e telebot_export_chat_invite_link(telebot_handler_t handle, } finish: - if (obj) json_object_put(obj); - telebot_core_put_response(&response); + if (obj) + json_object_put(obj); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_set_chat_photo(telebot_handler_t handle, long long int chat_id, - const char *photo) + const char *photo) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; - telebot_core_response_t response; - int ret = telebot_core_set_chat_photo(_handle->core_h, chat_id, photo, &response); - telebot_core_put_response(&response); - + telebot_core_response_t response = telebot_core_set_chat_photo(handle->core_h, chat_id, photo); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_delete_chat_photo(telebot_handler_t handle, long long int chat_id) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; - telebot_core_response_t response; - int ret = telebot_core_delete_chat_photo(_handle->core_h, chat_id, &response); - telebot_core_put_response(&response); - + telebot_core_response_t response = telebot_core_delete_chat_photo(handle->core_h, chat_id); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_set_chat_title(telebot_handler_t handle, long long int chat_id, - const char *title) + const char *title) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (title == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; - telebot_core_response_t response; - int ret = telebot_core_set_chat_title(_handle->core_h, chat_id, title, &response); - telebot_core_put_response(&response); - + telebot_core_response_t response = telebot_core_set_chat_title(handle->core_h, chat_id, title); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_set_chat_description(telebot_handler_t handle, - long long int chat_id, const char *description) + long long int chat_id, const char *description) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (description == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; - telebot_core_response_t response; - int ret = telebot_core_set_chat_description(_handle->core_h, chat_id, description, - &response); - telebot_core_put_response(&response); - + telebot_core_response_t response = telebot_core_set_chat_description(handle->core_h, chat_id, description); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_pin_chat_message(telebot_handler_t handle, long long int chat_id, - int message_id, bool disable_notification) + int message_id, bool disable_notification) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; telebot_core_response_t response; - int ret = telebot_core_pin_chat_message(_handle->core_h, chat_id, message_id, - disable_notification, &response); - telebot_core_put_response(&response); - + response = telebot_core_pin_chat_message(handle->core_h, chat_id, message_id, + disable_notification); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_unpin_chat_message(telebot_handler_t handle, long long int chat_id) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; telebot_core_response_t response; - int ret = telebot_core_unpin_chat_message(_handle->core_h, chat_id, &response); - telebot_core_put_response(&response); - + response = telebot_core_unpin_chat_message(handle->core_h, chat_id); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_leave_chat(telebot_handler_t handle, long long int chat_id) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; telebot_core_response_t response; - int ret = telebot_core_leave_chat(_handle->core_h, chat_id, &response); - telebot_core_put_response(&response); - + response = telebot_core_leave_chat(handle->core_h, chat_id); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_get_chat(telebot_handler_t handle, long long int chat_id, - telebot_chat_t *chat) + telebot_chat_t *chat) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (chat == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; - telebot_core_response_t response; - int ret = telebot_core_get_chat(_handle->core_h, chat_id, &response); + struct json_object *obj = NULL; + telebot_core_response_t response = telebot_core_get_chat(handle->core_h, chat_id); + int ret = telebot_core_get_response_code(response); if (ret != TELEBOT_ERROR_NONE) - return ret; + goto finish; - struct json_object *obj = telebot_parser_str_to_obj(response.data); + const char *rdata = telebot_core_get_response_data(response); + obj = telebot_parser_str_to_obj(rdata); if (obj == NULL) { ret = TELEBOT_ERROR_OPERATION_FAILED; @@ -1171,31 +1163,33 @@ telebot_error_e telebot_get_chat(telebot_handler_t handle, long long int chat_id ret = telebot_parser_get_chat(result, chat); finish: - if (ret) telebot_put_chat(chat); - if (obj) json_object_put(obj); - telebot_core_put_response(&response); + if (ret != TELEBOT_ERROR_NONE) + telebot_put_chat(chat); + + if (obj) + json_object_put(obj); + + telebot_core_put_response(response); return ret; } telebot_error_e telebot_get_chat_admins(telebot_handler_t handle, long long int chat_id, - telebot_chat_member_t **admins, int *count) + telebot_chat_member_t **admins, int *count) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if ((admins == NULL) || (count == NULL)) return TELEBOT_ERROR_INVALID_PARAMETER; - *admins = NULL; - *count = 0; - - telebot_core_response_t response; - int ret = telebot_core_get_chat_admins(_handle->core_h, chat_id, &response); + struct json_object *obj = NULL; + telebot_core_response_t response = telebot_core_get_chat_admins(handle->core_h, chat_id); + int ret = telebot_core_get_response_code(response); if (ret != TELEBOT_ERROR_NONE) - return ret; + goto finish; - struct json_object *obj = telebot_parser_str_to_obj(response.data); + const char *rdata = telebot_core_get_response_data(response); + obj = telebot_parser_str_to_obj(rdata); if (obj == NULL) { ret = TELEBOT_ERROR_OPERATION_FAILED; @@ -1219,9 +1213,13 @@ telebot_error_e telebot_get_chat_admins(telebot_handler_t handle, long long int ret = telebot_parser_get_chat_admins(result, admins, count); finish: - if (ret) telebot_put_chat_admins(*admins, *count); - if (obj) json_object_put(obj); - telebot_core_put_response(&response); + if (ret != TELEBOT_ERROR_NONE) + telebot_put_chat_admins(*admins, *count); + + if (obj) + json_object_put(obj); + + telebot_core_put_response(response); return ret; } @@ -1239,24 +1237,24 @@ telebot_error_e telebot_put_chat_admins(telebot_chat_member_t *admins, int count return TELEBOT_ERROR_NONE; } -telebot_error_e telebot_get_chat_members_count(telebot_handler_t handle, - long long int chat_id, int *count) +telebot_error_e telebot_get_chat_members_count(telebot_handler_t handle, long long int chat_id, int *count) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (count == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; - *count = 0; - telebot_core_response_t response; - int ret = telebot_core_get_chat_members_count(_handle->core_h, chat_id, &response); + struct json_object *obj = NULL; + telebot_core_response_t response = telebot_core_get_chat_members_count(handle->core_h, chat_id); + int ret = telebot_core_get_response_code(response); if (ret != TELEBOT_ERROR_NONE) - return ret; + goto finish; - struct json_object *obj = telebot_parser_str_to_obj(response.data); - if (obj == NULL) { + const char *rdata = telebot_core_get_response_data(response); + obj = telebot_parser_str_to_obj(rdata); + if (obj == NULL) + { ret = TELEBOT_ERROR_OPERATION_FAILED; goto finish; } @@ -1275,28 +1273,30 @@ telebot_error_e telebot_get_chat_members_count(telebot_handler_t handle, ret = TELEBOT_ERROR_OPERATION_FAILED; finish: - if (obj) json_object_put(obj); - telebot_core_put_response(&response); + if (obj) + json_object_put(obj); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_get_chat_member(telebot_handler_t handle, long long int chat_id, - int user_id, telebot_chat_member_t *member) + int user_id, telebot_chat_member_t *member) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (member == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; - telebot_core_response_t response; - int ret = telebot_core_get_chat_member(_handle->core_h, chat_id, user_id, &response); + struct json_object *obj = NULL; + telebot_core_response_t response = telebot_core_get_chat_member(handle->core_h, chat_id, user_id); + int ret = telebot_core_get_response_code(response); if (ret != TELEBOT_ERROR_NONE) - return ret; + goto finish; - struct json_object *obj = telebot_parser_str_to_obj(response.data); + const char *rdata = telebot_core_get_response_data(response); + obj = telebot_parser_str_to_obj(rdata); if (obj == NULL) { ret = TELEBOT_ERROR_OPERATION_FAILED; @@ -1320,9 +1320,13 @@ telebot_error_e telebot_get_chat_member(telebot_handler_t handle, long long int ret = telebot_parser_get_chat_member(result, member); finish: - if (ret) telebot_put_chat_member(member); - if (obj) json_object_put(obj); - telebot_core_put_response(&response); + if (ret != TELEBOT_ERROR_NONE) + telebot_put_chat_member(member); + + if (obj) + json_object_put(obj); + + telebot_core_put_response(response); return ret; } @@ -1339,61 +1343,54 @@ telebot_error_e telebot_put_chat_member(telebot_chat_member_t *member) } telebot_error_e telebot_set_chat_sticker_set(telebot_handler_t handle, - long long int chat_id, const char *sticker_set_name) + long long int chat_id, const char *sticker_set_name) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (sticker_set_name == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; - telebot_core_response_t response; - int ret = telebot_core_set_chat_sticker_set(_handle->core_h, chat_id, - sticker_set_name, &response); - telebot_core_put_response(&response); + telebot_core_response_t response = telebot_core_set_chat_sticker_set(handle->core_h, chat_id, sticker_set_name); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_delete_chat_sticker_set(telebot_handler_t handle, - long long int chat_id) + long long int chat_id) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; - telebot_core_response_t response; - int ret = telebot_core_delete_chat_sticker_set(_handle->core_h, chat_id, &response); - telebot_core_put_response(&response); - + telebot_core_response_t response = telebot_core_delete_chat_sticker_set(handle->core_h, chat_id); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_answer_callback_query(telebot_handler_t handle, - const char *callback_query_id, const char *text, bool show_alert, - const char *url, int cache_time) + const char *callback_query_id, const char *text, bool show_alert, + const char *url, int cache_time) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (callback_query_id == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; - telebot_core_response_t response; - int ret = telebot_core_answer_callback_query(_handle->core_h, callback_query_id, - text, show_alert, url, cache_time, &response); - telebot_core_put_response(&response); - + telebot_core_response_t response = telebot_core_answer_callback_query(handle->core_h, callback_query_id, + text, show_alert, url, cache_time); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_set_my_commands(telebot_handler_t handle, - telebot_bot_command_t commands[], int count) + telebot_bot_command_t commands[], int count) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if ((commands == NULL) || (count <= 0)) @@ -1414,32 +1411,30 @@ telebot_error_e telebot_set_my_commands(telebot_handler_t handle, const char *array_options = json_object_to_json_string(array); DBG("Commands: %s", array_options); - telebot_core_response_t response; - int ret = telebot_core_set_my_commands(_handle->core_h, array_options, &response); - telebot_core_put_response(&response); + telebot_core_response_t response = telebot_core_set_my_commands(handle->core_h, array_options); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); json_object_put(array); - return ret; } telebot_error_e telebot_get_my_commands(telebot_handler_t handle, - telebot_bot_command_t **commands, int *count) + telebot_bot_command_t **commands, int *count) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if ((commands == NULL) || (count == NULL)) return TELEBOT_ERROR_INVALID_PARAMETER; - *commands = NULL; - *count = 0; - telebot_core_response_t response; - int ret = telebot_core_get_my_commands(_handle->core_h, &response); + struct json_object *obj = NULL; + telebot_core_response_t response = telebot_core_get_my_commands(handle->core_h); + int ret = telebot_core_get_response_code(response); if (ret != TELEBOT_ERROR_NONE) - return ret; + goto finish; - struct json_object *obj = telebot_parser_str_to_obj(response.data); + const char *rdata = telebot_core_get_response_data(response); + obj = telebot_parser_str_to_obj(rdata); if (obj == NULL) { ret = TELEBOT_ERROR_OPERATION_FAILED; @@ -1463,9 +1458,11 @@ telebot_error_e telebot_get_my_commands(telebot_handler_t handle, ret = telebot_parser_get_array_bot_command(result, commands, count); finish: - if (ret) telebot_put_my_commands(*commands, *count); - if (obj) json_object_put(obj); - telebot_core_put_response(&response); + if (ret) + telebot_put_my_commands(*commands, *count); + if (obj) + json_object_put(obj); + telebot_core_put_response(response); return ret; } @@ -1484,86 +1481,74 @@ telebot_error_e telebot_put_my_commands(telebot_bot_command_t *commands, int cou } telebot_error_e telebot_edit_message_text(telebot_handler_t handle, - long long int chat_id, int message_id, const char *inline_message_id, - const char *text, const char *parse_mode, bool disable_web_page_preview, - const char *reply_markup) + long long int chat_id, int message_id, const char *inline_message_id, + const char *text, const char *parse_mode, bool disable_web_page_preview, + const char *reply_markup) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; if (text == NULL) return TELEBOT_ERROR_INVALID_PARAMETER; - telebot_core_response_t response; - int ret = telebot_core_edit_message_text(_handle->core_h, chat_id, message_id, - inline_message_id, text, parse_mode, disable_web_page_preview, - reply_markup, &response); - telebot_core_put_response(&response); - + telebot_core_response_t response = telebot_core_edit_message_text(handle->core_h, chat_id, message_id, + inline_message_id, text, parse_mode, + disable_web_page_preview, + reply_markup); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_edit_message_caption(telebot_handler_t handle, - long long int chat_id, int message_id, const char *inline_message_id, - const char *caption, const char *parse_mode, const char *reply_markup) + long long int chat_id, int message_id, const char *inline_message_id, + const char *caption, const char *parse_mode, const char *reply_markup) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; - telebot_core_response_t response; - int ret = telebot_core_edit_message_caption(_handle->core_h, chat_id, message_id, - inline_message_id, caption, parse_mode, reply_markup, &response); - telebot_core_put_response(&response); - + telebot_core_response_t response = telebot_core_edit_message_caption(handle->core_h, chat_id, message_id, + inline_message_id, caption, parse_mode, + reply_markup); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } telebot_error_e telebot_edit_message_reply_markup(telebot_handler_t handle, - long long int chat_id, int message_id, const char *inline_message_id, - const char *reply_markup) + long long int chat_id, int message_id, const char *inline_message_id, + const char *reply_markup) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; - telebot_core_response_t response; - int ret = telebot_core_edit_message_reply_markup(_handle->core_h, chat_id, - message_id, inline_message_id, reply_markup, &response); - telebot_core_put_response(&response); - + telebot_core_response_t response = telebot_core_edit_message_reply_markup(handle->core_h, chat_id, message_id, + inline_message_id, reply_markup); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } - telebot_error_e telebot_stop_poll(telebot_handler_t handle, long long int chat_id, - int message_id, const char *reply_markup) + int message_id, const char *reply_markup) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; - telebot_core_response_t response; - int ret = telebot_core_stop_poll(_handle->core_h, chat_id, - message_id, reply_markup, &response); - telebot_core_put_response(&response); - + telebot_core_response_t response = telebot_core_stop_poll(handle->core_h, chat_id, message_id, reply_markup); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } -telebot_error_e telebot_delete_message(telebot_handler_t handle, long long int chat_id, - int message_id) +telebot_error_e telebot_delete_message(telebot_handler_t handle, long long int chat_id, int message_id) { - telebot_hdata_t *_handle = (telebot_hdata_t *)handle; - if (_handle == NULL) + if (handle == NULL) return TELEBOT_ERROR_NOT_SUPPORTED; - telebot_core_response_t response; - int ret = telebot_core_delete_message(_handle->core_h, chat_id, - message_id, &response); - telebot_core_put_response(&response); - + telebot_core_response_t response = telebot_core_delete_message(handle->core_h, chat_id, message_id); + int ret = telebot_core_get_response_code(response); + telebot_core_put_response(response); return ret; } @@ -1620,7 +1605,7 @@ static void telebot_put_chat_permissions(telebot_chat_permissions_t *permissions { if (permissions == NULL) return; - //Nothing for now + // Nothing for now return; } @@ -1673,9 +1658,9 @@ static void telebot_put_message(telebot_message_t *msg) telebot_put_document(msg->document); TELEBOT_SAFE_FREE(msg->document); - //TODO - //telebot_put_game(msg->game); - //TELEBOT_SAFE_FREE(msg->game); + // TODO + // telebot_put_game(msg->game); + // TELEBOT_SAFE_FREE(msg->game); telebot_put_animation(msg->animation); TELEBOT_SAFE_FREE(msg->animation); @@ -1688,9 +1673,9 @@ static void telebot_put_message(telebot_message_t *msg) msg->count_photos = 0; } - //TODO - //telebot_put_sticker(msg->sticker); - //TELEBOT_SAFE_FREE(msg->sticker); + // TODO + // telebot_put_sticker(msg->sticker); + // TELEBOT_SAFE_FREE(msg->sticker); telebot_put_video(msg->video); TELEBOT_SAFE_FREE(msg->video); @@ -1746,21 +1731,21 @@ static void telebot_put_message(telebot_message_t *msg) telebot_put_message(msg->pinned_message); - //TODO - //telebot_put_invoice(msg->invoice); - //TELEBOT_SAFE_FREE(msg->invoice); + // TODO + // telebot_put_invoice(msg->invoice); + // TELEBOT_SAFE_FREE(msg->invoice); - //TODO - //telebot_put_payment(msg->successful_payment); - //TELEBOT_SAFE_FREE(msg->successful_payment); + // TODO + // telebot_put_payment(msg->successful_payment); + // TELEBOT_SAFE_FREE(msg->successful_payment); - //TODO - //telebot_put_passport_data(msg->passport_data); - //TELEBOT_SAFE_FREE(msg->passport_data); + // TODO + // telebot_put_passport_data(msg->passport_data); + // TELEBOT_SAFE_FREE(msg->passport_data); - //TODO - //telebot_put_inline_keyboard_markup(msg->reply_markup); - //TELEBOT_SAFE_FREE(msg->reply_markup); + // TODO + // telebot_put_inline_keyboard_markup(msg->reply_markup); + // TELEBOT_SAFE_FREE(msg->reply_markup); } static void telebot_put_telebot_message_entity(telebot_message_entity_t *entity) @@ -1878,7 +1863,7 @@ static void telebot_put_location(telebot_location_t *location) { if (location == NULL) return; - //Nothing to free + // Nothing to free return; } @@ -1944,7 +1929,7 @@ static void telebot_put_dice(telebot_dice_t *dice) { if (dice == NULL) return; - //Nothing to free + // Nothing to free return; } @@ -1964,6 +1949,6 @@ static void telebot_put_callback_query(telebot_callback_query_t *query) TELEBOT_SAFE_FREE(query->game_short_name); } -//TODO: static void telebot_put_invoice(telebot_invoice_t *invoice); -//TODO: static void telebot_put_payment(telebot_successful_payment_t *payment); -//TODO: static void telebot_put_game(telebot_game_t *game); +// TODO: static void telebot_put_invoice(telebot_invoice_t *invoice); +// TODO: static void telebot_put_payment(telebot_successful_payment_t *payment); +// TODO: static void telebot_put_game(telebot_game_t *game); From b9ab0ee721372be3fd8d42ac323aed45a6dc91ce Mon Sep 17 00:00:00 2001 From: Elmurod Talipov Date: Sun, 20 Nov 2022 18:13:10 +0900 Subject: [PATCH 2/5] Simplify passing parameters with mime Signed-off-by: Elmurod Talipov --- include/telebot-private.h | 61 +- src/telebot-core.c | 2128 +++++++++++++++++++------------------ 2 files changed, 1137 insertions(+), 1052 deletions(-) diff --git a/include/telebot-private.h b/include/telebot-private.h index c139325..6a236db 100644 --- a/include/telebot-private.h +++ b/include/telebot-private.h @@ -88,11 +88,11 @@ #define TELEBOT_METHOD_DELETE_MESSAGE "deleteMessage" #ifdef DEBUG - #define ERR(fmt, args...) fprintf(stderr, "[ERROR][%s:%d]" fmt "\n", __func__, __LINE__, ##args) - #define DBG(fmt, args...) fprintf(stdout, "[DEBUG][%s:%d]" fmt "\n", __func__, __LINE__, ##args) +#define ERR(fmt, args...) fprintf(stderr, "[ERROR][%s:%d]" fmt "\n", __func__, __LINE__, ##args) +#define DBG(fmt, args...) fprintf(stdout, "[DEBUG][%s:%d]" fmt "\n", __func__, __LINE__, ##args) #else - #define ERR(x, ...) - #define DBG(x, ...) +#define ERR(x, ...) +#define DBG(x, ...) #endif #define CHECK_ARG_NULL(PARAM) \ @@ -109,35 +109,62 @@ return telebot_core_get_error_response(TELEBOT_ERROR_INVALID_PARAMETER); \ } -typedef enum { - TELEBOT_MIME_TYPE_DATA = 0, +typedef enum +{ + TELEBOT_MIME_TYPE_CHAR, + TELEBOT_MIME_TYPE_INT, + TELEBOT_MIME_TYPE_U_INT, + TELEBOT_MIME_TYPE_LONG_INT, + TELEBOT_MIME_TYPE_U_LONG_INT, + TELEBOT_MIME_TYPE_LONG_LONG_INT, + TELEBOT_MIME_TYPE_U_LONG_LONG_INT, + TELEBOT_MIME_TYPE_FLOAT, + TELEBOT_MIME_TYPE_DOUBLE, + TELEBOT_MIME_TYPE_LONG_DOUBLE, + TELEBOT_MIME_TYPE_STRING, TELEBOT_MIME_TYPE_FILE, TELEBOT_MIME_TYPE_MAX, } telebot_core_mime_e; -typedef struct { +typedef struct telebot_core_mime_s +{ telebot_core_mime_e type; const char *name; - char data[TELEBOT_BUFFER_PAGE]; -} telebot_core_mime_t; + union + { + char c; + int d; + unsigned int u; + long int ld; + unsigned long int lu; + long long lld; + unsigned long long llu; + float f; + double lf; + long double llf; + const char *s; + } data; +} telebot_core_mime_t; /** * @brief This object represents core handler. */ -struct telebot_core_handler { - char *token; /**< Telegam bot token */ - char *proxy_addr; /**< Proxy address (optional) */ - char *proxy_auth; /**< Proxy authentication (optional) */ +struct telebot_core_handler +{ + char *token; /**< Telegam bot token */ + char *proxy_addr; /**< Proxy address (optional) */ + char *proxy_auth; /**< Proxy authentication (optional) */ }; /** * @brief This object represents a telegram bot response. */ -struct telebot_core_response { - telebot_error_e ret; /**< Telegram bot response code */ - size_t size; /**< Telegam bot response size */ - char *data; /**< Telegam bot response object */ +struct telebot_core_response +{ + telebot_error_e ret; /**< Telegram bot response code */ + size_t size; /**< Telegam bot response size */ + char *data; /**< Telegam bot response object */ }; #endif /* __TELEBOT_PRIVATE_H__ */ diff --git a/src/telebot-core.c b/src/telebot-core.c index d3f22ea..bdbc38c 100644 --- a/src/telebot-core.c +++ b/src/telebot-core.c @@ -184,6 +184,66 @@ static size_t write_data_cb(void *contents, size_t size, size_t nmemb, void *use return r_size; } +static void telebot_core_copy_mime_data_to_part(telebot_core_mime_t *mime, curl_mimepart *part) +{ + curl_mime_name(part, mime->name); + + char buffer[TELEBOT_BUFFER_PAGE]; + switch (mime->type) + { + case TELEBOT_MIME_TYPE_CHAR: + snprintf(buffer, sizeof(buffer), "%c", mime->data.c); + curl_mime_data(part, buffer, CURL_ZERO_TERMINATED); + break; + case TELEBOT_MIME_TYPE_INT: + snprintf(buffer, sizeof(buffer), "%d", mime->data.d); + curl_mime_data(part, buffer, CURL_ZERO_TERMINATED); + break; + case TELEBOT_MIME_TYPE_U_INT: + snprintf(buffer, sizeof(buffer), "%u", mime->data.u); + curl_mime_data(part, buffer, CURL_ZERO_TERMINATED); + break; + case TELEBOT_MIME_TYPE_LONG_INT: + snprintf(buffer, sizeof(buffer), "%ld", mime->data.ld); + curl_mime_data(part, buffer, CURL_ZERO_TERMINATED); + break; + case TELEBOT_MIME_TYPE_U_LONG_INT: + snprintf(buffer, sizeof(buffer), "%lu", mime->data.lu); + curl_mime_data(part, buffer, CURL_ZERO_TERMINATED); + break; + case TELEBOT_MIME_TYPE_LONG_LONG_INT: + snprintf(buffer, sizeof(buffer), "%lld", mime->data.lld); + curl_mime_data(part, buffer, CURL_ZERO_TERMINATED); + break; + case TELEBOT_MIME_TYPE_U_LONG_LONG_INT: + snprintf(buffer, sizeof(buffer), "%llu", mime->data.llu); + curl_mime_data(part, buffer, CURL_ZERO_TERMINATED); + break; + case TELEBOT_MIME_TYPE_FLOAT: + snprintf(buffer, sizeof(buffer), "%f", mime->data.f); + curl_mime_data(part, buffer, CURL_ZERO_TERMINATED); + break; + case TELEBOT_MIME_TYPE_DOUBLE: + snprintf(buffer, sizeof(buffer), "%lf", mime->data.lf); + curl_mime_data(part, buffer, CURL_ZERO_TERMINATED); + break; + case TELEBOT_MIME_TYPE_LONG_DOUBLE: + snprintf(buffer, sizeof(buffer), "%Lf", mime->data.llf); + curl_mime_data(part, buffer, CURL_ZERO_TERMINATED); + break; + case TELEBOT_MIME_TYPE_STRING: + curl_mime_data(part, mime->data.s, CURL_ZERO_TERMINATED); + break; + case TELEBOT_MIME_TYPE_FILE: + curl_mime_filedata(part, mime->data.s); + break; + case TELEBOT_MIME_TYPE_MAX: + default: + ERR("Invalid type: %d", mime->type); + break; + } +} + static telebot_core_response_t telebot_core_curl_perform(telebot_core_handler_t core_h, const char *method, telebot_core_mime_t mimes[], size_t size) { @@ -250,7 +310,7 @@ telebot_core_curl_perform(telebot_core_handler_t core_h, const char *method, tel resp->ret = TELEBOT_ERROR_OUT_OF_MEMORY; goto finish; } - for (int index = 0; index < size; index++) + for (size_t index = 0; index < size; index++) { curl_mimepart *part = curl_mime_addpart(mime); if (part == NULL) @@ -259,11 +319,7 @@ telebot_core_curl_perform(telebot_core_handler_t core_h, const char *method, tel resp->ret = TELEBOT_ERROR_OUT_OF_MEMORY; goto finish; } - curl_mime_name(part, mimes[index].name); - if (mimes[index].type == TELEBOT_MIME_TYPE_FILE) - curl_mime_filedata(part, mimes[index].data); - else - curl_mime_data(part, mimes[index].data, CURL_ZERO_TERMINATED); + telebot_core_copy_mime_data_to_part(&mimes[index], part); } curl_easy_setopt(curl_h, CURLOPT_MIMEPOST, mime); @@ -304,32 +360,32 @@ telebot_core_get_updates(telebot_core_handler_t core_h, int offset, int limit, i if (limit > TELEBOT_UPDATE_COUNT_MAX_LIMIT) limit = TELEBOT_UPDATE_COUNT_MAX_LIMIT; - int index = 0; + size_t count = 0; telebot_core_mime_t mimes[4]; // number of arguments - mimes[index].name = "offset"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", offset); - ++index; + mimes[count].name = "offset"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = offset; + count++; - mimes[index].name = "limit"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", limit); - ++index; + mimes[count].name = "limit"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = limit; + count++; - mimes[index].name = "timeout"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", timeout); - ++index; + mimes[count].name = "timeout"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = timeout; + count++; if (allowed_updates) { - mimes[index].name = "allowed_updates"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", allowed_updates); - ++index; + mimes[count].name = "allowed_updates"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = allowed_updates; + count++; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_UPDATES, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_UPDATES, mimes, count); } static telebot_core_response_t telebot_core_get_error_response(telebot_error_e ret) @@ -347,34 +403,34 @@ telebot_core_set_webhook(telebot_core_handler_t core_h, const char *url, const c { CHECK_ARG_NULL(url); - int index = 0; + size_t count = 0; telebot_core_mime_t mimes[4]; // number of arguments - mimes[index].name = "url"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", url); - ++index; + mimes[count].name = "url"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = url; + ++count; if (certificate != NULL) { - mimes[index].name = "certificate"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", certificate); - ++index; + mimes[count].name = "certificate"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = certificate; + count++; } - mimes[index].name = "max_connections"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", max_connections); + mimes[count].name = "max_connections"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = max_connections; if (allowed_updates != NULL) { - mimes[index].name = "allowed_updates"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", allowed_updates); - ++index; + mimes[count].name = "allowed_updates"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = allowed_updates; + count++; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_WEBHOOK, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_WEBHOOK, mimes, count); } telebot_core_response_t @@ -402,53 +458,53 @@ telebot_core_send_message(telebot_core_handler_t core_h, long long int chat_id, { CHECK_ARG_NULL(text); - int index = 0; + int count = 0; telebot_core_mime_t mimes[7]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "text"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", text); - ++index; + mimes[count].name = "text"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = text; + count++; if (parse_mode != NULL) { - mimes[index].name = "parse_mode"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", parse_mode); - ++index; + mimes[count].name = "parse_mode"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = parse_mode; + count++; } - mimes[index].name = "disable_notification"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (disable_notification ? "true" : "false")); - ++index; + mimes[count].name = "disable_notification"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = disable_notification ? "true" : "false"; + count++; - mimes[index].name = "disable_web_page_preview"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (disable_web_page_preview ? "true" : "false")); - ++index; + mimes[count].name = "disable_web_page_preview"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = disable_web_page_preview ? "true" : "false"; + count++; if (reply_to_message_id > 0) { - mimes[index].name = "reply_to_message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", reply_to_message_id); - ++index; + mimes[count].name = "reply_to_message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = reply_to_message_id; + count++; } if (reply_markup != NULL) { - mimes[index].name = "reply_markup"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", reply_markup); - ++index; + mimes[count].name = "reply_markup"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = reply_markup; + count++; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_MESSAGE, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_MESSAGE, mimes, count); } telebot_core_response_t @@ -461,29 +517,29 @@ telebot_core_forward_message(telebot_core_handler_t core_h, long long int chat_i return telebot_core_get_error_response(TELEBOT_ERROR_INVALID_PARAMETER); } - int index = 0; + int count = 0; telebot_core_mime_t mimes[4]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "from_chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", from_chat_id); - ++index; + mimes[count].name = "from_chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = from_chat_id; + count++; - mimes[index].name = "disable_notification"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (disable_notification ? "true" : "false")); - ++index; + mimes[count].name = "disable_notification"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = disable_notification ? "true" : "false"; + count++; - mimes[index].name = "message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", message_id); - ++index; + mimes[count].name = "message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = message_id; + count++; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_FORWARD_MESSAGE, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_FORWARD_MESSAGE, mimes, count); } telebot_core_response_t @@ -493,55 +549,55 @@ telebot_core_send_photo(telebot_core_handler_t core_h, long long int chat_id, co { CHECK_ARG_NULL(photo); - int index = 0; + int count = 0; telebot_core_mime_t mimes[7]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "photo"; - mimes[index].type = (is_file ? TELEBOT_MIME_TYPE_FILE : TELEBOT_MIME_TYPE_DATA); - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", photo); - ++index; + mimes[count].name = "photo"; + mimes[count].type = is_file ? TELEBOT_MIME_TYPE_FILE : TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = photo; + count++; if (caption != NULL) { - mimes[index].name = "caption"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", caption); - ++index; + mimes[count].name = "caption"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = caption; + count++; if (parse_mode != NULL) { - mimes[index].name = "parse_mode"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", parse_mode); - ++index; + mimes[count].name = "parse_mode"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = parse_mode; + count++; } } - mimes[index].name = "disable_notification"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (disable_notification ? "true" : "false")); - ++index; + mimes[count].name = "disable_notification"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = disable_notification ? "true" : "false"; + count++; if (reply_to_message_id > 0) { - mimes[index].name = "reply_to_message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", reply_to_message_id); - ++index; + mimes[count].name = "reply_to_message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = reply_to_message_id; + count++; } if (reply_markup != NULL) { - mimes[index].name = "reply_markup"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", reply_markup); - ++index; + mimes[count].name = "reply_markup"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = reply_markup; + count++; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_PHOTO, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_PHOTO, mimes, count); } telebot_core_response_t @@ -552,87 +608,87 @@ telebot_core_send_audio(telebot_core_handler_t core_h, long long int chat_id, co { CHECK_ARG_NULL(audio); - int index = 0; + int count = 0; telebot_core_mime_t mimes[11]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "audio"; - mimes[index].type = (is_file ? TELEBOT_MIME_TYPE_FILE : TELEBOT_MIME_TYPE_DATA); - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", audio); - ++index; + mimes[count].name = "audio"; + mimes[count].type = is_file ? TELEBOT_MIME_TYPE_FILE : TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = audio; + count++; if (caption != NULL) { - mimes[index].name = "caption"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", caption); - ++index; + mimes[count].name = "caption"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = caption; + count++; if (parse_mode != NULL) { - mimes[index].name = "parse_mode"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", parse_mode); - ++index; + mimes[count].name = "parse_mode"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = parse_mode; + count++; } } if (duration > 0) { - mimes[index].name = "duration"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", duration); - ++index; + mimes[count].name = "duration"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = duration; + count++; } if (performer != NULL) { - mimes[index].name = "performer"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", performer); - ++index; + mimes[count].name = "performer"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = performer; + count++; } if (title != NULL) { - mimes[index].name = "title"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", title); - ++index; + mimes[count].name = "title"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = title; + count++; } if (thumb != NULL) { - mimes[index].name = "thumb"; - mimes[index].type = TELEBOT_MIME_TYPE_FILE; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", thumb); - ++index; + mimes[count].name = "thumb"; + mimes[count].type = TELEBOT_MIME_TYPE_FILE; + mimes[count].data.s = thumb; + count++; } - mimes[index].name = "disable_notification"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (disable_notification ? "true" : "false")); - ++index; + mimes[count].name = "disable_notification"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = disable_notification ? "true" : "false"; + count++; if (reply_to_message_id > 0) { - mimes[index].name = "reply_to_message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", reply_to_message_id); - ++index; + mimes[count].name = "reply_to_message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = reply_to_message_id; + count++; } if (reply_markup != NULL) { - mimes[index].name = "reply_markup"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", reply_markup); - ++index; + mimes[count].name = "reply_markup"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = reply_markup; + count++; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_AUDIO, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_AUDIO, mimes, count); } telebot_core_response_t @@ -642,63 +698,63 @@ telebot_core_send_document(telebot_core_handler_t core_h, long long int chat_id, { CHECK_ARG_NULL(document); - int index = 0; + int count = 0; telebot_core_mime_t mimes[8]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "document"; - mimes[index].type = (is_file ? TELEBOT_MIME_TYPE_FILE : TELEBOT_MIME_TYPE_DATA); - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", document); - ++index; + mimes[count].name = "document"; + mimes[count].type = is_file ? TELEBOT_MIME_TYPE_FILE : TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = document; + count++; if (thumb != NULL) { - mimes[index].name = "thumb"; - mimes[index].type = TELEBOT_MIME_TYPE_FILE; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", thumb); - ++index; + mimes[count].name = "thumb"; + mimes[count].type = TELEBOT_MIME_TYPE_FILE; + mimes[count].data.s = thumb; + count++; } if (caption != NULL) { - mimes[index].name = "caption"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", caption); - ++index; + mimes[count].name = "caption"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = caption; + count++; if (parse_mode != NULL) { - mimes[index].name = "parse_mode"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", parse_mode); - ++index; + mimes[count].name = "parse_mode"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = parse_mode; + count++; } } - mimes[index].name = "disable_notification"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (disable_notification ? "true" : "false")); - ++index; + mimes[count].name = "disable_notification"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = disable_notification ? "true" : "false"; + count++; if (reply_to_message_id > 0) { - mimes[index].name = "reply_to_message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", reply_to_message_id); - ++index; + mimes[count].name = "reply_to_message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = reply_to_message_id; + count++; } if (reply_markup != NULL) { - mimes[index].name = "reply_markup"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", reply_markup); - ++index; + mimes[count].name = "reply_markup"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = reply_markup; + count++; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_DOCUMENT, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_DOCUMENT, mimes, count); } telebot_core_response_t @@ -709,92 +765,92 @@ telebot_core_send_video(telebot_core_handler_t core_h, long long int chat_id, co { CHECK_ARG_NULL(video); - int index = 0; + int count = 0; telebot_core_mime_t mimes[12]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "video"; - mimes[index].type = (is_file ? TELEBOT_MIME_TYPE_FILE : TELEBOT_MIME_TYPE_DATA); - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", video); - ++index; + mimes[count].name = "video"; + mimes[count].type = is_file ? TELEBOT_MIME_TYPE_FILE : TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = video; + count++; if (duration > 0) { - mimes[index].name = "duration"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", duration); - ++index; + mimes[count].name = "duration"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = duration; + count++; } if (width > 0) { - mimes[index].name = "width"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", width); - ++index; + mimes[count].name = "width"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = width; + count++; } if (height > 0) { - mimes[index].name = "height"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", height); - ++index; + mimes[count].name = "height"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = height; + count++; } if (thumb != NULL) { - mimes[index].name = "thumb"; - mimes[index].type = TELEBOT_MIME_TYPE_FILE; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", thumb); - ++index; + mimes[count].name = "thumb"; + mimes[count].type = TELEBOT_MIME_TYPE_FILE; + mimes[count].data.s = thumb; + count++; } if (caption != NULL) { - mimes[index].name = "caption"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", caption); - ++index; + mimes[count].name = "caption"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = caption; + count++; if (parse_mode != NULL) { - mimes[index].name = "parse_mode"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", parse_mode); - ++index; + mimes[count].name = "parse_mode"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = parse_mode; + count++; } } - mimes[index].name = "supports_streaming"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (supports_streaming ? "true" : "false")); - ++index; + mimes[count].name = "supports_streaming"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = supports_streaming ? "true" : "false"; + count++; - mimes[index].name = "disable_notification"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (disable_notification ? "true" : "false")); - ++index; + mimes[count].name = "disable_notification"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = disable_notification ? "true" : "false"; + count++; if (reply_to_message_id > 0) { - mimes[index].name = "reply_to_message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", reply_to_message_id); - ++index; + mimes[count].name = "reply_to_message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = reply_to_message_id; + count++; } if (reply_markup != NULL) { - mimes[index].name = "reply_markup"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", reply_markup); - ++index; + mimes[count].name = "reply_markup"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = reply_markup; + count++; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_VIDEO, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_VIDEO, mimes, count); } telebot_core_response_t @@ -805,87 +861,87 @@ telebot_core_send_animation(telebot_core_handler_t core_h, long long int chat_id { CHECK_ARG_NULL(animation); - int index = 0; + int count = 0; telebot_core_mime_t mimes[11]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "animation"; - mimes[index].type = (is_file ? TELEBOT_MIME_TYPE_FILE : TELEBOT_MIME_TYPE_DATA); - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", animation); - ++index; + mimes[count].name = "animation"; + mimes[count].type = is_file ? TELEBOT_MIME_TYPE_FILE : TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = animation; + count++; if (duration > 0) { - mimes[index].name = "duration"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", duration); - ++index; + mimes[count].name = "duration"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = duration; + count++; } if (width > 0) { - mimes[index].name = "width"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", width); - ++index; + mimes[count].name = "width"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = width; + count++; } if (height > 0) { - mimes[index].name = "height"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", height); - ++index; + mimes[count].name = "height"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = height; + count++; } if (thumb != NULL) { - mimes[index].name = "thumb"; - mimes[index].type = TELEBOT_MIME_TYPE_FILE; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", thumb); - ++index; + mimes[count].name = "thumb"; + mimes[count].type = TELEBOT_MIME_TYPE_FILE; + mimes[count].data.s = thumb; + count++; } if (caption != NULL) { - mimes[index].name = "caption"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", caption); - ++index; + mimes[count].name = "caption"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = caption; + count++; if (parse_mode != NULL) { - mimes[index].name = "parse_mode"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", parse_mode); - ++index; + mimes[count].name = "parse_mode"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = parse_mode; + count++; } } - mimes[index].name = "disable_notification"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (disable_notification ? "true" : "false")); - ++index; + mimes[count].name = "disable_notification"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = disable_notification ? "true" : "false"; + count++; if (reply_to_message_id > 0) { - mimes[index].name = "reply_to_message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", reply_to_message_id); - ++index; + mimes[count].name = "reply_to_message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = reply_to_message_id; + count++; } if (reply_markup != NULL) { - mimes[index].name = "reply_markup"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", reply_markup); - ++index; + mimes[count].name = "reply_markup"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = reply_markup; + count++; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_ANIMATION, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_ANIMATION, mimes, count); } telebot_core_response_t @@ -895,63 +951,63 @@ telebot_core_send_voice(telebot_core_handler_t core_h, long long int chat_id, co { CHECK_ARG_NULL(voice); - int index = 0; + int count = 0; telebot_core_mime_t mimes[8]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "voice"; - mimes[index].type = (is_file ? TELEBOT_MIME_TYPE_FILE : TELEBOT_MIME_TYPE_DATA); - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", voice); - ++index; + mimes[count].name = "voice"; + mimes[count].type = is_file ? TELEBOT_MIME_TYPE_FILE : TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = voice; + count++; if (duration > 0) { - mimes[index].name = "duration"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", duration); - ++index; + mimes[count].name = "duration"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = duration; + count++; } if (caption != NULL) { - mimes[index].name = "caption"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", caption); - ++index; + mimes[count].name = "caption"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = caption; + count++; if (parse_mode != NULL) { - mimes[index].name = "parse_mode"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", parse_mode); - ++index; + mimes[count].name = "parse_mode"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = parse_mode; + count++; } } - mimes[index].name = "disable_notification"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (disable_notification ? "true" : "false")); - ++index; + mimes[count].name = "disable_notification"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = disable_notification ? "true" : "false"; + count++; if (reply_to_message_id > 0) { - mimes[index].name = "reply_to_message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", reply_to_message_id); - ++index; + mimes[count].name = "reply_to_message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = reply_to_message_id; + count++; } if (reply_markup != NULL) { - mimes[index].name = "reply_markup"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", reply_markup); - ++index; + mimes[count].name = "reply_markup"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = reply_markup; + count++; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_VOICE, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_VOICE, mimes, count); } telebot_core_response_t @@ -961,64 +1017,65 @@ telebot_core_send_video_note(telebot_core_handler_t core_h, long long int chat_i { CHECK_ARG_NULL(video_note); - int index = 0; + int count = 0; telebot_core_mime_t mimes[8]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "video_note"; - mimes[index].type = (is_file ? TELEBOT_MIME_TYPE_FILE : TELEBOT_MIME_TYPE_DATA); - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", video_note); - ++index; + mimes[count] + .name = "video_note"; + mimes[count].type = is_file ? TELEBOT_MIME_TYPE_FILE : TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = video_note; + count++; if (duration > 0) { - mimes[index].name = "duration"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", duration); - ++index; + mimes[count].name = "duration"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = duration; + count++; } if (length > 0) { - mimes[index].name = "length"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", length); - ++index; + mimes[count].name = "length"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = length; + count++; } if (thumb != NULL) { - mimes[index].name = "thumb"; - mimes[index].type = TELEBOT_MIME_TYPE_FILE; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", thumb); - ++index; + mimes[count].name = "thumb"; + mimes[count].type = TELEBOT_MIME_TYPE_FILE; + mimes[count].data.s = thumb; + count++; } - mimes[index].name = "disable_notification"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (disable_notification ? "true" : "false")); - ++index; + mimes[count].name = "disable_notification"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = disable_notification ? "true" : "false"; + count++; if (reply_to_message_id > 0) { - mimes[index].name = "reply_to_message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", reply_to_message_id); - ++index; + mimes[count].name = "reply_to_message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = reply_to_message_id; + count++; } if (reply_markup != NULL) { - mimes[index].name = "reply_markup"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", reply_markup); - ++index; + mimes[count].name = "reply_markup"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = reply_markup; + count++; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_VIDEO_NOTE, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_VIDEO_NOTE, mimes, count); } telebot_core_response_t @@ -1026,53 +1083,53 @@ telebot_core_send_location(telebot_core_handler_t core_h, long long int chat_id, int live_period, bool disable_notification, int reply_to_message_id, const char *reply_markup) { - int index = 0; + int count = 0; telebot_core_mime_t mimes[7]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "latitude"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%f", latitude); - ++index; + mimes[count].name = "latitude"; + mimes[count].type = TELEBOT_MIME_TYPE_FLOAT; + mimes[count].data.f = latitude; + count++; - mimes[index].name = "longitude"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%f", longitude); - ++index; + mimes[count].name = "longitude"; + mimes[count].type = TELEBOT_MIME_TYPE_FLOAT; + mimes[count].data.f = longitude; + count++; if (live_period > 0) { - mimes[index].name = "live_period"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", live_period); - ++index; + mimes[count].name = "live_period"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = live_period; + count++; } - mimes[index].name = "disable_notification"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (disable_notification ? "true" : "false")); - ++index; + mimes[count].name = "disable_notification"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = disable_notification ? "true" : "false"; + count++; if (reply_to_message_id > 0) { - mimes[index].name = "reply_to_message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", reply_to_message_id); - ++index; + mimes[count].name = "reply_to_message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = reply_to_message_id; + count++; } if (reply_markup != NULL) { - mimes[index].name = "reply_markup"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", reply_markup); - ++index; + mimes[count].name = "reply_markup"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = reply_markup; + count++; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_LOCATION, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_LOCATION, mimes, count); } telebot_core_response_t @@ -1080,86 +1137,86 @@ telebot_core_edit_message_live_location(telebot_core_handler_t core_h, long long const char *inline_message_id, float latitude, float longitude, const char *reply_markup) { - int index = 0; + int count = 0; telebot_core_mime_t mimes[6]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "latitude"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%f", latitude); - ++index; + mimes[count].name = "latitude"; + mimes[count].type = TELEBOT_MIME_TYPE_FLOAT; + mimes[count].data.f = latitude; + count++; - mimes[index].name = "longitude"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%f", longitude); - ++index; + mimes[count].name = "longitude"; + mimes[count].type = TELEBOT_MIME_TYPE_FLOAT; + mimes[count].data.f = longitude; + count++; if (message_id > 0) { - mimes[index].name = "message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", message_id); - ++index; + mimes[count].name = "message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = message_id; + count++; } if (inline_message_id != NULL) { - mimes[index].name = "inline_message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", inline_message_id); - ++index; + mimes[count].name = "inline_message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = inline_message_id; + count++; } if (reply_markup != NULL) { - mimes[index].name = "reply_markup"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", reply_markup); - ++index; + mimes[count].name = "reply_markup"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = reply_markup; + count++; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_EDIT_MESSAGE_LIVE_LOCATION, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_EDIT_MESSAGE_LIVE_LOCATION, mimes, count); } telebot_core_response_t telebot_core_stop_message_live_location(telebot_core_handler_t core_h, long long int chat_id, int message_id, char *inline_message_id, const char *reply_markup) { - int index = 0; + int count = 0; telebot_core_mime_t mimes[4]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; if (message_id > 0) { - mimes[index].name = "message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", message_id); - ++index; + mimes[count].name = "message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = message_id; + count++; } if (inline_message_id != NULL) { - mimes[index].name = "inline_message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", inline_message_id); - ++index; + mimes[count].name = "inline_message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = inline_message_id; + count++; } if (reply_markup != NULL) { - mimes[index].name = "reply_markup"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", reply_markup); - ++index; + mimes[count].name = "reply_markup"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = reply_markup; + count++; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_STOP_MESSAGE_LIVE_LOCATION, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_STOP_MESSAGE_LIVE_LOCATION, mimes, count); } telebot_core_response_t @@ -1170,71 +1227,71 @@ telebot_core_send_venue(telebot_core_handler_t core_h, long long int chat_id, fl CHECK_ARG_NULL(title); CHECK_ARG_NULL(address); - int index = 0; + int count = 0; telebot_core_mime_t mimes[10]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; - - mimes[index].name = "latitude"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%f", latitude); - ++index; - - mimes[index].name = "longitude"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%f", longitude); - ++index; - - mimes[index].name = "title"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", title); - ++index; - - mimes[index].name = "address"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", address); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; + + mimes[count].name = "latitude"; + mimes[count].type = TELEBOT_MIME_TYPE_FLOAT; + mimes[count].data.f = latitude; + count++; + + mimes[count].name = "longitude"; + mimes[count].type = TELEBOT_MIME_TYPE_FLOAT; + mimes[count].data.f = longitude; + count++; + + mimes[count].name = "title"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = title; + count++; + + mimes[count].name = "address"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = address; + count++; if (foursquare_id != NULL) { - mimes[index].name = "foursquare_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", foursquare_id); - ++index; + mimes[count].name = "foursquare_id"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = foursquare_id; + count++; } if (foursquare_type != NULL) { - mimes[index].name = "foursquare_type"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", foursquare_type); - ++index; + mimes[count].name = "foursquare_type"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = foursquare_type; + count++; } - mimes[index].name = "disable_notification"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (disable_notification ? "true" : "false")); - ++index; + mimes[count].name = "disable_notification"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = disable_notification ? "true" : "false"; + count++; if (reply_to_message_id > 0) { - mimes[index].name = "reply_to_message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", reply_to_message_id); - ++index; + mimes[count].name = "reply_to_message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = reply_to_message_id; + count++; } if (reply_markup != NULL) { - mimes[index].name = "reply_markup"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", reply_markup); - ++index; + mimes[count].name = "reply_markup"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = reply_markup; + count++; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_VENUE, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_VENUE, mimes, count); } telebot_core_response_t @@ -1245,61 +1302,61 @@ telebot_core_send_contact(telebot_core_handler_t core_h, long long int chat_id, CHECK_ARG_NULL(phone_number); CHECK_ARG_NULL(first_name); - int index = 0; + int count = 0; telebot_core_mime_t mimes[8]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "phone_number"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", phone_number); - ++index; + mimes[count].name = "phone_number"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = phone_number; + count++; - mimes[index].name = "first_name"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", first_name); - ++index; + mimes[count].name = "first_name"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = first_name; + count++; if (last_name != NULL) { - mimes[index].name = "last_name"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", last_name); - ++index; + mimes[count].name = "last_name"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = last_name; + count++; } if (vcard != NULL) { - mimes[index].name = "vcard"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", vcard); - ++index; + mimes[count].name = "vcard"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = vcard; + count++; } - mimes[index].name = "disable_notification"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (disable_notification ? "true" : "false")); - ++index; + mimes[count].name = "disable_notification"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = disable_notification ? "true" : "false"; + count++; if (reply_to_message_id > 0) { - mimes[index].name = "reply_to_message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", reply_to_message_id); - ++index; + mimes[count].name = "reply_to_message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = reply_to_message_id; + count++; } if (reply_markup != NULL) { - mimes[index].name = "reply_markup"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", reply_markup); - ++index; + mimes[count].name = "reply_markup"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = reply_markup; + count++; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_CONTACT, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_CONTACT, mimes, count); } telebot_core_response_t @@ -1310,111 +1367,111 @@ telebot_core_send_poll(telebot_core_handler_t core_h, long long int chat_id, con CHECK_ARG_NULL(question); CHECK_ARG_NULL(options); - int index = 0; + int count = 0; telebot_core_mime_t mimes[11]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; - - mimes[index].name = "question"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", question); - ++index; - - mimes[index].name = "options"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", options); - ++index; - - mimes[index].name = "is_anonymous"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (is_anonymous ? "true" : "false")); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; + + mimes[count].name = "question"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = question; + count++; + + mimes[count].name = "options"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = options; + count++; + + mimes[count].name = "is_anonymous"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = is_anonymous ? "true" : "false"; + count++; if (type != NULL) { - mimes[index].name = "type"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", type); - ++index; + mimes[count].name = "type"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = type; + count++; } - mimes[index].name = "allows_multiple_answers"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (allows_multiple_answers ? "true" : "false")); - ++index; + mimes[count].name = "allows_multiple_answers"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = allows_multiple_answers ? "true" : "false"; + count++; if (correct_option_id >= 0) { - mimes[index].name = "correct_option_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", correct_option_id); - ++index; + mimes[count].name = "correct_option_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = correct_option_id; + count++; } - mimes[index].name = "is_closed"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (is_closed ? "true" : "false")); - ++index; + mimes[count].name = "is_closed"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = is_closed ? "true" : "false"; + count++; - mimes[index].name = "disable_notification"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (disable_notification ? "true" : "false")); - ++index; + mimes[count].name = "disable_notification"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = disable_notification ? "true" : "false"; + count++; if (reply_to_message_id > 0) { - mimes[index].name = "reply_to_message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", reply_to_message_id); - ++index; + mimes[count].name = "reply_to_message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = reply_to_message_id; + count++; } if (reply_markup != NULL) { - mimes[index].name = "reply_markup"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", reply_markup); - ++index; + mimes[count].name = "reply_markup"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = reply_markup; + count++; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_POLL, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_POLL, mimes, count); } telebot_core_response_t telebot_core_send_dice(telebot_core_handler_t core_h, long long int chat_id, bool disable_notification, int reply_to_message_id, const char *reply_markup) { - int index = 0; + int count = 0; telebot_core_mime_t mimes[4]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "disable_notification"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (disable_notification ? "true" : "false")); - ++index; + mimes[count].name = "disable_notification"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = disable_notification ? "true" : "false"; + count++; if (reply_to_message_id > 0) { - mimes[index].name = "reply_to_message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", reply_to_message_id); - ++index; + mimes[count].name = "reply_to_message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = reply_to_message_id; + count++; } if (reply_markup != NULL) { - mimes[index].name = "reply_markup"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", reply_markup); - ++index; + mimes[count].name = "reply_markup"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = reply_markup; + count++; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_DICE, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_DICE, mimes, count); } telebot_core_response_t @@ -1422,19 +1479,19 @@ telebot_core_send_chat_action(telebot_core_handler_t core_h, long long int chat_ { CHECK_ARG_NULL(action); - int index = 0; + int count = 0; telebot_core_mime_t mimes[2]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "action"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", action); - ++index; + mimes[count].name = "action"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = action; + count++; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_CHAT_ACTION, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SEND_CHAT_ACTION, mimes, count); } telebot_core_response_t @@ -1442,24 +1499,24 @@ telebot_core_get_user_profile_photos(telebot_core_handler_t core_h, int user_id, { CHECK_ARG_CONDITION((user_id <= 0), "Invalid value of user_id"); - int index = 0; + int count = 0; telebot_core_mime_t mimes[3]; // number of arguments - mimes[index].name = "user_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", user_id); - ++index; + mimes[count].name = "user_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = user_id; + count++; - mimes[index].name = "offset"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", offset); - ++index; + mimes[count].name = "offset"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = offset; + count++; - mimes[index].name = "limit"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", limit); - ++index; + mimes[count].name = "limit"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = limit; + count++; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_USER_PHOTOS, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_USER_PHOTOS, mimes, count); } telebot_core_response_t @@ -1467,14 +1524,14 @@ telebot_core_get_file(telebot_core_handler_t core_h, const char *file_id) { CHECK_ARG_NULL(file_id); - int index = 0; + int count = 0; telebot_core_mime_t mimes[1]; // number of arguments - mimes[index].name = "file_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", file_id); - ++index; + mimes[count].name = "file_id"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = file_id; + count++; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_FILE, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_FILE, mimes, count); } static size_t @@ -1550,27 +1607,27 @@ telebot_core_kick_chat_member(telebot_core_handler_t core_h, long long int chat_ { CHECK_ARG_CONDITION((user_id <= 0), "Valid user_id is required"); - int index = 0; + int count = 0; telebot_core_mime_t mimes[3]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "user_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", user_id); - ++index; + mimes[count].name = "user_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = user_id; + count++; if (until_date > 0) { - mimes[index].name = "until_date"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%ld", until_date); - ++index; + mimes[count].name = "until_date"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_INT; + mimes[count].data.ld = until_date; + count++; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_KICK_CHAT_MEMBER, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_KICK_CHAT_MEMBER, mimes, count); } telebot_core_response_t @@ -1578,19 +1635,19 @@ telebot_core_unban_chat_member(telebot_core_handler_t core_h, long long int chat { CHECK_ARG_CONDITION((user_id <= 0), "Valid user_id is required"); - int index = 0; + int count = 0; telebot_core_mime_t mimes[2]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "user_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", user_id); - ++index; + mimes[count].name = "user_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = user_id; + count++; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_UNBAN_CHAT_MEMBER, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_UNBAN_CHAT_MEMBER, mimes, count); } telebot_core_response_t @@ -1601,67 +1658,67 @@ telebot_core_restrict_chat_member(telebot_core_handler_t core_h, long long int c { CHECK_ARG_CONDITION((user_id <= 0), "Valid user_id is required"); - int index = 0; + int count = 0; telebot_core_mime_t mimes[11]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "user_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", user_id); - ++index; + mimes[count].name = "user_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = user_id; + count++; if (until_date > 0) { - mimes[index].name = "until_date"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%ld", until_date); - ++index; + mimes[count].name = "until_date"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_INT; + mimes[count].data.ld = until_date; + count++; } - mimes[index].name = "can_send_messages"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_send_messages ? "true" : "false")); - ++index; + mimes[count].name = "can_send_messages"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_send_messages ? "true" : "false"; + count++; - mimes[index].name = "can_send_media_messages"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_send_media_messages ? "true" : "false")); - ++index; + mimes[count].name = "can_send_media_messages"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_send_media_messages ? "true" : "false"; + count++; - mimes[index].name = "can_send_polls"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_send_polls ? "true" : "false")); - ++index; + mimes[count].name = "can_send_polls"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_send_polls ? "true" : "false"; + count++; - mimes[index].name = "can_send_other_messages"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_send_other_messages ? "true" : "false")); - ++index; + mimes[count].name = "can_send_other_messages"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_send_other_messages ? "true" : "false"; + count++; - mimes[index].name = "can_add_web_page_previews"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_add_web_page_previews ? "true" : "false")); - ++index; + mimes[count].name = "can_add_web_page_previews"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_add_web_page_previews ? "true" : "false"; + count++; - mimes[index].name = "can_change_info"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_change_info ? "true" : "false")); - ++index; + mimes[count].name = "can_change_info"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_change_info ? "true" : "false"; + count++; - mimes[index].name = "can_invite_users"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_invite_users ? "true" : "false")); - ++index; + mimes[count].name = "can_invite_users"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_invite_users ? "true" : "false"; + count++; - mimes[index].name = "can_pin_messages"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_invite_users ? "true" : "false")); - ++index; + mimes[count].name = "can_pin_messages"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_invite_users ? "true" : "false"; + count++; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_RESTRICT_CHAT_MEMBER, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_RESTRICT_CHAT_MEMBER, mimes, count); } telebot_core_response_t @@ -1672,59 +1729,59 @@ telebot_core_promote_chat_member(telebot_core_handler_t core_h, long long int ch { CHECK_ARG_CONDITION((user_id <= 0), "Valid user_id is required"); - int index = 0; + int count = 0; telebot_core_mime_t mimes[10]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; - - mimes[index].name = "user_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", user_id); - ++index; - - mimes[index].name = "can_change_info"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_change_info ? "true" : "false")); - ++index; - - mimes[index].name = "can_post_messages"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_post_messages ? "true" : "false")); - ++index; - - mimes[index].name = "can_edit_messages"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_edit_messages ? "true" : "false")); - ++index; - - mimes[index].name = "can_delete_messages"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_delete_messages ? "true" : "false")); - ++index; - - mimes[index].name = "can_invite_users"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_invite_users ? "true" : "false")); - ++index; - - mimes[index].name = "can_restrict_members"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_restrict_members ? "true" : "false")); - ++index; - - mimes[index].name = "can_pin_messages"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_pin_messages ? "true" : "false")); - ++index; - - mimes[index].name = "can_promote_members"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_promote_members ? "true" : "false")); - ++index; - - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_PROMOTE_CHAT_MEMBER, mimes, index); + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; + + mimes[count].name = "user_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = user_id; + count++; + + mimes[count].name = "can_change_info"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_change_info ? "true" : "false"; + count++; + + mimes[count].name = "can_post_messages"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_post_messages ? "true" : "false"; + count++; + + mimes[count].name = "can_edit_messages"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_edit_messages ? "true" : "false"; + count++; + + mimes[count].name = "can_delete_messages"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_delete_messages ? "true" : "false"; + count++; + + mimes[count].name = "can_invite_users"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_invite_users ? "true" : "false"; + count++; + + mimes[count].name = "can_restrict_members"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_restrict_members ? "true" : "false"; + count++; + + mimes[count].name = "can_pin_messages"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_pin_messages ? "true" : "false"; + count++; + + mimes[count].name = "can_promote_members"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_promote_members ? "true" : "false"; + count++; + + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_PROMOTE_CHAT_MEMBER, mimes, count); } telebot_core_response_t @@ -1733,24 +1790,24 @@ telebot_core_set_chat_admin_custom_title(telebot_core_handler_t core_h, long lon { CHECK_ARG_CONDITION((user_id <= 0), "Valid user_id is required"); - int index = 0; + int count = 0; telebot_core_mime_t mimes[3]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "user_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", user_id); - ++index; + mimes[count].name = "user_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = user_id; + count++; - mimes[index].name = "custom_title"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", custom_title); - ++index; + mimes[count].name = "custom_title"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = custom_title; + count++; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_ADMIN_TITLE, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_ADMIN_TITLE, mimes, count); } telebot_core_response_t @@ -1759,67 +1816,68 @@ telebot_core_set_chat_permissions(telebot_core_handler_t core_h, long long int c bool can_add_web_page_previews, bool can_change_info, bool can_invite_users, bool can_pin_messages) { - int index = 0; + int count = 0; telebot_core_mime_t mimes[9]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; - - mimes[index].name = "can_send_messages"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_send_messages ? "true" : "false")); - ++index; - - mimes[index].name = "can_send_media_messages"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_send_media_messages ? "true" : "false")); - ++index; - - mimes[index].name = "can_send_polls"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_send_polls ? "true" : "false")); - ++index; - - mimes[index].name = "can_send_other_messages"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_send_other_messages ? "true" : "false")); - ++index; - - mimes[index].name = "can_add_web_page_previews"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_add_web_page_previews ? "true" : "false")); - ++index; - - mimes[index].name = "can_change_info"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_change_info ? "true" : "false")); - ++index; - - mimes[index].name = "can_invite_users"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_invite_users ? "true" : "false")); - ++index; - - mimes[index].name = "can_pin_messages"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (can_invite_users ? "true" : "false")); - ++index; - - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_PERMISSIONS, mimes, index); + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; + + mimes[count].name = "can_send_messages"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_send_messages ? "true" : "false"; + count++; + + mimes[count].name = "can_send_media_messages"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_send_media_messages ? "true" : "false"; + count++; + + mimes[count].name = "can_send_polls"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_send_polls ? "true" : "false"; + count++; + + mimes[count].name = "can_send_other_messages"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_send_other_messages ? "true" : "false"; + count++; + + mimes[count].name = "can_add_web_page_previews"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_add_web_page_previews ? "true" : "false"; + count++; + + mimes[count].name = "can_change_info"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_change_info ? "true" : "false"; + count++; + + mimes[count].name = "can_invite_users"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_invite_users ? "true" : "false"; + count++; + + mimes[count] + .name = "can_pin_messages"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = can_invite_users ? "true" : "false"; + count++; + + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_PERMISSIONS, mimes, count); } telebot_core_response_t telebot_core_export_chat_invite_link(telebot_core_handler_t core_h, long long int chat_id) { - int index = 0; + int count = 0; telebot_core_mime_t mimes[1]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_EXPORT_CHAT_INVITE_LINK, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_EXPORT_CHAT_INVITE_LINK, mimes, count); } telebot_core_response_t @@ -1827,19 +1885,19 @@ telebot_core_set_chat_photo(telebot_core_handler_t core_h, long long int chat_id { CHECK_ARG_NULL(photo); - int index = 0; + int count = 0; telebot_core_mime_t mimes[2]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "photo"; - mimes[index].type = TELEBOT_MIME_TYPE_FILE; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", photo); - ++index; + mimes[count].name = "photo"; + mimes[count].type = TELEBOT_MIME_TYPE_FILE; + mimes[count].data.s = photo; + count++; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_PHOTO, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_PHOTO, mimes, count); } telebot_core_response_t @@ -1847,14 +1905,14 @@ telebot_core_delete_chat_photo(telebot_core_handler_t core_h, long long int chat { CHECK_ARG_CONDITION((chat_id <= 0), "Invalid chat id"); - int index = 0; + int count = 0; telebot_core_mime_t mimes[1]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_DELETE_CHAT_PHOTO, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_DELETE_CHAT_PHOTO, mimes, count); } telebot_core_response_t @@ -1863,19 +1921,19 @@ telebot_core_set_chat_title(telebot_core_handler_t core_h, long long int chat_id CHECK_ARG_CONDITION((chat_id <= 0), "Valid chat id is required"); CHECK_ARG_CONDITION((title == NULL) || (strlen(title) > 255), "Valid title is required"); - int index = 0; + int count = 0; telebot_core_mime_t mimes[2]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "title"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", title); - ++index; + mimes[count].name = "title"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = title; + count++; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_TITLE, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_TITLE, mimes, count); } telebot_core_response_t @@ -1885,19 +1943,19 @@ telebot_core_set_chat_description(telebot_core_handler_t core_h, long long int c CHECK_ARG_CONDITION((description == NULL) || (strlen(description) > 255), "Valid description is required"); - int index = 0; + int count = 0; telebot_core_mime_t mimes[2]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "description"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", description); - ++index; + mimes[count].name = "description"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = description; + count++; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_DESCRIPTION, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_DESCRIPTION, mimes, count); } telebot_core_response_t @@ -1907,24 +1965,24 @@ telebot_core_pin_chat_message(telebot_core_handler_t core_h, long long int chat_ CHECK_ARG_CONDITION((chat_id <= 0), "Valid chat id is required"); CHECK_ARG_CONDITION((message_id <= 0), "Valid message_id is required"); - int index = 0; + int count = 0; telebot_core_mime_t mimes[3]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", message_id); - ++index; + mimes[count].name = "message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = message_id; + count++; - mimes[index].name = "message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (disable_notification ? "True" : "False")); - ++index; + mimes[count].name = "message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = disable_notification ? "True" : "False"; + count++; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_PIN_CHAT_MESSAGE, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_PIN_CHAT_MESSAGE, mimes, count); } telebot_core_response_t @@ -1932,14 +1990,14 @@ telebot_core_unpin_chat_message(telebot_core_handler_t core_h, long long int cha { CHECK_ARG_CONDITION((chat_id <= 0), "Valid chat id is required"); - int index = 0; + int count = 0; telebot_core_mime_t mimes[1]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_UNPIN_CHAT_MESSAGE, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_UNPIN_CHAT_MESSAGE, mimes, count); } telebot_core_response_t @@ -1947,14 +2005,14 @@ telebot_core_leave_chat(telebot_core_handler_t core_h, long long int chat_id) { CHECK_ARG_CONDITION((chat_id <= 0), "Valid chat id is required"); - int index = 0; + int count = 0; telebot_core_mime_t mimes[1]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_LEAVE_CHAT, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_LEAVE_CHAT, mimes, count); } telebot_core_response_t @@ -1962,14 +2020,14 @@ telebot_core_get_chat(telebot_core_handler_t core_h, long long int chat_id) { CHECK_ARG_CONDITION((chat_id <= 0), "Valid chat id is required"); - int index = 0; + int count = 0; telebot_core_mime_t mimes[1]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_CHAT, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_CHAT, mimes, count); } telebot_core_response_t @@ -1977,14 +2035,14 @@ telebot_core_get_chat_admins(telebot_core_handler_t core_h, long long int chat_i { CHECK_ARG_CONDITION((chat_id <= 0), "Valid chat id is required"); - int index = 0; + int count = 0; telebot_core_mime_t mimes[1]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_CHAT_ADMINS, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_CHAT_ADMINS, mimes, count); } telebot_core_response_t @@ -1992,14 +2050,14 @@ telebot_core_get_chat_members_count(telebot_core_handler_t core_h, long long int { CHECK_ARG_CONDITION((chat_id <= 0), "Valid chat id is required"); - int index = 0; + int count = 0; telebot_core_mime_t mimes[1]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_CHAT_MEMBERS_COUNT, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_CHAT_MEMBERS_COUNT, mimes, count); } telebot_core_response_t @@ -2008,19 +2066,19 @@ telebot_core_get_chat_member(telebot_core_handler_t core_h, long long int chat_i CHECK_ARG_CONDITION((chat_id <= 0), "Valid chat id is required"); CHECK_ARG_CONDITION((user_id <= 0), "Valid user_id is required"); - int index = 0; + int count = 0; telebot_core_mime_t mimes[2]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "user_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", user_id); - ++index; + mimes[count].name = "user_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = user_id; + count++; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_CHAT_MEMBER, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_GET_CHAT_MEMBER, mimes, count); } telebot_core_response_t @@ -2029,19 +2087,19 @@ telebot_core_set_chat_sticker_set(telebot_core_handler_t core_h, long long int c CHECK_ARG_CONDITION((chat_id <= 0), "Valid chat id is required"); CHECK_ARG_NULL(sticker_set_name); - int index = 0; + int count = 0; telebot_core_mime_t mimes[2]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "sticker_set_name"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", sticker_set_name); - ++index; + mimes[count].name = "sticker_set_name"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = sticker_set_name; + count++; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_STICKER_SET, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_CHAT_STICKER_SET, mimes, count); } telebot_core_response_t @@ -2049,14 +2107,14 @@ telebot_core_delete_chat_sticker_set(telebot_core_handler_t core_h, long long in { CHECK_ARG_CONDITION((chat_id <= 0), "Valid chat id is required"); - int index = 0; + int count = 0; telebot_core_mime_t mimes[1]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_DEL_CHAT_STICKER_SET, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_DEL_CHAT_STICKER_SET, mimes, count); } telebot_core_response_t @@ -2065,43 +2123,43 @@ telebot_core_answer_callback_query(telebot_core_handler_t core_h, const char *ca { CHECK_ARG_NULL(callback_query_id); - int index = 0; + int count = 0; telebot_core_mime_t mimes[5]; // number of arguments - mimes[index].name = "callback_query_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", callback_query_id); - ++index; + mimes[count].name = "callback_query_id"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = callback_query_id; + count++; if (text != NULL) { - mimes[index].name = "text"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", text); - ++index; + mimes[count].name = "text"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = text; + count++; } - mimes[index].name = "show_alert"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (show_alert ? "True" : "False")); - ++index; + mimes[count].name = "show_alert"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = show_alert ? "True" : "False"; + count++; if (url != NULL) { - mimes[index].name = "url"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", url); - ++index; + mimes[count].name = "url"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = url; + count++; } if (cache_time > 0) { - mimes[index].name = "cache_time"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", cache_time); - ++index; + mimes[count].name = "cache_time"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = cache_time; + count++; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_ANSWER_CALLBACK_QUERY, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_ANSWER_CALLBACK_QUERY, mimes, count); } telebot_core_response_t @@ -2109,14 +2167,14 @@ telebot_core_set_my_commands(telebot_core_handler_t core_h, const char *commands { CHECK_ARG_NULL(commands); - int index = 0; + int count = 0; telebot_core_mime_t mimes[1]; // number of arguments - mimes[index].name = "commands"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", commands); - ++index; + mimes[count].name = "commands"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = commands; + count++; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_MY_COMMANDS, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_MY_COMMANDS, mimes, count); } telebot_core_response_t @@ -2136,56 +2194,56 @@ telebot_core_edit_message_text(telebot_core_handler_t core_h, long long int chat return telebot_core_get_error_response(TELEBOT_ERROR_INVALID_PARAMETER); } - int index = 0; + int count = 0; telebot_core_mime_t mimes[7]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; if (message_id > 0) { - mimes[index].name = "message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", message_id); - ++index; + mimes[count].name = "message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = message_id; + count++; } if (inline_message_id != NULL) { - mimes[index].name = "inline_message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", inline_message_id); - ++index; + mimes[count].name = "inline_message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = inline_message_id; + count++; } - mimes[index].name = "text"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", text); - ++index; + mimes[count].name = "text"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = text; + count++; if (parse_mode != NULL) { - mimes[index].name = "parse_mode"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", parse_mode); - ++index; + mimes[count].name = "parse_mode"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = parse_mode; + count++; } - mimes[index].name = "disable_web_page_preview"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", (disable_web_page_preview ? "true" : "false")); - ++index; + mimes[count].name = "disable_web_page_preview"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = disable_web_page_preview ? "true" : "false"; + count++; if (reply_markup != NULL) { - mimes[index].name = "reply_markup"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", reply_markup); - ++index; + mimes[count].name = "reply_markup"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = reply_markup; + count++; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_EDIT_MESSAGE_TEXT, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_EDIT_MESSAGE_TEXT, mimes, count); } telebot_core_response_t @@ -2199,51 +2257,51 @@ telebot_core_edit_message_caption(telebot_core_handler_t core_h, long long int c return telebot_core_get_error_response(TELEBOT_ERROR_INVALID_PARAMETER); } - int index = 0; + int count = 0; telebot_core_mime_t mimes[6]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; if (message_id > 0) { - mimes[index].name = "message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", message_id); - ++index; + mimes[count].name = "message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = message_id; + count++; } if (inline_message_id != NULL) { - mimes[index].name = "inline_message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", inline_message_id); - ++index; + mimes[count].name = "inline_message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = inline_message_id; + count++; } - mimes[index].name = "caption"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", caption); - ++index; + mimes[count].name = "caption"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = caption; + count++; if (parse_mode != NULL) { - mimes[index].name = "parse_mode"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", parse_mode); - ++index; + mimes[count].name = "parse_mode"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = parse_mode; + count++; } if (reply_markup != NULL) { - mimes[index].name = "reply_markup"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", reply_markup); - ++index; + mimes[count].name = "reply_markup"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = reply_markup; + count++; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_EDIT_MESSAGE_CAPTION, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_EDIT_MESSAGE_CAPTION, mimes, count); } telebot_core_response_t @@ -2256,38 +2314,38 @@ telebot_core_edit_message_reply_markup(telebot_core_handler_t core_h, long long return telebot_core_get_error_response(TELEBOT_ERROR_INVALID_PARAMETER); } - int index = 0; + int count = 0; telebot_core_mime_t mimes[4]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; if (message_id > 0) { - mimes[index].name = "message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", message_id); - ++index; + mimes[count].name = "message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = message_id; + count++; } if (inline_message_id != NULL) { - mimes[index].name = "inline_message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", inline_message_id); - ++index; + mimes[count].name = "inline_message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = inline_message_id; + count++; } if (reply_markup != NULL) { - mimes[index].name = "reply_markup"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", reply_markup); - ++index; + mimes[count].name = "reply_markup"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = reply_markup; + count++; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_EDIT_MESSAGE_REPLY_MARKUP, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_EDIT_MESSAGE_REPLY_MARKUP, mimes, count); } telebot_core_response_t @@ -2296,27 +2354,27 @@ telebot_core_stop_poll(telebot_core_handler_t core_h, long long int chat_id, int CHECK_ARG_CONDITION((chat_id <= 0) || (message_id <= 0), "Valid chat_id and message_id required"); - int index = 0; + int count = 0; telebot_core_mime_t mimes[3]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", message_id); - ++index; + mimes[count].name = "message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = message_id; + count++; if (reply_markup != NULL) { - mimes[index].name = "reply_markup"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", reply_markup); - ++index; + mimes[count].name = "reply_markup"; + mimes[count].type = TELEBOT_MIME_TYPE_STRING; + mimes[count].data.s = reply_markup; + count++; } - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_STOP_POLL, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_STOP_POLL, mimes, count); } telebot_core_response_t @@ -2325,17 +2383,17 @@ telebot_core_delete_message(telebot_core_handler_t core_h, long long int chat_id CHECK_ARG_CONDITION((chat_id <= 0) || (message_id <= 0), "Valid chat_id and message_id required"); - int index = 0; + int count = 0; telebot_core_mime_t mimes[2]; // number of arguments - mimes[index].name = "chat_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%lld", chat_id); - ++index; + mimes[count].name = "chat_id"; + mimes[count].type = TELEBOT_MIME_TYPE_LONG_LONG_INT; + mimes[count].data.lld = chat_id; + count++; - mimes[index].name = "message_id"; - mimes[index].type = TELEBOT_MIME_TYPE_DATA; - snprintf(mimes[index].data, sizeof(mimes[index].data), "%d", message_id); - ++index; + mimes[count].name = "message_id"; + mimes[count].type = TELEBOT_MIME_TYPE_INT; + mimes[count].data.d = message_id; + count++; - return telebot_core_curl_perform(core_h, TELEBOT_METHOD_DELETE_MESSAGE, mimes, index); + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_DELETE_MESSAGE, mimes, count); } From 2f579638e360ea402e84a84393a654f90b58ddd1 Mon Sep 17 00:00:00 2001 From: Elmurod Talipov Date: Sun, 20 Nov 2022 23:36:43 +0900 Subject: [PATCH 3/5] [Bot API 6.3] update user object Signed-off-by: Elmurod Talipov --- include/telebot-types.h | 8 +++++++- src/telebot-parser.c | 10 +++++++++- test/echobot.c | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/include/telebot-types.h b/include/telebot-types.h index 6001d2d..9f244ae 100644 --- a/include/telebot-types.h +++ b/include/telebot-types.h @@ -61,7 +61,7 @@ typedef enum telebot_update_type { */ typedef struct telebot_user { /** Unique identifier for this user or bot. */ - int id; + long long int id; /** True, if this user is bot. */ bool is_bot; @@ -78,6 +78,12 @@ typedef struct telebot_user { /** Optional. IETF language tag of the user's language. */ char *language_code; + /** Optional. True, if this user is a Telegram Premium user. */ + bool is_premium; + + /** Optional. True, if this user added the bot to the attachment menu. */ + bool added_to_attachment_menu; + /** Optional. Optional. True, if the bot can be invited to groups. Returned only in getMe. */ bool can_join_groups; diff --git a/src/telebot-parser.c b/src/telebot-parser.c index 54a6015..f971e53 100644 --- a/src/telebot-parser.c +++ b/src/telebot-parser.c @@ -218,7 +218,7 @@ telebot_error_e telebot_parser_get_user(struct json_object *obj, telebot_user_t ERR("Object is not json user type, id not found"); return TELEBOT_ERROR_OPERATION_FAILED; } - user->id = json_object_get_int(id); + user->id = json_object_get_int64(id); struct json_object *is_bot = NULL; if (!json_object_object_get_ex(obj, "is_bot", &is_bot)) @@ -248,6 +248,14 @@ telebot_error_e telebot_parser_get_user(struct json_object *obj, telebot_user_t if (json_object_object_get_ex(obj, "language_code", &language_code)) user->language_code = TELEBOT_SAFE_STRDUP(json_object_get_string(language_code)); + struct json_object *is_premium = NULL; + if (json_object_object_get_ex(obj, "is_premium", &is_premium)) + user->is_premium = json_object_get_boolean(is_premium); + + struct json_object *added_to_attachment_menu = NULL; + if (json_object_object_get_ex(obj, "added_to_attachment_menu", &added_to_attachment_menu)) + user->added_to_attachment_menu = json_object_get_boolean(added_to_attachment_menu); + struct json_object *can_join_groups = NULL; if (json_object_object_get_ex(obj, "can_join_groups", &can_join_groups)) user->can_join_groups = json_object_get_boolean(can_join_groups); diff --git a/test/echobot.c b/test/echobot.c index fbb2fef..e8cd499 100644 --- a/test/echobot.c +++ b/test/echobot.c @@ -43,7 +43,7 @@ int main(int argc, char *argv[]) return -1; } - printf("ID: %d\n", me.id); + printf("ID: %lld\n", me.id); printf("First Name: %s\n", me.first_name); printf("User Name: %s\n", me.username); From aa7b622208ffdaf2d3f8dda40d92b5a71e9453a9 Mon Sep 17 00:00:00 2001 From: Elmurod Talipov Date: Mon, 21 Nov 2022 01:03:43 +0900 Subject: [PATCH 4/5] [Bot API 6.3] update chat object Signed-off-by: Elmurod Talipov --- include/telebot-parser.h | 3 ++ include/telebot-types.h | 89 ++++++++++++++++++++++++++++++- src/telebot-parser.c | 110 +++++++++++++++++++++++++++++++++++++-- src/telebot.c | 43 +++++++++++---- 4 files changed, 229 insertions(+), 16 deletions(-) diff --git a/include/telebot-parser.h b/include/telebot-parser.h index a99b891..7703fb0 100644 --- a/include/telebot-parser.h +++ b/include/telebot-parser.h @@ -114,4 +114,7 @@ telebot_error_e telebot_parser_get_array_bot_command(struct json_object *obj, te /** Prase response paramters object */ telebot_error_e telebot_parser_get_response_parameters(struct json_object *obj, telebot_response_paramters_t *resp_param); +/** Prase chat location object object */ +telebot_error_e telebot_parser_get_chat_location(struct json_object *obj, telebot_chat_location_t *chat_location); + #endif /* __TELEBOT_PARSER_H__ */ diff --git a/include/telebot-types.h b/include/telebot-types.h index 9f244ae..08303fe 100644 --- a/include/telebot-types.h +++ b/include/telebot-types.h @@ -117,9 +117,53 @@ typedef struct telebot_chat { /** Optional. Last name of the other party in a private chat. */ char *last_name; + /** Optional. True, if the supergroup chat is a forum (has topics enabled). */ + bool is_forum; + /** Optional. Chat photo. Returned only in getChat. */ struct telebot_chat_photo *photo; + /** Optional. If non-empty, the list of all active chat usernames; + * for private chats, supergroups and channels. Returned only in getChat. + */ + char **active_usernames; + int count_active_usernames; + + /** + * Optional. Custom emoji identifier of emoji status of the other party in + * a private chat. Returned only in getChat. + */ + char *emoji_status_custom_emoji_id; + + /** Optional. Bio of the other party in a private chat. Returned only in getChat. */ + char *bio; + + /** + * Optional. True, if privacy settings of the other party in the private + * chat allows to use tg://user?id= links only in chats with the user. + * Returned only in getChat. + */ + bool has_private_forwards; + + /** + * Optional. True, if the privacy settings of the other party restrict + * sending voice and video note messages in the private chat. + * Returned only in getChat. + */ + bool has_restricted_voice_and_video_messages; + + /** + * Optional. True, if users need to join the supergroup before they can + * send messages. Returned only in getChat. + */ + bool join_to_send_messages; + + /** + * Optional. True, if all users directly joining the supergroup need + * to be approved by supergroup administrators. Returned only in getChat. + */ + bool join_by_request; + /** * Optional. Desription, for supergroups and channel chats. * Returned only in getChat. @@ -135,16 +179,30 @@ typedef struct telebot_chat { /** Optional. Pinned message, for supergroups. Returned only in getChat. */ struct telebot_message *pinned_message; - /** Optional. Default chat member permissions, for groups and supergroups. + /** + * Optional. Default chat member permissions, for groups and supergroups. * Returned only in getChat. */ struct telebot_chat_permissions *permissions; - /** Optional. For supergroups, the minimum allowed delay between consecutive + /** + * Optional. For supergroups, the minimum allowed delay between consecutive * messages sent by each unpriviledged user. Returned only in getChat. */ int slow_mode_delay; + /** + * Optional. The time after which all messages sent to the chat will be + * automatically deleted; in seconds. Returned only in getChat. + */ + int message_auto_delete_time; + + /** + * Optional. True, if messages from the chat can't be forwarded to other chats. + * Returned only in getChat. + */ + bool has_protected_content; + /** * Optional. For supergroups, name of group sticker set. * Returned only in getChat. @@ -157,6 +215,18 @@ typedef struct telebot_chat { */ bool can_set_sticker_set; + /** + * Optional. Unique identifier for the linked chat, i.e. the discussion + * group identifier for a channel and vice versa; for supergroups and + * channel chats. Returned only in getChat. + */ + long long int linked_chat_id; + + /** + * Optional. For supergroups, the location to which the supergroup is connected. + * Returned only in getChat. + */ + struct telebot_chat_location *location; } telebot_chat_t; @@ -1296,6 +1366,21 @@ typedef struct telebot_webhook_info { } telebot_webhook_info_t; +/** + * @brief Thi object represetns information about the current status of a webhook. + */ +typedef struct telebot_chat_location { + /** + * The location to which the supergroup is connected. Can't be a live location. + */ + struct telebot_location *location; + + /** + * Location address; 1-64 characters, as defined by the chat owner + */ + char *address; +} telebot_chat_location_t; + /** * @brief This is opaque object to represent a telebot handler. */ diff --git a/src/telebot-parser.c b/src/telebot-parser.c index f971e53..9cf433d 100644 --- a/src/telebot-parser.c +++ b/src/telebot-parser.c @@ -335,6 +335,10 @@ telebot_error_e telebot_parser_get_chat(struct json_object *obj, telebot_chat_t if (json_object_object_get_ex(obj, "last_name", &last_name)) chat->last_name = TELEBOT_SAFE_STRDUP(json_object_get_string(last_name)); + struct json_object *is_forum = NULL; + if (json_object_object_get_ex(obj, "is_forum", &is_forum)) + chat->is_forum = json_object_get_boolean(is_forum); + struct json_object *chat_photo = NULL; if (json_object_object_get_ex(obj, "photo", &chat_photo)) { @@ -346,6 +350,46 @@ telebot_error_e telebot_parser_get_chat(struct json_object *obj, telebot_chat_t } } + struct json_object *array_active_usernames = NULL; + if (json_object_object_get_ex(obj, "active_usernames", &array_active_usernames)) + { + int count_active_usernames = json_object_array_length(array_active_usernames); + if ((chat->count_active_usernames > 0) && + (chat->active_usernames = calloc(count_active_usernames, sizeof(char *)))) + { + chat->count_active_usernames = count_active_usernames; + for (int index = 0; index < count_active_usernames; index++) + { + struct json_object *item = json_object_array_get_idx(array_active_usernames, index); + chat->active_usernames[index] = TELEBOT_SAFE_STRDUP(json_object_get_string(item)); + } + } + } + + struct json_object *emoji_status_custom_emoji_id = NULL; + if (json_object_object_get_ex(obj, "emoji_status_custom_emoji_id", &emoji_status_custom_emoji_id)) + chat->emoji_status_custom_emoji_id = TELEBOT_SAFE_STRDUP(json_object_get_string(emoji_status_custom_emoji_id)); + + struct json_object *bio = NULL; + if (json_object_object_get_ex(obj, "bio", &bio)) + chat->bio = TELEBOT_SAFE_STRDUP(json_object_get_string(bio)); + + struct json_object *has_private_forwards = NULL; + if (json_object_object_get_ex(obj, "has_private_forwards", &has_private_forwards)) + chat->has_private_forwards = json_object_get_boolean(has_private_forwards); + + struct json_object *has_restricted_voice_and_video_messages = NULL; + if (json_object_object_get_ex(obj, "has_restricted_voice_and_video_messages", &has_restricted_voice_and_video_messages)) + chat->has_restricted_voice_and_video_messages = json_object_get_boolean(has_restricted_voice_and_video_messages); + + struct json_object *join_to_send_messages = NULL; + if (json_object_object_get_ex(obj, "join_to_send_messages", &join_to_send_messages)) + chat->join_to_send_messages = json_object_get_boolean(join_to_send_messages); + + struct json_object *join_by_request = NULL; + if (json_object_object_get_ex(obj, "join_by_request", &join_by_request)) + chat->join_by_request = json_object_get_boolean(join_by_request); + struct json_object *description = NULL; if (json_object_object_get_ex(obj, "description", &description)) chat->description = TELEBOT_SAFE_STRDUP(json_object_get_string(description)); @@ -378,9 +422,15 @@ telebot_error_e telebot_parser_get_chat(struct json_object *obj, telebot_chat_t struct json_object *slow_mode_delay = NULL; if (json_object_object_get_ex(obj, "slow_mode_delay", &slow_mode_delay)) - { chat->slow_mode_delay = json_object_get_int(slow_mode_delay); - } + + struct json_object *has_protected_content = NULL; + if (json_object_object_get_ex(obj, "has_protected_content", &has_protected_content)) + chat->has_protected_content = json_object_get_boolean(has_protected_content); + + struct json_object *message_auto_delete_time = NULL; + if (json_object_object_get_ex(obj, "message_auto_delete_time", &message_auto_delete_time)) + chat->message_auto_delete_time = json_object_get_int(message_auto_delete_time); struct json_object *sticker_set_name = NULL; if (json_object_object_get_ex(obj, "sticker_set_name", &sticker_set_name)) @@ -390,6 +440,21 @@ telebot_error_e telebot_parser_get_chat(struct json_object *obj, telebot_chat_t if (json_object_object_get_ex(obj, "can_set_sticker_set", &can_set_sticker_set)) chat->can_set_sticker_set = json_object_get_boolean(can_set_sticker_set); + struct json_object *linked_chat_id = NULL; + if (json_object_object_get_ex(obj, "linked_chat_id", &linked_chat_id)) + chat->linked_chat_id = json_object_get_int64(linked_chat_id); + + struct json_object *location = NULL; + if (json_object_object_get_ex(obj, "location", &location)) + { + chat->location = calloc(1, sizeof(telebot_chat_location_t)); + if (telebot_parser_get_chat_location(location, chat->location) != TELEBOT_ERROR_NONE) + { + ERR("Failed to get chat location from chat object"); + TELEBOT_SAFE_FREE(chat->location); + } + } + return TELEBOT_ERROR_NONE; } @@ -581,7 +646,7 @@ telebot_error_e telebot_parser_get_message(struct json_object *obj, telebot_mess } } - //TODO: implement game parsing + // TODO: implement game parsing struct json_object *photo = NULL; if (json_object_object_get_ex(obj, "photo", &photo)) @@ -590,7 +655,7 @@ telebot_error_e telebot_parser_get_message(struct json_object *obj, telebot_mess ERR("Failed to get from message object"); } - //TODO: implement sticker parsing + // TODO: implement sticker parsing struct json_object *video = NULL; if (json_object_object_get_ex(obj, "video", &video)) { @@ -749,7 +814,7 @@ telebot_error_e telebot_parser_get_message(struct json_object *obj, telebot_mess if (json_object_object_get_ex(obj, "connected_website", &connected_website)) msg->connected_website = TELEBOT_SAFE_STRDUP(json_object_get_string(connected_website)); - //TODO: implement invoce, successful_payment, passport_data, reply_markup + // TODO: implement invoce, successful_payment, passport_data, reply_markup return TELEBOT_ERROR_NONE; } @@ -2028,3 +2093,38 @@ telebot_error_e telebot_parser_get_response_parameters(struct json_object *obj, return TELEBOT_ERROR_NONE; } + +telebot_error_e telebot_parser_get_chat_location(struct json_object *obj, telebot_chat_location_t *chat_location) +{ + if ((obj == NULL) || (chat_location == NULL)) + return TELEBOT_ERROR_INVALID_PARAMETER; + + struct json_object *address = NULL; + if (!json_object_object_get_ex(obj, "address", &address)) + { + ERR("Object is not chat type, address not found"); + return TELEBOT_ERROR_OPERATION_FAILED; + } + chat_location->address = TELEBOT_SAFE_STRDUP(json_object_get_string(address)); + + struct json_object *location = NULL; + if (json_object_object_get_ex(obj, "location", &location)) + { + chat_location->location = malloc(sizeof(telebot_location_t)); + if (telebot_parser_get_location(location, chat_location->location) != TELEBOT_ERROR_NONE) + { + ERR("Failed to get from chat object"); + TELEBOT_SAFE_FREE(chat_location->location); + TELEBOT_SAFE_FREE(chat_location->address); + return TELEBOT_ERROR_OPERATION_FAILED; + } + } + else + { + ERR("Object is not chat location type, location not found"); + TELEBOT_SAFE_FREE(chat_location->address); + return TELEBOT_ERROR_OPERATION_FAILED; + } + + return TELEBOT_ERROR_NONE; +} diff --git a/src/telebot.c b/src/telebot.c index ebded9c..3317621 100644 --- a/src/telebot.c +++ b/src/telebot.c @@ -35,19 +35,27 @@ struct telebot_handler { telebot_core_handler_t core_h; /**< Core handler */ - int offset; /**< Offset value to get updates */ + int offset; /**< Offset value to get updates */ }; static const char *telebot_update_type_str[TELEBOT_UPDATE_TYPE_MAX] = { - "message", "edited_message", "channel_post", - "edited_channel_post", "inline_query", - "chonse_inline_result", "callback_query", - "shipping_query", "pre_checkout_query", - "poll", "poll_answer"}; + "message", + "edited_message", + "channel_post", + "edited_channel_post", + "inline_query", + "chonse_inline_result", + "callback_query", + "shipping_query", + "pre_checkout_query", + "poll", + "poll_answer" +}; static void telebot_put_user(telebot_user_t *user); static void telebot_put_chat_photo(telebot_chat_photo_t *photo); static void telebot_put_chat_permissions(telebot_chat_permissions_t *permissions); +static void telebot_put_chat_location(telebot_chat_location_t *chat_location); static void telebot_put_message(telebot_message_t *msg); static void telebot_put_telebot_message_entity(telebot_message_entity_t *entity); static void telebot_put_audio(telebot_audio_t *audio); @@ -131,9 +139,10 @@ telebot_error_e telebot_get_proxy(telebot_handler_t handle, char **addr) return telebot_core_get_proxy(handle->core_h, addr); } -telebot_error_e telebot_get_updates(telebot_handler_t handle, int offset, int limit, int timeout, - telebot_update_type_e allowed_updates[], int allowed_updates_count, - telebot_update_t **updates, int *count) +telebot_error_e +telebot_get_updates(telebot_handler_t handle, int offset, int limit, int timeout, + telebot_update_type_e allowed_updates[], int allowed_updates_count, + telebot_update_t **updates, int *count) { int ret = TELEBOT_ERROR_NONE; telebot_core_response_t response; @@ -1577,6 +1586,14 @@ telebot_error_e telebot_put_chat(telebot_chat_t *chat) telebot_put_chat_photo(chat->photo); TELEBOT_SAFE_FREE(chat->photo); + for (size_t index=0; index < chat->count_active_usernames; index++) + TELEBOT_SAFE_FREE(chat->active_usernames[index]); + TELEBOT_SAFE_FREE(chat->active_usernames); + chat->count_active_usernames = 0; + + TELEBOT_SAFE_FREE(chat->emoji_status_custom_emoji_id); + TELEBOT_SAFE_FREE(chat->bio); + TELEBOT_SAFE_FREE(chat->description); TELEBOT_SAFE_FREE(chat->invite_link); @@ -1587,6 +1604,8 @@ telebot_error_e telebot_put_chat(telebot_chat_t *chat) TELEBOT_SAFE_FREE(chat->permissions); TELEBOT_SAFE_FREE(chat->sticker_set_name); + telebot_put_chat_location(chat->location); + TELEBOT_SAFE_FREE(chat->location); return TELEBOT_ERROR_NONE; } @@ -1609,6 +1628,12 @@ static void telebot_put_chat_permissions(telebot_chat_permissions_t *permissions return; } +static void telebot_put_chat_location(telebot_chat_location_t *chat_location) +{ + TELEBOT_SAFE_FREE(chat_location->address); + telebot_put_location(chat_location->location); +} + static void telebot_put_message(telebot_message_t *msg) { if (msg == NULL) From b27c75ff2773cd8377f53b5847904402f07f534e Mon Sep 17 00:00:00 2001 From: Elmurod Talipov Date: Sat, 26 Nov 2022 15:12:12 +0900 Subject: [PATCH 5/5] [Bot API 6.3] Update message object Signed-off-by: Elmurod Talipov --- include/telebot-parser.h | 5 +- include/telebot-types.h | 123 +++++++++++++++++----- src/telebot-parser.c | 214 +++++++++++++++++++++++++++------------ src/telebot.c | 53 +++++----- 4 files changed, 280 insertions(+), 115 deletions(-) diff --git a/include/telebot-parser.h b/include/telebot-parser.h index 7703fb0..671a0e9 100644 --- a/include/telebot-parser.h +++ b/include/telebot-parser.h @@ -103,7 +103,7 @@ telebot_error_e telebot_parser_get_file(struct json_object *obj, telebot_file_t telebot_error_e telebot_parser_get_callback_query(struct json_object *obj, telebot_callback_query_t *cb_query); /** Prase chat photo object */ -telebot_error_e telebot_parser_get_chat_photo(struct json_object *obj, telebot_chat_photo_t *photo); +telebot_error_e telebot_parser_get_chat_photo(struct json_object *obj, telebot_chat_photo_t *photo); /** Prase bot command object */ telebot_error_e telebot_parser_get_bot_command(struct json_object *obj, telebot_bot_command_t *command); @@ -117,4 +117,7 @@ telebot_error_e telebot_parser_get_response_parameters(struct json_object *obj, /** Prase chat location object object */ telebot_error_e telebot_parser_get_chat_location(struct json_object *obj, telebot_chat_location_t *chat_location); +/** Prase chat message auto-delete timer changed object object */ +telebot_error_e telebot_parser_get_message_auto_delete_timer_changed(struct json_object *obj, telebot_message_auto_delete_timer_changed_t *timer_changed); + #endif /* __TELEBOT_PARSER_H__ */ diff --git a/include/telebot-types.h b/include/telebot-types.h index 08303fe..1d0e9d1 100644 --- a/include/telebot-types.h +++ b/include/telebot-types.h @@ -237,9 +237,25 @@ typedef struct telebot_message { /** Unique message identifier */ int message_id; + /** + * Unique identifier of a message thread to which the message belongs; + * for supergroups only + */ + int message_thread_id; + /** Optional. Sender, can be empty for messages sent to channels */ struct telebot_user *from; + /** + * Optional. Sender of the message, sent on behalf of a chat. For example, + * the channel itself for channel posts, the supergroup itself for messages + * from anonymous group administrators, the linked channel for messages + * automatically forwarded to the discussion group. For backward compatibility, + * the field from contains a fake sender user in non-channel chats, + * if the message was sent on behalf of a chat. + */ + struct telebot_chat *sender_chat; + /** Date the message was sent in Unix time */ long date; @@ -279,6 +295,15 @@ typedef struct telebot_message { */ long forward_date; + /** Optional. True, if the message is sent to a forum topic */ + bool is_topic_message; + + /** + * Optional. True, if the message is a channel post that was automatically + * forwarded to the connected discussion group + */ + bool is_automatic_forward; + /** * For replies, the original message. Note that the Message object in this * field will not contain further reply_to_message fields even if it itself @@ -286,9 +311,15 @@ typedef struct telebot_message { */ struct telebot_message *reply_to_message; + /** Optional. Bot through which the message was sent */ + struct telebot_user *via_bot; + /** Optional. Date the message was last edited in Unix time */ long edit_date; + /** Optional. True, if the message can't be forwarded */ + bool has_protected_content; + /** * Optional. The unique identifier of a media message group this message * belongs to @@ -309,11 +340,11 @@ typedef struct telebot_message { int count_entities; /** - * Optional. For messages with a caption, special entities like usernames, - * URLs, bot commands, etc. that appear in the caption. + * Optional. Message is an animation, information about the animation. + * For backward compatibility, when this field is set, the document field + * will also be set */ - struct telebot_message_entity *caption_entities; - int count_caption_entities; + struct telebot_animation *animation; /** Optional. Message is an audio file, information about the file */ struct telebot_audio *audio; @@ -321,12 +352,6 @@ typedef struct telebot_message { /** Optional. Message is a general file, information about the file */ struct telebot_document *document; - /** Optional. Message is a animation, information about the animation */ - struct telebot_animation *animation; - - /** Optional. Message is a game, information about the game. */ - struct telebot_game *game; //TODO:define type - /** Optional. Message is a photo, available sizes of the photo */ struct telebot_photo *photos; int count_photos; @@ -337,29 +362,39 @@ typedef struct telebot_message { /** Optional. Message is a video, information about the video */ struct telebot_video *video; - /** Optional. Message is a voice message, information about the file */ - struct telebot_voice *voice; - /** Optional. Message is a video note, information about the video message */ struct telebot_video_note *video_note; + /** Optional. Message is a voice message, information about the file */ + struct telebot_voice *voice; + /** Optional. Caption for the photo or video */ char *caption; + /** + * Optional. For messages with a caption, special entities like usernames, + * URLs, bot commands, etc. that appear in the caption. + */ + struct telebot_message_entity *caption_entities; + int count_caption_entities; + /** Optional. Message is a shared contact, information about the contact */ struct telebot_contact *contact; - /** Optional. Message is a shared location, information about the location */ - struct telebot_location *location; + /** Optional. Message is a dice with random value from 1 to 6 */ + struct telebot_dice *dice; - /** Optional. Message is a venue, information about the venue */ - struct telebot_venue *venue; + /** Optional. Message is a game, information about the game. */ + struct telebot_game *game; //TODO:define type /** Optional. Message is a native poll, information about the poll */ struct telebot_poll *poll; - /** Optional. Message is a dice with random value from 1 to 6 */ - struct telebot_dice *dice; + /** Optional. Message is a venue, information about the venue */ + struct telebot_venue *venue; + + /** Optional. Message is a shared location, information about the location */ + struct telebot_location *location; /** * Optional. New members that were added to the group or supergroup and @@ -394,6 +429,9 @@ typedef struct telebot_message { /** Optional. Service message: the channel has been created */ bool channel_chat_created; + /** Optional. Service message: auto-delete timer settings changed in the chat */ + struct telebot_message_auto_delete_timer_changed *message_auto_delete_timer_changed; + /** * Optional. The group has been migrated to a supergroup with the specified * identifier, not exceeding 1e13 by absolute value @@ -431,6 +469,36 @@ typedef struct telebot_message { /** Telegram Passport data */ struct telebot_passport_data *passport_data; //TODO:define type + /** + * Optional. Service message. A user in the chat triggered another user's + * proximity alert while sharing Live Location. + */ + struct telebot_proximity_alert_triggered *proximity_alert_triggered; //TODO:define type + + /** Optional. Service message: forum topic created. */ + struct telebot_forum_topic_created *forum_topic_created; //TODO:define type + + /** Optional. Service message: forum topic closed. */ + struct telebot_forum_topic_closed *forum_topic_closed; //TODO:define type + + /** Optional. Service message: forum topic reopened. */ + struct telebot_forum_topic_reopened *forum_topic_reopened; //TODO:define type + + /** Optional. Service message: video chat scheduled. */ + struct telebot_video_chat_scheduled *video_chat_scheduled; //TODO:define type + + /** Optional. Service message: video chat started. */ + struct telebot_video_chat_started *video_chat_started; //TODO:define type + + /** Optional. Service message: video chat ended. */ + struct telebot_video_chat_ended *video_chat_ended; //TODO:define type + + /** Optional. Service message: new participants invited to a video chat. */ + struct telebot_video_chat_participants_invited *video_chat_participants_invited; //TODO:define type + + /** Optional. Service message: data sent by a Web App. */ + struct telebot_web_app_data *web_app_data; //TODO:define type + /** * Inline keyboard attached to the message. login_url buttons are * represented as ordinary url buttons. @@ -1370,17 +1438,22 @@ typedef struct telebot_webhook_info { * @brief Thi object represetns information about the current status of a webhook. */ typedef struct telebot_chat_location { - /** - * The location to which the supergroup is connected. Can't be a live location. - */ + /** The location to which the supergroup is connected. Can't be a live location.*/ struct telebot_location *location; - /** - * Location address; 1-64 characters, as defined by the chat owner - */ + /** Location address; 1-64 characters, as defined by the chat owner */ char *address; } telebot_chat_location_t; +/** + * @brief This object represents a service message about a change in + * auto-delete timer settings. + */ +typedef struct telebot_message_auto_delete_timer_changed { + /** New auto-delete time for messages in the chat; in seconds */ + int message_auto_delete_time; +} telebot_message_auto_delete_timer_changed_t; + /** * @brief This is opaque object to represent a telebot handler. */ diff --git a/src/telebot-parser.c b/src/telebot-parser.c index 9cf433d..d088441 100644 --- a/src/telebot-parser.c +++ b/src/telebot-parser.c @@ -532,6 +532,10 @@ telebot_error_e telebot_parser_get_message(struct json_object *obj, telebot_mess } /* Optional Fields */ + struct json_object *message_thread_id = NULL; + if (json_object_object_get_ex(obj, "message_thread_id", &message_thread_id)) + msg->message_thread_id = json_object_get_int(message_thread_id); + struct json_object *from = NULL; if (json_object_object_get_ex(obj, "from", &from)) { @@ -543,6 +547,17 @@ telebot_error_e telebot_parser_get_message(struct json_object *obj, telebot_mess } } + struct json_object *sender_chat = NULL; + if (json_object_object_get_ex(obj, "sender_chat", &sender_chat)) + { + msg->sender_chat = malloc(sizeof(telebot_chat_t)); + if (telebot_parser_get_chat(sender_chat, msg->sender_chat) != TELEBOT_ERROR_NONE) + { + ERR("Failed to get from message object"); + TELEBOT_SAFE_FREE(msg->sender_chat); + } + } + struct json_object *forward_from = NULL; if (json_object_object_get_ex(obj, "forward_from", &forward_from)) { @@ -581,6 +596,14 @@ telebot_error_e telebot_parser_get_message(struct json_object *obj, telebot_mess if (json_object_object_get_ex(obj, "forward_date", &forward_date)) msg->forward_date = json_object_get_int(forward_date); + struct json_object *is_topic_message = NULL; + if (json_object_object_get_ex(obj, "is_topic_message", &is_topic_message)) + msg->is_topic_message = json_object_get_boolean(is_topic_message); + + struct json_object *is_automatic_forward = NULL; + if (json_object_object_get_ex(obj, "is_automatic_forward", &is_automatic_forward)) + msg->is_automatic_forward = json_object_get_boolean(is_automatic_forward); + struct json_object *reply_to_message = NULL; if (json_object_object_get_ex(obj, "reply_to_message", &reply_to_message)) { @@ -592,10 +615,25 @@ telebot_error_e telebot_parser_get_message(struct json_object *obj, telebot_mess } } + struct json_object *via_bot = NULL; + if (json_object_object_get_ex(obj, "via_bot", &via_bot)) + { + msg->via_bot = malloc(sizeof(telebot_user_t)); + if (telebot_parser_get_user(via_bot, msg->via_bot) != TELEBOT_ERROR_NONE) + { + ERR("Failed to get from message object"); + TELEBOT_SAFE_FREE(msg->via_bot); + } + } + struct json_object *edit_date = NULL; if (json_object_object_get_ex(obj, "edit_date", &edit_date)) msg->edit_date = json_object_get_int(edit_date); + struct json_object *has_protected_content = NULL; + if (json_object_object_get_ex(obj, "has_protected_content", &has_protected_content)) + msg->has_protected_content = json_object_get_boolean(has_protected_content); + struct json_object *media_group_id = NULL; if (json_object_object_get_ex(obj, "media_group_id", &media_group_id)) msg->media_group_id = TELEBOT_SAFE_STRDUP(json_object_get_string(media_group_id)); @@ -616,12 +654,15 @@ telebot_error_e telebot_parser_get_message(struct json_object *obj, telebot_mess ERR("Failed to get from message object"); } - struct json_object *caption_entities = NULL; - if (json_object_object_get_ex(obj, "caption_entities", &caption_entities)) + struct json_object *animation = NULL; + if (json_object_object_get_ex(obj, "animation", &animation)) { - if (telebot_parser_get_message_entities(caption_entities, &(msg->caption_entities), - &(msg->count_caption_entities)) != TELEBOT_ERROR_NONE) - ERR("Failed to get from message object"); + msg->animation = malloc(sizeof(telebot_animation_t)); + if (telebot_parser_get_animation(animation, msg->animation) != TELEBOT_ERROR_NONE) + { + ERR("Failed to get from message object"); + TELEBOT_SAFE_FREE(msg->animation); + } } struct json_object *audio = NULL; @@ -646,8 +687,6 @@ telebot_error_e telebot_parser_get_message(struct json_object *obj, telebot_mess } } - // TODO: implement game parsing - struct json_object *photo = NULL; if (json_object_object_get_ex(obj, "photo", &photo)) { @@ -656,6 +695,7 @@ telebot_error_e telebot_parser_get_message(struct json_object *obj, telebot_mess } // TODO: implement sticker parsing + struct json_object *video = NULL; if (json_object_object_get_ex(obj, "video", &video)) { @@ -667,17 +707,6 @@ telebot_error_e telebot_parser_get_message(struct json_object *obj, telebot_mess } } - struct json_object *voice = NULL; - if (json_object_object_get_ex(obj, "voice", &voice)) - { - msg->voice = malloc(sizeof(telebot_voice_t)); - if (telebot_parser_get_voice(voice, msg->voice) != TELEBOT_ERROR_NONE) - { - ERR("Failed to get from message object"); - TELEBOT_SAFE_FREE(msg->voice); - } - } - struct json_object *video_note = NULL; if (json_object_object_get_ex(obj, "video_note", &video_note)) { @@ -689,10 +718,29 @@ telebot_error_e telebot_parser_get_message(struct json_object *obj, telebot_mess } } + struct json_object *voice = NULL; + if (json_object_object_get_ex(obj, "voice", &voice)) + { + msg->voice = malloc(sizeof(telebot_voice_t)); + if (telebot_parser_get_voice(voice, msg->voice) != TELEBOT_ERROR_NONE) + { + ERR("Failed to get from message object"); + TELEBOT_SAFE_FREE(msg->voice); + } + } + struct json_object *caption = NULL; if (json_object_object_get_ex(obj, "caption", &caption)) msg->caption = TELEBOT_SAFE_STRDUP(json_object_get_string(caption)); + struct json_object *caption_entities = NULL; + if (json_object_object_get_ex(obj, "caption_entities", &caption_entities)) + { + if (telebot_parser_get_message_entities(caption_entities, &(msg->caption_entities), + &(msg->count_caption_entities)) != TELEBOT_ERROR_NONE) + ERR("Failed to get from message object"); + } + struct json_object *contact = NULL; if (json_object_object_get_ex(obj, "contact", &contact)) { @@ -704,27 +752,18 @@ telebot_error_e telebot_parser_get_message(struct json_object *obj, telebot_mess } } - struct json_object *location = NULL; - if (json_object_object_get_ex(obj, "location", &location)) + struct json_object *dice = NULL; + if (json_object_object_get_ex(obj, "dice", &dice)) { - msg->location = malloc(sizeof(telebot_location_t)); - if (telebot_parser_get_location(location, msg->location) != TELEBOT_ERROR_NONE) + msg->dice = malloc(sizeof(telebot_dice_t)); + if (telebot_parser_get_dice(dice, msg->dice) != TELEBOT_ERROR_NONE) { - ERR("Failed to get from message object"); - TELEBOT_SAFE_FREE(msg->location); + ERR("Failed to get from message object"); + TELEBOT_SAFE_FREE(msg->dice); } } - struct json_object *venue = NULL; - if (json_object_object_get_ex(obj, "venue", &venue)) - { - msg->venue = malloc(sizeof(telebot_venue_t)); - if (telebot_parser_get_venue(venue, msg->venue) != TELEBOT_ERROR_NONE) - { - ERR("Failed to get from message object"); - TELEBOT_SAFE_FREE(msg->venue); - } - } + // TODO: implement game parsing struct json_object *poll = NULL; if (json_object_object_get_ex(obj, "poll", &poll)) @@ -737,35 +776,47 @@ telebot_error_e telebot_parser_get_message(struct json_object *obj, telebot_mess } } - struct json_object *dice = NULL; - if (json_object_object_get_ex(obj, "dice", &dice)) + struct json_object *venue = NULL; + if (json_object_object_get_ex(obj, "venue", &venue)) { - msg->dice = malloc(sizeof(telebot_dice_t)); - if (telebot_parser_get_dice(dice, msg->dice) != TELEBOT_ERROR_NONE) + msg->venue = malloc(sizeof(telebot_venue_t)); + if (telebot_parser_get_venue(venue, msg->venue) != TELEBOT_ERROR_NONE) { - ERR("Failed to get from message object"); - TELEBOT_SAFE_FREE(msg->dice); + ERR("Failed to get from message object"); + TELEBOT_SAFE_FREE(msg->venue); + } + } + + struct json_object *location = NULL; + if (json_object_object_get_ex(obj, "location", &location)) + { + msg->location = malloc(sizeof(telebot_location_t)); + if (telebot_parser_get_location(location, msg->location) != TELEBOT_ERROR_NONE) + { + ERR("Failed to get from message object"); + TELEBOT_SAFE_FREE(msg->location); } } - struct json_object *ncm = NULL; - if (json_object_object_get_ex(obj, "new_chat_members", &ncm)) + struct json_object *new_chat_members = NULL; + if (json_object_object_get_ex(obj, "new_chat_members", &new_chat_members)) { - if (telebot_parser_get_users(ncm, &(msg->new_chat_members), &(msg->count_new_chat_members)) != TELEBOT_ERROR_NONE) + int ret = telebot_parser_get_users(new_chat_members, &(msg->new_chat_members), &(msg->count_new_chat_members)); + if (ret != TELEBOT_ERROR_NONE) ERR("Failed to get from message object"); } - struct json_object *lcm = NULL; - if (json_object_object_get_ex(obj, "left_chat_members", &lcm)) + struct json_object *left_chat_members = NULL; + if (json_object_object_get_ex(obj, "left_chat_members", &left_chat_members)) { - if (telebot_parser_get_users(lcm, &(msg->left_chat_members), &(msg->count_left_chat_members)) != - TELEBOT_ERROR_NONE) + int ret = telebot_parser_get_users(left_chat_members, &(msg->left_chat_members), &(msg->count_left_chat_members)); + if (ret != TELEBOT_ERROR_NONE) ERR("Failed to get from message object"); } - struct json_object *nct = NULL; - if (json_object_object_get_ex(obj, "new_chat_title", &nct)) - msg->new_chat_title = TELEBOT_SAFE_STRDUP(json_object_get_string(nct)); + struct json_object *new_chat_title = NULL; + if (json_object_object_get_ex(obj, "new_chat_title", &new_chat_title)) + msg->new_chat_title = TELEBOT_SAFE_STRDUP(json_object_get_string(new_chat_title)); struct json_object *new_chat_photo = NULL; if (json_object_object_get_ex(obj, "new_chat_photo", &new_chat_photo)) @@ -779,25 +830,38 @@ telebot_error_e telebot_parser_get_message(struct json_object *obj, telebot_mess if (json_object_object_get_ex(obj, "delete_chat_photo", &del_chat_photo)) msg->delete_chat_photo = json_object_get_boolean(del_chat_photo); - struct json_object *gcc = NULL; - if (json_object_object_get_ex(obj, "group_chat_created", &gcc)) - msg->group_chat_created = json_object_get_boolean(gcc); + struct json_object *group_chat_created = NULL; + if (json_object_object_get_ex(obj, "group_chat_created", &group_chat_created)) + msg->group_chat_created = json_object_get_boolean(group_chat_created); - struct json_object *sgcc = NULL; - if (json_object_object_get_ex(obj, "supergroup_chat_created", &sgcc)) - msg->supergroup_chat_created = json_object_get_boolean(sgcc); + struct json_object *supergroup_chat_created = NULL; + if (json_object_object_get_ex(obj, "supergroup_chat_created", &supergroup_chat_created)) + msg->supergroup_chat_created = json_object_get_boolean(supergroup_chat_created); - struct json_object *cacc = NULL; - if (json_object_object_get_ex(obj, "channel_chat_created", &cacc)) - msg->channel_chat_created = json_object_get_boolean(cacc); + struct json_object *channel_chat_created = NULL; + if (json_object_object_get_ex(obj, "channel_chat_created", &channel_chat_created)) + msg->channel_chat_created = json_object_get_boolean(channel_chat_created); - struct json_object *mtci = NULL; - if (json_object_object_get_ex(obj, "migrate_to_chat_id", &mtci)) - msg->migrate_to_chat_id = json_object_get_int64(mtci); + struct json_object *message_auto_delete_timer_changed = NULL; + if (json_object_object_get_ex(obj, "message_auto_delete_timer_changed", &message_auto_delete_timer_changed)) + { + msg->message_auto_delete_timer_changed = malloc(sizeof(telebot_message_auto_delete_timer_changed_t)); + int ret = telebot_parser_get_message_auto_delete_timer_changed(message_auto_delete_timer_changed, + msg->message_auto_delete_timer_changed); + if (ret != TELEBOT_ERROR_NONE) + { + ERR("Failed to get from message object"); + TELEBOT_SAFE_FREE(msg->message_auto_delete_timer_changed); + } + } + + struct json_object *migrate_to_chat_id = NULL; + if (json_object_object_get_ex(obj, "migrate_to_chat_id", &migrate_to_chat_id)) + msg->migrate_to_chat_id = json_object_get_int64(migrate_to_chat_id); - struct json_object *mftci = NULL; - if (json_object_object_get_ex(obj, "migrate_from_chat_id", &mftci)) - msg->migrate_from_chat_id = json_object_get_int64(mftci); + struct json_object *migrate_from_chat_id = NULL; + if (json_object_object_get_ex(obj, "migrate_from_chat_id", &migrate_from_chat_id)) + msg->migrate_from_chat_id = json_object_get_int64(migrate_from_chat_id); struct json_object *pinned_message = NULL; if (json_object_object_get_ex(obj, "pinned_message", &pinned_message)) @@ -2085,7 +2149,7 @@ telebot_error_e telebot_parser_get_response_parameters(struct json_object *obj, memset(resp_param, 0, sizeof(telebot_response_paramters_t)); struct json_object *migrate_to_chat_id = NULL; if (json_object_object_get_ex(obj, "migrate_to_chat_id", &migrate_to_chat_id)) - resp_param->migrate_to_chat_id = json_object_get_int(migrate_to_chat_id); + resp_param->migrate_to_chat_id = json_object_get_int64(migrate_to_chat_id); struct json_object *retry_after = NULL; if (json_object_object_get_ex(obj, "retry_after", &retry_after)) @@ -2128,3 +2192,21 @@ telebot_error_e telebot_parser_get_chat_location(struct json_object *obj, telebo return TELEBOT_ERROR_NONE; } + +telebot_error_e +telebot_parser_get_message_auto_delete_timer_changed(struct json_object *obj, + telebot_message_auto_delete_timer_changed_t *timer_changed) +{ + if ((obj == NULL) || (timer_changed == NULL)) + return TELEBOT_ERROR_INVALID_PARAMETER; + + struct json_object *message_auto_delete_time = NULL; + if (!json_object_object_get_ex(obj, "message_auto_delete_time", &message_auto_delete_time)) + { + ERR("Object is not message auto-delete timer type, message_auto_delete_time not found"); + return TELEBOT_ERROR_OPERATION_FAILED; + } + timer_changed->message_auto_delete_time = json_object_get_int(message_auto_delete_time); + + return TELEBOT_ERROR_NONE; +} diff --git a/src/telebot.c b/src/telebot.c index 3317621..fd69b46 100644 --- a/src/telebot.c +++ b/src/telebot.c @@ -1642,6 +1642,9 @@ static void telebot_put_message(telebot_message_t *msg) telebot_put_user(msg->from); TELEBOT_SAFE_FREE(msg->from); + telebot_put_chat(msg->sender_chat); + TELEBOT_SAFE_FREE(msg->sender_chat); + telebot_put_chat(msg->chat); TELEBOT_SAFE_FREE(msg->chat); @@ -1657,6 +1660,9 @@ static void telebot_put_message(telebot_message_t *msg) telebot_put_message(msg->reply_to_message); TELEBOT_SAFE_FREE(msg->reply_to_message); + telebot_put_user(msg->via_bot); + TELEBOT_SAFE_FREE(msg->via_bot); + TELEBOT_SAFE_FREE(msg->media_group_id); TELEBOT_SAFE_FREE(msg->author_signature); TELEBOT_SAFE_FREE(msg->text); @@ -1669,13 +1675,8 @@ static void telebot_put_message(telebot_message_t *msg) msg->count_entities = 0; } - if (msg->caption_entities) - { - for (int index = 0; index < msg->count_caption_entities; index++) - telebot_put_telebot_message_entity(&(msg->caption_entities[index])); - TELEBOT_SAFE_FREE(msg->caption_entities); - msg->count_caption_entities = 0; - } + telebot_put_animation(msg->animation); + TELEBOT_SAFE_FREE(msg->animation); telebot_put_audio(msg->audio); TELEBOT_SAFE_FREE(msg->audio); @@ -1683,13 +1684,6 @@ static void telebot_put_message(telebot_message_t *msg) telebot_put_document(msg->document); TELEBOT_SAFE_FREE(msg->document); - // TODO - // telebot_put_game(msg->game); - // TELEBOT_SAFE_FREE(msg->game); - - telebot_put_animation(msg->animation); - TELEBOT_SAFE_FREE(msg->animation); - if (msg->photos) { for (int index = 0; index < msg->count_photos; index++) @@ -1705,28 +1699,39 @@ static void telebot_put_message(telebot_message_t *msg) telebot_put_video(msg->video); TELEBOT_SAFE_FREE(msg->video); - telebot_put_voice(msg->voice); - TELEBOT_SAFE_FREE(msg->voice); - telebot_put_video_note(msg->video_note); TELEBOT_SAFE_FREE(msg->video_note); + telebot_put_voice(msg->voice); + TELEBOT_SAFE_FREE(msg->voice); + TELEBOT_SAFE_FREE(msg->caption); + if (msg->caption_entities) + { + for (int index = 0; index < msg->count_caption_entities; index++) + telebot_put_telebot_message_entity(&(msg->caption_entities[index])); + TELEBOT_SAFE_FREE(msg->caption_entities); + msg->count_caption_entities = 0; + } telebot_put_contact(msg->contact); TELEBOT_SAFE_FREE(msg->contact); - telebot_put_location(msg->location); - TELEBOT_SAFE_FREE(msg->location); + telebot_put_dice(msg->dice); + TELEBOT_SAFE_FREE(msg->dice); - telebot_put_venue(msg->venue); - TELEBOT_SAFE_FREE(msg->venue); + // TODO + // telebot_put_game(msg->game); + // TELEBOT_SAFE_FREE(msg->game); telebot_put_poll(msg->poll); TELEBOT_SAFE_FREE(msg->poll); - telebot_put_dice(msg->dice); - TELEBOT_SAFE_FREE(msg->dice); + telebot_put_venue(msg->venue); + TELEBOT_SAFE_FREE(msg->venue); + + telebot_put_location(msg->location); + TELEBOT_SAFE_FREE(msg->location); if (msg->new_chat_members) { @@ -1754,6 +1759,8 @@ static void telebot_put_message(telebot_message_t *msg) msg->count_new_chat_photos = 0; } + TELEBOT_SAFE_FREE(msg->message_auto_delete_timer_changed); + telebot_put_message(msg->pinned_message); // TODO