Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get/set bot short_description #59

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions include/telebot-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,27 @@ telebot_error_e telebot_core_delete_message(telebot_core_handler_t *core_h,
*/
void telebot_core_put_response(telebot_core_response_t *response);

/**
* @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
*/
Expand Down
26 changes: 26 additions & 0 deletions include/telebot-methods.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand Down
3 changes: 3 additions & 0 deletions include/telebot-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
41 changes: 41 additions & 0 deletions src/telebot-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -2628,3 +2628,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);
}
84 changes: 80 additions & 4 deletions src/telebot.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
#include <errno.h>
#include <json.h>
#include <json_object.h>
#include <telebot.h>
#include <telebot-core.h>
#include <telebot-private.h>
#include <telebot-parser.h>
#include "telebot.h"
#include "telebot-core.h"
#include "telebot-private.h"
#include "telebot-parser.h"

typedef struct telebot_handler_s
{
Expand Down Expand Up @@ -1967,3 +1967,79 @@ 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);

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)
{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this code should be in parser.

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;
}