MCPcopy Index your code
hub / github.com/antirez/botlib / botSendImage

Function botSendImage

botlib.c:337–395  ·  view source on GitHub ↗

Send an image using the sendPhoto endpoint. Return 1 on success, 0 * on error. */

Source from the content-addressed store, hash-verified

335/* Send an image using the sendPhoto endpoint. Return 1 on success, 0
336 * on error. */
337int botSendImage(int64_t target, char *filename) {
338 CURL *curl;
339 CURLcode res;
340 int retval = 0;
341 struct curl_httppost *formpost = NULL;
342 struct curl_httppost *lastptr = NULL;
343
344 /* Build the POST form to submit. */
345 sds strtarget = sdsfromlonglong(target);
346 curl_formadd(&formpost, &lastptr,
347 CURLFORM_COPYNAME, "chat_id",
348 CURLFORM_COPYCONTENTS, strtarget,
349 CURLFORM_END);
350 sdsfree(strtarget);
351
352 curl_formadd(&formpost, &lastptr,
353 CURLFORM_COPYNAME, "photo",
354 CURLFORM_FILE, filename,
355 CURLFORM_END);
356
357 curl = curl_easy_init();
358 if (curl) {
359 char url[1024];
360 snprintf(url, sizeof(url),
361 "https://api.telegram.org/bot%s/sendPhoto", Bot.apikey);
362 curl_easy_setopt(curl, CURLOPT_URL, url);
363 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
364 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, makeHTTPGETCallWriterSDS);
365 sds body = sdsempty(); // Accumulate the reply here.
366 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body);
367 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
368 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1L);
369 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 15);
370 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 15);
371
372 /* Perform the request, res will get the return code */
373 res = curl_easy_perform(curl);
374
375 /* Check for errors */
376 if (res == CURLE_OK) {
377 retval = 1;
378 /* Return 0 if the request worked but returned a 500 code. */
379 long code;
380 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
381 if (code == 500 || code == 400) retval = 0;
382 } else {
383 retval = 0;
384 }
385
386 if (retval == 0)
387 printf("sendImage() error from Telegram API: %s\n", body);
388 sdsfree(body);
389
390 /* always cleanup */
391 curl_easy_cleanup(curl);
392 }
393 curl_formfree(formpost);
394 return retval;

Callers

nothing calls this directly

Calls 3

sdsfromlonglongFunction · 0.85
sdsfreeFunction · 0.85
sdsemptyFunction · 0.85

Tested by

no test coverage detected