From 0de2dce3cc83d5ee78db80ab70116eada8b6740f Mon Sep 17 00:00:00 2001 From: ozlb Date: Sun, 31 Dec 2023 08:41:46 +0100 Subject: [PATCH 1/6] short_description - h bot's short description, which is shown on the bot's profile page and is sent together with the link when users share the bot --- include/telebot-core.h | 25 +++++++++++++++++++++++++ include/telebot-methods.h | 26 ++++++++++++++++++++++++++ include/telebot-private.h | 3 +++ 3 files changed, 54 insertions(+) diff --git a/include/telebot-core.h b/include/telebot-core.h index cec6ef2..241519e 100644 --- a/include/telebot-core.h +++ b/include/telebot-core.h @@ -1199,6 +1199,31 @@ telebot_error_e telebot_core_delete_message(telebot_core_handler_t *core_h, */ void telebot_core_put_response(telebot_core_response_t *response); +//////////////////////////////////////////////////////////////////////////////////////// +//ozlb +//////////////////////////////////////////////////////////////////////////////////////// + +/** + * @brief Use this method to change the bot's short description, which is shown on the bot's profile page and is sent together with the link when users share the bot. + * @param[in] core_h The telebot core handler created with #telebot_core_create(). + * @param[in] short_description New short description for the bot; 0-120 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. + */ +telebot_error_e telebot_core_set_my_short_description(telebot_core_handler_t *core_h, + const char *short_description, + telebot_core_response_t *response); + +/** + * @brief Use this method to get the current bot short description for the given user language. Returns BotShortDescription on success. + * @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. + */ +telebot_error_e telebot_core_get_my_short_description(telebot_core_handler_t *core_h, + telebot_core_response_t *response); + /** * @} // end of APIs */ diff --git a/include/telebot-methods.h b/include/telebot-methods.h index 7a7e90e..898a50a 100644 --- a/include/telebot-methods.h +++ b/include/telebot-methods.h @@ -881,6 +881,7 @@ telebot_error_e telebot_set_chat_title(telebot_handler_t handle, long long int c telebot_error_e telebot_set_chat_description(telebot_handler_t handle, long long int chat_id, const char *description); + /** * @brief Pin a message in a supergroup or a channel. The bot must be an administrator * in the chat for this to work and must have the 'can_pin_messages' admin right @@ -1180,6 +1181,31 @@ telebot_error_e telebot_stop_poll(telebot_handler_t handle, long long int chat_i telebot_error_e telebot_delete_message(telebot_handler_t handle, long long int chat_id, int message_id); + +//////////////////////////////////////////////////////////////////////////////////////// +//ozlb +//////////////////////////////////////////////////////////////////////////////////////// + +/** + * @brief Use this method to change the bot's short description, which is shown on the bot's profile page and is sent together with the link when users share the bot. + * @param[in] handle The telebot handler created with #telebot_create(). + * @param[in] short_description New chat description, 0-120 characters. + * @return on Success, #TELEBOT_ERROR_NONE is returned, otherwise a negative + * error value. + */ +telebot_error_e telebot_set_my_short_description(telebot_handler_t handle, + const char *short_description); + +/** + * @brief Use this method to get the current bot description for the given user language. Returns BotDescription on success. + * + * @param[in] handle The telebot handler created with #telebot_create(). + * @param[out] short_description String The bot's short description + * @return on Success, #TELEBOT_ERROR_NONE is returned, and user object is + * stored in input parameter. + */ +telebot_error_e telebot_get_my_short_description(telebot_handler_t handle, char **short_description); + /** * @} // end of APIs */ diff --git a/include/telebot-private.h b/include/telebot-private.h index b3748e0..dd59d7b 100644 --- a/include/telebot-private.h +++ b/include/telebot-private.h @@ -84,6 +84,9 @@ #define TELEBOT_METHOD_EDIT_MESSAGE_REPLY_MARKUP "editMessageReplyMarkup" #define TELEBOT_METHOD_STOP_POLL "stopPoll" #define TELEBOT_METHOD_DELETE_MESSAGE "deleteMessage" +//ozlb +#define TELEBOT_METHOD_SET_MY_SHORT_DESCRIPTION "setMyShortDescription" +#define TELEBOT_METHOD_GET_MY_SHORT_DESCRIPTION "getMyShortDescription" #ifdef DEBUG #define ERR(fmt, args...) fprintf(stderr, "[ERROR][%s:%d]" fmt "\n", __func__, __LINE__, ##args) From ecfdd355ebc452d4675cb43d770a2826f5559872 Mon Sep 17 00:00:00 2001 From: ozlb Date: Sun, 31 Dec 2023 08:42:46 +0100 Subject: [PATCH 2/6] short_description - c bot's short description, which is shown on the bot's profile page and is sent together with the link when users share the bot --- src/telebot-core.c | 60 +++++++++++++++++++++++-------- src/telebot.c | 88 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 129 insertions(+), 19 deletions(-) diff --git a/src/telebot-core.c b/src/telebot-core.c index 19292a8..b6fa75e 100644 --- a/src/telebot-core.c +++ b/src/telebot-core.c @@ -20,12 +20,11 @@ #include #include #include -#include #include #include -#include -#include -#include +#include "telebot-private.h" +#include "telebot-common.h" +#include "telebot-core.h" void telebot_core_put_response(telebot_core_response_t *response) { @@ -135,18 +134,8 @@ static telebot_error_e telebot_core_curl_perform(telebot_core_handler_t *core_h, curl_easy_getinfo(curl_h, CURLINFO_RESPONSE_CODE, &resp_code); if (resp_code != 200L) { + ERR("Wrong HTTP response received, response: %ld", resp_code); ret = TELEBOT_ERROR_OPERATION_FAILED; - - struct json_object *obj = json_tokener_parse(resp->data); - if (obj == NULL) { - ERR("HTTP error: %ld", resp_code); - ERR("Response: %s", resp->data); - } else { - ERR("HTTP error: %d - \"%s\"", - json_object_get_int(json_object_object_get(obj, "error_code")), - json_object_get_string(json_object_object_get(obj, "description"))); - } - goto finish; } @@ -2628,3 +2617,44 @@ telebot_error_e telebot_core_delete_message(telebot_core_handler_t *core_h, return telebot_core_curl_perform(core_h, TELEBOT_METHOD_DELETE_MESSAGE, mimes, index, response); } + +//////////////////////////////////////////////////////////////////////////////////////// +//ozlb +//////////////////////////////////////////////////////////////////////////////////////// + +telebot_error_e telebot_core_set_my_short_description(telebot_core_handler_t *core_h, + const char *short_description, telebot_core_response_t *response) +{ + if ((core_h == NULL) || (core_h->token == NULL)) + { + ERR("Handler, or token is NULL"); + return TELEBOT_ERROR_INVALID_PARAMETER; + } + + if ((short_description == NULL) || (strlen(short_description) > 120)) + { + ERR("Valid title is required"); + return TELEBOT_ERROR_INVALID_PARAMETER; + } + + int index = 0; + telebot_core_mime_t mimes[1]; // number of arguments + mimes[index].name = "short_description"; + mimes[index].type = TELEBOT_MIME_TYPE_DATA; + snprintf(mimes[index].data, sizeof(mimes[index].data), "%s", short_description); + ++index; + + return telebot_core_curl_perform(core_h, TELEBOT_METHOD_SET_MY_SHORT_DESCRIPTION, mimes, index, response); +} + +telebot_error_e telebot_core_get_my_short_description(telebot_core_handler_t *core_h, + telebot_core_response_t *response) +{ + 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_SHORT_DESCRIPTION, NULL, 0, response); +} diff --git a/src/telebot.c b/src/telebot.c index 37ede1e..b2f01b7 100644 --- a/src/telebot.c +++ b/src/telebot.c @@ -25,10 +25,10 @@ #include #include #include -#include -#include -#include -#include +#include "telebot.h" +#include "telebot-core.h" +#include "telebot-private.h" +#include "telebot-parser.h" typedef struct telebot_handler_s { @@ -1967,3 +1967,83 @@ static void telebot_put_callback_query(telebot_callback_query_t *query) //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); + +//////////////////////////////////////////////////////////////////////////////////////// +//ozlb +//////////////////////////////////////////////////////////////////////////////////////// + +telebot_error_e telebot_set_my_short_description(telebot_handler_t handle, + const char *short_description) +{ + telebot_hdata_t *_handle = (telebot_hdata_t *)handle; + if (_handle == NULL) + return TELEBOT_ERROR_NOT_SUPPORTED; + + if (short_description == NULL) + return TELEBOT_ERROR_INVALID_PARAMETER; + + telebot_core_response_t response; + int ret = telebot_core_set_my_short_description(_handle->core_h, short_description, + &response); + telebot_core_put_response(&response); + + return ret; +} + +telebot_error_e telebot_get_my_short_description(telebot_handler_t handle, char **short_description) +{ + telebot_hdata_t *_handle = (telebot_hdata_t *)handle; + if (_handle == NULL) + return TELEBOT_ERROR_NOT_SUPPORTED; + + if (short_description == NULL) + return TELEBOT_ERROR_INVALID_PARAMETER; + + *short_description = NULL; + + telebot_core_response_t response; + int ret = telebot_core_get_my_short_description(_handle->core_h, &response); + if (ret != TELEBOT_ERROR_NONE) + return ret; + + struct json_object *obj = telebot_parser_str_to_obj(response.data); + if (obj == NULL) + { + ret = TELEBOT_ERROR_OPERATION_FAILED; + goto finish; + } + + struct json_object *ok = NULL; + if (!json_object_object_get_ex(obj, "ok", &ok) || !json_object_get_boolean(ok)) + { + ret = TELEBOT_ERROR_OPERATION_FAILED; + goto finish; + } + + struct json_object *result = NULL; + if (!json_object_object_get_ex(obj, "result", &result)) + { + ret = TELEBOT_ERROR_OPERATION_FAILED; + goto finish; + } + + struct json_object *j_short_description = NULL; + if (json_object_object_get_ex(result, "short_description", &j_short_description)) + { + *short_description = TELEBOT_SAFE_STRDUP(json_object_get_string(j_short_description)); + if (*short_description == NULL) + ret = TELEBOT_ERROR_OUT_OF_MEMORY; + else + ret = TELEBOT_ERROR_NONE; + } + else + { + ret = TELEBOT_ERROR_OPERATION_FAILED; + } + +finish: + if (obj) json_object_put(obj); + telebot_core_put_response(&response); + + return ret; +} From 0c846a0a65a04beaa8d1531f81a02704ba00ab4b Mon Sep 17 00:00:00 2001 From: ozlb Date: Sun, 31 Dec 2023 08:47:49 +0100 Subject: [PATCH 3/6] short_description bot's short description, which is shown on the bot's profile page and is sent together with the link when users share the bot --- src/telebot-core.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/telebot-core.c b/src/telebot-core.c index b6fa75e..2519e04 100644 --- a/src/telebot-core.c +++ b/src/telebot-core.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include "telebot-private.h" @@ -134,8 +135,18 @@ static telebot_error_e telebot_core_curl_perform(telebot_core_handler_t *core_h, curl_easy_getinfo(curl_h, CURLINFO_RESPONSE_CODE, &resp_code); if (resp_code != 200L) { - ERR("Wrong HTTP response received, response: %ld", resp_code); ret = TELEBOT_ERROR_OPERATION_FAILED; + + struct json_object *obj = json_tokener_parse(resp->data); + if (obj == NULL) { + ERR("HTTP error: %ld", resp_code); + ERR("Response: %s", resp->data); + } else { + ERR("HTTP error: %d - \"%s\"", + json_object_get_int(json_object_object_get(obj, "error_code")), + json_object_get_string(json_object_object_get(obj, "description"))); + } + goto finish; } From 571a89f7799990703b4f99166a46b03ee5440d14 Mon Sep 17 00:00:00 2001 From: ozlb Date: Tue, 30 Jan 2024 00:07:26 +0100 Subject: [PATCH 4/6] Update telebot-core.h Please remove custom comments https://github.com/smartnode/telebot/pull/59#discussion_r1460791586 --- include/telebot-core.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/include/telebot-core.h b/include/telebot-core.h index 241519e..220929d 100644 --- a/include/telebot-core.h +++ b/include/telebot-core.h @@ -1199,10 +1199,6 @@ telebot_error_e telebot_core_delete_message(telebot_core_handler_t *core_h, */ void telebot_core_put_response(telebot_core_response_t *response); -//////////////////////////////////////////////////////////////////////////////////////// -//ozlb -//////////////////////////////////////////////////////////////////////////////////////// - /** * @brief Use this method to change the bot's short description, which is shown on the bot's profile page and is sent together with the link when users share the bot. * @param[in] core_h The telebot core handler created with #telebot_core_create(). From c01f5c060d62f520f2a2674e85ef43689fcf4efe Mon Sep 17 00:00:00 2001 From: ozlb Date: Tue, 30 Jan 2024 00:08:22 +0100 Subject: [PATCH 5/6] Update telebot-core.c https://github.com/smartnode/telebot/pull/59#discussion_r1460792137 Do NOT change this, to keep consistency. --- src/telebot-core.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/telebot-core.c b/src/telebot-core.c index 2519e04..0c1150f 100644 --- a/src/telebot-core.c +++ b/src/telebot-core.c @@ -23,9 +23,9 @@ #include #include #include -#include "telebot-private.h" -#include "telebot-common.h" -#include "telebot-core.h" +#include +#include +#include void telebot_core_put_response(telebot_core_response_t *response) { From aa5acf081f0292667aed10f1a3e79c3d4bbdcd3d Mon Sep 17 00:00:00 2001 From: ozlb Date: Tue, 30 Jan 2024 00:10:25 +0100 Subject: [PATCH 6/6] Update telebot.c Please remove custom comments --- src/telebot.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/telebot.c b/src/telebot.c index b2f01b7..aaa940b 100644 --- a/src/telebot.c +++ b/src/telebot.c @@ -1968,10 +1968,6 @@ static void telebot_put_callback_query(telebot_callback_query_t *query) //TODO: static void telebot_put_payment(telebot_successful_payment_t *payment); //TODO: static void telebot_put_game(telebot_game_t *game); -//////////////////////////////////////////////////////////////////////////////////////// -//ozlb -//////////////////////////////////////////////////////////////////////////////////////// - telebot_error_e telebot_set_my_short_description(telebot_handler_t handle, const char *short_description) {