How to post a photo to Telegram api?
Posted: Sun Nov 05, 2023 10:14 am
Hello, I am using ESP32-CAM board and trying to post a photo to Telegram api. I've tried to send a text message and take a picture. They are working. But I can not send a photo to api. It gives a response like below:
And here is my post a photo code below:
I've stucked to the same error by days. Thanks for your attention.
- I (6517) Sending sendMessage: Starting Taking Picture!
- I (6517) Sending sendMessage: pic->len=7119
- W (6527) Sending sendMessage: Iniciare
- W (6527) Sending sendMessage: Enviare POST
- I (8097) Sending sendMessage: HTTP POST Status = 400, content_length = 91
- W (8107) Sending sendMessage: Desde Perform el output es: {"ok":false,"error_code":400,"description":"Bad Request: there is no photo in the request"}
- static esp_err_t https_telegram_sendPicture_perform_post()
- {
- ESP_LOGI(TAG3, "Starting Taking Picture!");
- camera_fb_t *pic = esp_camera_fb_get();
- if (!pic)
- {
- ESP_LOGE(TAG3, "Camera capture failed");
- return ESP_FAIL;
- }
- else
- {
- ESP_LOGI(TAG3, "pic->len=%d", pic->len);
- }
- char url[512] = "";
- char output_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0}; // Buffer to store response of http request
- // char output_buffer[1024*10] = {0}; // Buffer to store response of http request
- esp_http_client_config_t config =
- {
- .url = "https://api.telegram.org",
- .transport_type = HTTP_TRANSPORT_OVER_SSL,
- .event_handler = _http_event_handler,
- .cert_pem = telegram_certificate_pem_start,
- .user_data = output_buffer,
- };
- //POST
- ESP_LOGW(TAG3, "Iniciare");
- esp_http_client_handle_t client = esp_http_client_init(&config)
- //Copy the url+TOKEN
- strcat(url, url_string);
- //Passing the method
- strcat(url, "/sendPhoto");
- esp_http_client_set_url(client, url);
- ESP_LOGW(TAG3, "Enviare POST");
- char post_data[1024*10] = "";
- sprintf(post_data,"{\"chat_id\":%s,\"photo\":%s}", chat_ID2, (const char *) pic->buf);
- esp_http_client_set_method(client, HTTP_METHOD_POST);
- esp_http_client_set_header(client, "Content-Type", "image/jpeg");
- esp_http_client_set_post_field(client, (const char*) post_data, strlen(post_data));
- esp_err_t err = esp_http_client_perform(client);
- if (err == ESP_OK)
- {
- ESP_LOGI(TAG3, "HTTP POST Status = %ld, content_length = %ld",
- (long)esp_http_client_get_status_code(client),
- (long)esp_http_client_get_content_length(client));
- ESP_LOGW(TAG3, "Desde Perform el output es: %s",output_buffer);
- }
- else
- {
- ESP_LOGE(TAG3, "HTTP POST request failed: %s", esp_err_to_name(err));
- }
- ESP_LOGW(TAG, "Limpiare");
- esp_http_client_close(client);
- esp_http_client_cleanup(client);
- ESP_LOGI(TAG3, "esp_get_free_heap_size: %lu", esp_get_free_heap_size ());
- esp_camera_fb_return(pic);
- return ESP_OK;
- }