Skip to content

Commit 1140fcd

Browse files
committed
fix: handle empty thumbnail strings and correct video thumb check
- treat "" same as NULL for thumb parameter in core send routines (audio, document, animation, video, video_note) to avoid curl CURLE_READ_ERROR (26) when uploading with no thumbnail. - add comments documenting optional thumb behavior. - fix typo in send_video: was comparing thumb[0] != '0', now proper '\0'. - update tests/helpers to exercise media sending paths. Signed-off-by: Elmurod Talipov <elmurod.talipov@gmail.com>
1 parent 6f5b93a commit 1140fcd

7 files changed

Lines changed: 230 additions & 201 deletions

File tree

CMakeLists.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ SET(SRCS
2121
src/telebot-games.c
2222
)
2323

24-
ADD_DEFINITIONS("-DDEBUG=0")
2524
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include)
2625
SET(DEPENDENTS "libcurl json-c")
2726
INCLUDE(FindPkgConfig)
@@ -30,7 +29,16 @@ pkg_check_modules(PKGS REQUIRED ${DEPENDENTS})
3029
FOREACH(flag ${PKGS_CFLAGS})
3130
SET(EXTRA_LIB_CFLAGS "${EXTRA_LIB_CFLAGS} ${flag}")
3231
ENDFOREACH(flag)
33-
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_LIB_CFLAGS} -Werror -Wall -Wno-unused-function -O2" )
32+
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_LIB_CFLAGS} -Werror -Wall -Wno-unused-function" )
33+
34+
#Debug option
35+
IF(DEBUG)
36+
MESSAGE("Build debug version")
37+
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g")
38+
ADD_DEFINITIONS("-DDEBUG")
39+
ELSE(DEBUG)
40+
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2")
41+
ENDIF(DEBUG)
3442

3543
# libtelebot
3644
ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS})

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,25 @@ To build the library run following commands:
2828

2929
```sh
3030
cd [your repository]
31-
mkdir -p Build && cd Build
32-
cmake ../
31+
mkdir -p build && cd build
32+
cmake ..
3333
make
3434
```
3535

36+
For debug build
37+
```sh
38+
mkdir -p build && cd build
39+
cmake .. -DDEBUG=1
40+
make
41+
```
42+
43+
## Testing
44+
45+
Place Telegam Bot API Token in .toke file in the root folder of the repository, and run as below
46+
```sh
47+
./build/test/testbot
48+
```
49+
3650
<details>
3751
<summary>Sample</summary>
3852

include/telebot-private.h

Lines changed: 166 additions & 167 deletions
Large diffs are not rendered by default.

src/telebot-core.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ telebot_core_send_audio(telebot_core_handler_t core_h, long long int chat_id, co
664664
count++;
665665
}
666666

667-
if (thumb != NULL)
667+
if (thumb != NULL && thumb[0] != '\0')
668668
{
669669
mimes[count].name = "thumb";
670670
mimes[count].type = TELEBOT_MIME_TYPE_FILE;
@@ -715,7 +715,7 @@ telebot_core_send_document(telebot_core_handler_t core_h, long long int chat_id,
715715
mimes[count].data.s = document;
716716
count++;
717717

718-
if (thumb != NULL)
718+
if (thumb != NULL && thumb[0] != '\0')
719719
{
720720
mimes[count].name = "thumb";
721721
mimes[count].type = TELEBOT_MIME_TYPE_FILE;
@@ -806,7 +806,7 @@ telebot_core_send_video(telebot_core_handler_t core_h, long long int chat_id, co
806806
count++;
807807
}
808808

809-
if (thumb != NULL)
809+
if (thumb != NULL && thumb[0] != '\0')
810810
{
811811
mimes[count].name = "thumb";
812812
mimes[count].type = TELEBOT_MIME_TYPE_FILE;
@@ -902,7 +902,7 @@ telebot_core_send_animation(telebot_core_handler_t core_h, long long int chat_id
902902
count++;
903903
}
904904

905-
if (thumb != NULL)
905+
if (thumb != NULL && thumb[0] != '0')
906906
{
907907
mimes[count].name = "thumb";
908908
mimes[count].type = TELEBOT_MIME_TYPE_FILE;
@@ -1051,7 +1051,7 @@ telebot_core_send_video_note(telebot_core_handler_t core_h, long long int chat_i
10511051
count++;
10521052
}
10531053

1054-
if (thumb != NULL)
1054+
if (thumb != NULL && thumb[0] != '\0')
10551055
{
10561056
mimes[count].name = "thumb";
10571057
mimes[count].type = TELEBOT_MIME_TYPE_FILE;

src/telebot-parser.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -649,16 +649,6 @@ telebot_error_e telebot_parser_get_chat(struct json_object *obj, telebot_chat_t
649649
if (json_object_object_get_ex(obj, "invite_link", &invite_link))
650650
chat->invite_link = TELEBOT_SAFE_STRDUP(json_object_get_string(invite_link));
651651

652-
struct json_object *pinned_message = NULL;
653-
if (json_object_object_get_ex(obj, "pinned_message", &pinned_message))
654-
{
655-
chat->pinned_message = calloc(1, sizeof(telebot_message_t));
656-
if (telebot_parser_get_message(pinned_message, chat->pinned_message) != TELEBOT_ERROR_NONE)
657-
{
658-
ERR("Failed to get <pinned_message> from chat object");
659-
TELEBOT_SAFE_FREE(chat->pinned_message);
660-
}
661-
}
662652

663653
struct json_object *permissions = NULL;
664654
if (json_object_object_get_ex(obj, "permissions", &permissions))

src/telebot.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,23 @@ static const char *telebot_update_type_str[TELEBOT_UPDATE_TYPE_MAX] = {
3737
"edited_message",
3838
"channel_post",
3939
"edited_channel_post",
40+
"business_connection",
41+
"business_message",
42+
"edited_business_message",
43+
"deleted_business_messages",
44+
"message_reaction",
45+
"message_reaction_count",
4046
"inline_query",
4147
"chosen_inline_result",
4248
"callback_query",
4349
"shipping_query",
4450
"pre_checkout_query",
51+
"purchased_paid_media",
4552
"poll",
4653
"poll_answer",
4754
"my_chat_member",
4855
"chat_member",
4956
"chat_join_request",
50-
"message_reaction",
51-
"message_reaction_count",
5257
"chat_boost",
5358
"removed_chat_boost"};
5459

@@ -1945,6 +1950,9 @@ static void telebot_put_chat_permissions(telebot_chat_permissions_t *permissions
19451950

19461951
static void telebot_put_chat_location(telebot_chat_location_t *chat_location)
19471952
{
1953+
if (chat_location == NULL)
1954+
return;
1955+
19481956
TELEBOT_SAFE_FREE(chat_location->address);
19491957
telebot_put_location(chat_location->location);
19501958
}

test/testbot.c

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))
1010

1111
/* Local sample media paths */
12-
#define SAMPLE_PHOTO "samples/logo.png"
13-
#define SAMPLE_AUDIO "samples/audio.mp3"
14-
#define SAMPLE_VIDEO "samples/video.mp4"
15-
#define SAMPLE_DOC "samples/document.md"
16-
#define SAMPLE_ANIMATION "samples/animation.gif"
17-
#define SAMPLE_VOICE "samples/voice.wav"
12+
#define SAMPLE_PHOTO "test/samples/logo.png"
13+
#define SAMPLE_AUDIO "test/samples/audio.mp3"
14+
#define SAMPLE_VIDEO "test/samples/video.mp4"
15+
#define SAMPLE_DOC "test/samples/document.md"
16+
#define SAMPLE_ANIMATION "test/samples/animation.gif"
17+
#define SAMPLE_VOICE "test/samples/voice.wav"
1818

1919
void setup_commands(telebot_handler_t handle)
2020
{
@@ -70,7 +70,7 @@ void handle_message(telebot_handler_t handle, telebot_message_t *message)
7070
"{\"text\":\"Document\",\"callback_data\":\"media_doc\"}],"
7171
"[{\"text\":\"Animation\",\"callback_data\":\"media_animation\"},"
7272
"{\"text\":\"Voice\",\"callback_data\":\"media_voice\"}]]}";
73-
ret = telebot_send_message(handle, message->chat->id, "Select media to send (from local files):", "", false, false, 0, keyboard);
73+
ret = telebot_send_message(handle, message->chat->id, "Select media to send:", "", false, false, 0, keyboard);
7474
}
7575
else if (strstr(message->text, "/keyboard"))
7676
{
@@ -149,29 +149,39 @@ void handle_callback_query(telebot_handler_t handle, telebot_callback_query_t *q
149149
telebot_error_e ret = TELEBOT_ERROR_NONE;
150150
long long int chat_id = query->message->chat->id;
151151

152+
int duration = 0;
153+
int width = 0;
154+
int height = 0;
155+
const char *thumb = NULL;
156+
bool disable_notification = false;
157+
int reply_to_message_id = 0;
158+
const char *reply_markup = NULL;
159+
const char *parse_mode = "";
160+
bool is_file = true;
161+
152162
if (strcmp(query->data, "media_photo") == 0)
153163
{
154-
ret = telebot_send_photo(handle, chat_id, SAMPLE_PHOTO, true, "Test Photo from local file", "", false, 0, "");
164+
ret = telebot_send_photo(handle, chat_id, SAMPLE_PHOTO, is_file, "Test Photo", parse_mode, disable_notification, reply_to_message_id, reply_markup);
155165
}
156166
else if (strcmp(query->data, "media_audio") == 0)
157167
{
158-
ret = telebot_send_audio(handle, chat_id, SAMPLE_AUDIO, true, "Test Audio from local file", "", 0, "Artist", "Title", "", false, 0, "");
168+
ret = telebot_send_audio(handle, chat_id, SAMPLE_AUDIO, is_file, "Test Audio", parse_mode, duration, "Artist", "Title", parse_mode, disable_notification, reply_to_message_id, reply_markup);
159169
}
160170
else if (strcmp(query->data, "media_video") == 0)
161171
{
162-
ret = telebot_send_video(handle, chat_id, SAMPLE_VIDEO, true, 0, 0, 0, "", "Test Video from local file", "", false, false, 0, "");
172+
ret = telebot_send_video(handle, chat_id, SAMPLE_VIDEO, is_file, duration, width, height, thumb, "Test Video", parse_mode, disable_notification, false, reply_to_message_id, reply_markup);
163173
}
164174
else if (strcmp(query->data, "media_doc") == 0)
165175
{
166-
ret = telebot_send_document(handle, chat_id, SAMPLE_DOC, true, "", "Test Document from local file", "", false, 0, "");
176+
ret = telebot_send_document(handle, chat_id, SAMPLE_DOC, is_file, thumb, "Test Document", parse_mode, disable_notification, reply_to_message_id, reply_markup);
167177
}
168178
else if (strcmp(query->data, "media_animation") == 0)
169179
{
170-
ret = telebot_send_animation(handle, chat_id, SAMPLE_ANIMATION, true, 0, 0, 0, "", "Test Animation from local file", "", false, 0, "");
180+
ret = telebot_send_animation(handle, chat_id, SAMPLE_ANIMATION, is_file, duration, width, height, thumb, "Test Animation", parse_mode, disable_notification, reply_to_message_id, reply_markup);
171181
}
172182
else if (strcmp(query->data, "media_voice") == 0)
173183
{
174-
ret = telebot_send_voice(handle, chat_id, SAMPLE_VOICE, true, "Test Voice from local file", "", 0, false, 0, "");
184+
ret = telebot_send_voice(handle, chat_id, SAMPLE_VOICE, is_file, "Test Voice", parse_mode, duration, disable_notification, reply_to_message_id, reply_markup);
175185
}
176186

177187
/* Acknowledge callback query */

0 commit comments

Comments
 (0)