2323#include <stdio.h>
2424#include <stdlib.h>
2525#include <string.h>
26+ #include <json.h>
27+ #include <json_object.h>
2628#include <telebot-common.h>
2729#include <telebot-core.h>
2830#include <telebot-private.h>
@@ -65,12 +67,13 @@ telebot_core_create(telebot_core_handler_t *core_h, const char *token)
6567
6668 * core_h = NULL ;
6769
68- telebot_core_handler_t _core_h = malloc (sizeof (telebot_core_handler_t ));
70+ telebot_core_handler_t _core_h = malloc (sizeof (struct telebot_core_handler ));
6971 if (_core_h == NULL )
7072 {
7173 ERR ("Failed to allocate memory" );
7274 return TELEBOT_ERROR_OUT_OF_MEMORY ;
7375 }
76+
7477 _core_h -> token = strdup (token );
7578 if (_core_h -> token == NULL )
7679 {
@@ -1131,15 +1134,15 @@ static const char *telebot_core_get_media_type(const char *filename)
11311134}
11321135
11331136telebot_core_response_t
1134- telebot_core_send_media_group (telebot_core_handler_t core_h , long long int chat_id , char * media_paths [], int count ,
1137+ telebot_core_send_media_group (telebot_core_handler_t core_h , long long int chat_id , char * media_paths [], int path_count ,
11351138 bool disable_notification , int reply_to_message_id )
11361139{
11371140 CHECK_ARG_NULL (media_paths );
1138- CHECK_ARG_CONDITION (count <= 0 , "Invalid media path count, should be greater than 0" );
1139- CHECK_ARG_CONDITION (count > 10 , "Invalid media path count, should be less than or equal to 10" )
1141+ CHECK_ARG_CONDITION (path_count <= 0 , "Invalid media path count, should be greater than 0" );
1142+ CHECK_ARG_CONDITION (path_count > 10 , "Invalid media path count, should be less than or equal to 10" )
11401143
11411144 // Validate all media paths are non-NULL
1142- for (int i = 0 ; i < count ; ++ i )
1145+ for (int i = 0 ; i < path_count ; ++ i )
11431146 {
11441147 if (media_paths [i ] == NULL )
11451148 {
@@ -1157,7 +1160,7 @@ telebot_core_send_media_group(telebot_core_handler_t core_h, long long int chat_
11571160 }
11581161
11591162 // Allocate memory for filenames
1160- char * * filenames = calloc (count , sizeof (char * ));
1163+ char * * filenames = calloc (path_count , sizeof (char * ));
11611164 if (filenames == NULL )
11621165 {
11631166 json_object_put (media_array );
@@ -1166,10 +1169,10 @@ telebot_core_send_media_group(telebot_core_handler_t core_h, long long int chat_
11661169 }
11671170
11681171 // Determine media types for validation
1169- const char * * media_types = calloc (count , sizeof (char * ));
1172+ const char * * media_types = calloc (path_count , sizeof (char * ));
11701173 if (media_types == NULL )
11711174 {
1172- for (int i = 0 ; i < count ; i ++ )
1175+ for (int i = 0 ; i < path_count ; i ++ )
11731176 {
11741177 free (filenames [i ]);
11751178 }
@@ -1180,7 +1183,7 @@ telebot_core_send_media_group(telebot_core_handler_t core_h, long long int chat_
11801183 ;
11811184 }
11821185
1183- for (int i = 0 ; i < count ; ++ i )
1186+ for (int i = 0 ; i < path_count ; ++ i )
11841187 {
11851188 // Extract filename from path using basename
11861189 const char * filename = basename (media_paths [i ]);
@@ -1209,7 +1212,7 @@ telebot_core_send_media_group(telebot_core_handler_t core_h, long long int chat_
12091212 // Validate media group composition
12101213 // Count unique types in the group
12111214 int photo_count = 0 , video_count = 0 , audio_count = 0 , document_count = 0 ;
1212- for (int i = 0 ; i < count ; i ++ )
1215+ for (int i = 0 ; i < path_count ; i ++ )
12131216 {
12141217 if (strcmp (media_types [i ], "photo" ) == 0 )
12151218 photo_count ++ ;
@@ -1226,7 +1229,7 @@ telebot_core_send_media_group(telebot_core_handler_t core_h, long long int chat_
12261229 // 2. Mixed photo and video only
12271230 bool valid_combination = false;
12281231
1229- if (photo_count == count || video_count == count || audio_count == count || document_count == count )
1232+ if (photo_count == path_count || video_count == path_count || audio_count == path_count || document_count == path_count )
12301233 {
12311234 // All same type - valid
12321235 valid_combination = true;
@@ -1240,7 +1243,7 @@ telebot_core_send_media_group(telebot_core_handler_t core_h, long long int chat_
12401243 if (!valid_combination )
12411244 {
12421245 // Free allocated resources
1243- for (int i = 0 ; i < count ; i ++ )
1246+ for (int i = 0 ; i < path_count ; i ++ )
12441247 {
12451248 free (filenames [i ]);
12461249 }
@@ -1252,7 +1255,7 @@ telebot_core_send_media_group(telebot_core_handler_t core_h, long long int chat_
12521255 }
12531256
12541257 // Create JSON objects for media array
1255- for (int i = 0 ; i < count ; ++ i )
1258+ for (int i = 0 ; i < path_count ; ++ i )
12561259 {
12571260 struct json_object * item = json_object_new_object ();
12581261 json_object_object_add (item , "type" , json_object_new_string (media_types [i ]));
@@ -1271,7 +1274,7 @@ telebot_core_send_media_group(telebot_core_handler_t core_h, long long int chat_
12711274 if (media_json_str == NULL )
12721275 {
12731276 // Free allocated filenames
1274- for (int i = 0 ; i < count ; i ++ )
1277+ for (int i = 0 ; i < path_count ; i ++ )
12751278 {
12761279 free (filenames [i ]);
12771280 }
@@ -1283,49 +1286,49 @@ telebot_core_send_media_group(telebot_core_handler_t core_h, long long int chat_
12831286
12841287 // Prepare MIME parts
12851288 telebot_core_mime_t mimes [20 ]; // max: chat_id + media + disable_notif + reply_id + 10 files
1286- int index = 0 ;
1289+ int count = 0 ;
12871290
12881291 // chat_id
1289- mimes [index ].name = "chat_id" ;
1290- mimes [index ].type = TELEBOT_MIME_TYPE_LONG_LONG_INT ;
1291- mimes [index ].data .lld = chat_id ;
1292- ++ index ;
1292+ mimes [count ].name = "chat_id" ;
1293+ mimes [count ].type = TELEBOT_MIME_TYPE_LONG_LONG_INT ;
1294+ mimes [count ].data .lld = chat_id ;
1295+ count ++ ;
12931296
12941297 // media (JSON string)
1295- mimes [index ].name = "media" ;
1296- mimes [index ].type = TELEBOT_MIME_TYPE_STRING ;
1297- mimes [index ].data .s = media_json_str ;
1298- ++ index ;
1298+ mimes [count ].name = "media" ;
1299+ mimes [count ].type = TELEBOT_MIME_TYPE_STRING ;
1300+ mimes [count ].data .s = media_json_str ;
1301+ count ++ ;
12991302
13001303 // disable_notification
1301- mimes [index ].name = "disable_notification" ;
1302- mimes [index ].type = TELEBOT_MIME_TYPE_STRING ;
1303- mimes [index ].data .s = disable_notification ? "true" : "false" ;
1304- ++ index ;
1304+ mimes [count ].name = "disable_notification" ;
1305+ mimes [count ].type = TELEBOT_MIME_TYPE_STRING ;
1306+ mimes [count ].data .s = disable_notification ? "true" : "false" ;
1307+ count ++ ;
13051308
13061309 // reply_to_message_id (optional)
13071310 if (reply_to_message_id > 0 )
13081311 {
1309- mimes [index ].name = "reply_to_message_id" ;
1310- mimes [index ].type = TELEBOT_MIME_TYPE_INT ;
1311- mimes [index ].data .d = reply_to_message_id ;
1312- ++ index ;
1312+ mimes [count ].name = "reply_to_message_id" ;
1313+ mimes [count ].type = TELEBOT_MIME_TYPE_INT ;
1314+ mimes [count ].data .d = reply_to_message_id ;
1315+ count ++ ;
13131316 }
13141317
13151318 // Attach actual photo files using the correct names
1316- for (int i = 0 ; i < count ; ++ i )
1319+ for (int i = 0 ; i < path_count ; ++ i )
13171320 {
1318- mimes [index ].name = filenames [i ]; // Use actual filename instead of generated name
1319- mimes [index ].type = TELEBOT_MIME_TYPE_FILE ;
1320- mimes [index ].data .s = media_paths [i ];
1321- ++ index ;
1321+ mimes [count ].name = filenames [i ]; // Use actual filename instead of generated name
1322+ mimes [count ].type = TELEBOT_MIME_TYPE_FILE ;
1323+ mimes [count ].data .s = media_paths [i ];
1324+ count ++ ;
13221325 }
13231326
13241327 // Perform request
1325- telebot_core_response_t response = telebot_core_curl_perform (core_h , TELEBOT_METHOD_SEND_MEDIA_GROUP , mimes , index );
1328+ telebot_core_response_t response = telebot_core_curl_perform (core_h , TELEBOT_METHOD_SEND_MEDIA_GROUP , mimes , count );
13261329
13271330 // Clean up allocated filenames
1328- for (int i = 0 ; i < count ; i ++ )
1331+ for (int i = 0 ; i < path_count ; i ++ )
13291332 {
13301333 free (filenames [i ]);
13311334 }
0 commit comments