smartnode / telebot

Telegram Bot API in C
https://elmurod.net/telebot
Apache License 2.0
162 stars 38 forks source link

Download image #7

Closed dlinyj closed 6 years ago

dlinyj commented 7 years ago

I need to download the transmitted picture. I wrote the following small code:

    if (message->photo[1].file_size > 0) {
        char file_id[TELEBOT_FILE_ID_SIZE];
        strcpy(file_id,message->photo[1].file_id);
        ret = telebot_download_file(file_id, "photos/file_7.jpg");
        if (ret != TELEBOT_ERROR_NONE) {
            printf("Failed to download file: %d \n", ret);
        }
    }

But it does not work, it returns -1. In the process of debugging, it was revealed that a variable file_path was not obtained in the function of the telebot_error_e telebot_download_file(char file_id, char path).

ret = telebot_parser_get_file_path(obj, &file_path);
json_object_put(obj);

if (file_path == NULL) {
    printf("if (file_path == NULL) {\n");
    return TELEBOT_ERROR_OPERATION_FAILED;
}

Specifically, the problem in this function call:

telebot_error_e telebot_parser_get_file_path(struct json_object *obj, char **path) {

if (json_object_object_get_ex (obj, "file_path", &file_path)) {
    *path = strdup(json_object_get_string(file_path));
    json_object_put (file_path);
}
else {
    *path = NULL;
    return TELEBOT_ERROR_OPERATION_FAILED;
}

}

This function returns TELEBOT_ERROR_OPERATION_FAILED. The system displays the following debugging messages:

[DEBUG][telebot_core_curl_perform:100]Response: {"ok":true,"result":[{"update_id":940457749, "message":{"message_id":163,"from":{"id":183246257,"is_bot":false,"first_name":"\u0421\u0435\u0440\u0433\u0435\u0439","last_name":"\u0414\u043e\u043b\u0438\u043d","username":"dlinyj","language_code":"ru"},"chat":{"id":183246257,"first_name":"\u0421\u0435\u0440\u0433\u0435\u0439","last_name":"\u0414\u043e\u043b\u0438\u043d","username":"dlinyj","type":"private"},"date":1504612315,"photo":[{"file_id":"AgADAgADEKgxG9_kcUkSKsR2nOYdSvfrDw4ABPxIpZ1qrqh9gTgAAgI","file_size":704,"width":90,"height":15},{"file_id":"AgADAgADEKgxG9_kcUkSKsR2nOYdSvfrDw4ABPioQiHmnf3cgjgAAgI","file_size":5111,"width":320,"height":52},{"file_id":"AgADAgADEKgxG9_kcUkSKsR2nOYdSvfrDw4ABAHAh8adkcv3gzgAAgI","file_size":20592,"width":800,"height":129},{"file_id":"AgADAgADEKgxG9_kcUkSKsR2nOYdSvfrDw4ABP7c6v1gPEDDgDgAAgI","file_size":42989,"width":1280,"height":206}]}}]} file_id = AgADAgADEKgxG9_kcUkSKsR2nOYdSvfrDw4ABPioQiHmnf3cgjgAAgI [DEBUG][telebot_core_curl_perform:100]Response: {"ok":true,"result":{"file_id":"AgADAgADEKgxG9_kcUkSKsR2nOYdSvfrDw4ABPioQiHmnf3cgjgAAgI","file_size":5111,"file_path":"photos/file_14.jpg"}} Failed to download file: -1 [DEBUG][telebot_core_curl_perform:100]Response: {"ok":true,"result":{"message_id":164,"from":{"id":405310886,"is_bot":true,"first_name":"hipcrt_bot","username":"hipcrt_bot"},"chat":{"id":183246257,"first_name":"\u0421\u0435\u0440\u0433\u0435\u0439","last_name":"\u0414\u043e\u043b\u0438\u043d","username":"dlinyj","type":"private"},"date":1504612316,"photo":[{"file_id":"AgADAgAD5KcxG568eUn9Ofw4oE4jWP88Sw0ABAueH2XiToz4NFEPAAEC","file_size":1769,"file_path":"photos/file_12.jpg","width":90,"height":90},{"file_id":"AgADAgAD5KcxG568eUn9Ofw4oE4jWP88Sw0ABLEyzOcAAQyVyDVRDwABAg","file_size":13258,"width":320,"height":320},{"file_id":"AgADAgAD5KcxG568eUn9Ofw4oE4jWP88Sw0ABOHzFRhiJI_CM1EPAAEC","file_size":15608,"width":400,"height":400}]}} [DEBUG][telebot_core_curl_perform:100]Response: {"ok":true,"result":{"message_id":165,"from":{"id":405310886,"is_bot":true,"first_name":"hipcrt_bot","username":"hipcrt_bot"},"chat":{"id":183246257,"first_name":"\u0421\u0435\u0440\u0433\u0435\u0439","last_name":"\u0414\u043e\u043b\u0438\u043d","username":"dlinyj","type":"private"},"date":1504612316,"text":"getphoto"}}

What am I doing wrong?

smartnode commented 6 years ago

Thanks for reporting this bug. I have fixed it in the following commit. https://github.com/smartnode/telebot/commit/0b67c259d1c38a1233c2e448fea8636e1cd8f4a8