# shellcheck shell=sh disable=SC2039,SC3043,SC2154


___x_cmd_telegram_bot___send_json() {
    local req_json ; req_json="$(___x_cmd_cmds_cat)"
    telegram:debug "$(printf "%s" "$req_json" | ___x_cmd jo fmt)"

    [ -n "$token" ] || {
        local token
        ___x_cmd_telegram_cur token:= 2>/dev/null
        [ -n "$token" ] || N=telegram M="Please setting up your token first ==> 'x telegram --cfg token=<your token>'" log:ret:1
    }

    local proxy
    ___x_cmd_telegram_cur proxy:= 2>/dev/null

    # telegram:debug "$webhook/$msg_type"
    ___x_cmd curl -X POST ${proxy:+-x} ${proxy:+"$proxy"}   \
                  -H 'Content-Type: application/json'       \
                  -d "$req_json"                            \
                  "https://api.telegram.org/bot${token}/${msg_type}"
}

___x_cmd_telegram_bot___send_file() {
    local file_type="$1"
    [ -n "$token" ] || {
        local token
        ___x_cmd_telegram_cur token:= 2>/dev/null
        [ -n "$token" ] || N=telegram M="Please setting up your token first ==> 'x telegram --cfg token=<your token>'" log:ret:1
    }
    # send file
    local proxy
    ___x_cmd_telegram_cur proxy:= 2>/dev/null

    [ -f "$file_path" ] || N=telegram M="File not found ==> $file_path" log:ret:1

    # telegram:debug "$token/$msg_type"
    ___x_cmd curl -X POST ${proxy:+-x} ${proxy:+"$proxy"}   \
                    -H 'Content-Type:multipart/form-data'   \
                    -F "chat_id=${chat_id}"                 \
                    -F "${file_type}=@${file_path}"         \
                    "https://api.telegram.org/bot${token}/${msg_type}"
}

___x_cmd_telegram_bot___get_chat_() {
    [ -n "$chat" ] || {
        ___x_cmd_telegram_cur chat:= 2>/dev/null
        [ -n "$chat" ] || N=telegram M="Please provide the chat ID" log:ret:1
    }
    x_="$chat"
}



___x_cmd_telegram_bot___get_mobiles() {
    # TODO: tolerance.
    printf "%s" "$@" | ___x_cmd_cmds_awk '{ gsub(/,/, "\",\"", $0); print "\"" $0 "\"" }'
}
