# Copyright (C) 2024-2026 Maria Lisina # Copyright (C) 2024-2026 Danil Lisin # SPDX-License-Identifier: Apache-2.0 if [[ -z "${file_id}" ]] then message_id="$(jq -r '.message.reply_to_message.message_id' <<< "${update}")" file_id="$(jq -r '.message.reply_to_message.photo.[1].file_id' <<< "${update}")" if [[ "${message_id}" == "null" || "${message_id}" == "${message_thread_id}" ]] then output_text="You must use this command in reply to an image" return fi if [[ "${file_id}" == "null" ]] then output_text="Could not find image in replied message" return fi fi if ! output_data="$( curl --connect-timeout ${connrefused_timeout} \ --data-urlencode "file_id=${file_id}" \ --get \ --max-time ${internal_timeout} \ --proxy "${internal_proxy}" \ --retry 1 \ --retry-connrefused \ --retry-max-time $((external_timeout - connrefused_timeout)) \ --silent \ --user-agent "${useragent}" \ "${api_address}/bot${api_token}/getFile" )" then output_text="Failed to get image file" log_text="getFile (${update_id}): Failed to access Telegram Bot API" source "${units}/log.zsh" return fi if ! jq -e '.' <<< "${output_data}" > /dev/null then output_text="An unknown error occurred" log_text="getFile (${update_id}): An unknown error occurred" source "${units}/log.zsh" return fi if [[ "$(jq -r '.ok' <<< "${output_data}")" != "true" ]] then output_text="Failed to get image file" error_description="$(jq -r '.description' <<< "${output_data}")" if [[ "${error_description}" != "null" ]] then log_text="getFile (${update_id}): ${error_description}" else log_text="getFile (${update_id}): An unknown error occurred" fi source "${units}/log.zsh" return fi file_path="$(jq -r '.result.file_path' <<< "${output_data}")" until mkdir "${cache}/${update_id}_file.lock" do sleep 1 done if [[ "${api_address}" != "${local_address}" ]] then output_file="${cache}/${update_id}_file.jpg" if ! curl --connect-timeout ${connrefused_timeout} \ --max-time ${internal_timeout} \ --output "${output_file}" \ --proxy "${internal_proxy}" \ --retry 1 \ --retry-connrefused \ --retry-max-time $((external_timeout - connrefused_timeout)) \ --silent \ --user-agent "${useragent}" \ "${api_address}/file/bot${api_token}/${file_path}" then output_text="Failed to download image file" log_text="getFile (${update_id}): Failed to access Telegram Bot API" source "${units}/log.zsh" return fi if [[ "$(jq -r '.ok' "${output_file}")" == "false" ]] then output_text="Failed to download image file" error_description="$(jq -r '.description' "${output_file}")" if [[ "${error_description}" != "null" ]] then log_text="getFile (${update_id}): ${error_description}" else log_text="getFile (${update_id}): An unknown error occurred" fi source "${units}/log.zsh" return fi file_path="${output_file}" fi sn_query="file=@${file_path}" source "${units}/sn_search.zsh" rmdir "${cache}/${update_id}_file.lock"