es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

Cómo enviar una foto desde la API del bot de Telegram (JavaScript)

Tengo un problema con el envío de la API a través de mensajes de texto de Telegram. Los mensajes llegan, pero las imágenes no están llegando y no entiendo dónde está el problema. ¿Es posible modificar este código y hacerlo enviar imágenes?

var telegram_bot_id = "api";
var chat_id = "id";
var img;
var ready = function () {
    img = document.getElementById("photo").value;
    message = "foto: " + img;
};
var sender = function () {
    ready();
    var settings = {
        "async": true,
        "crossDomain": true,
        "url": "https://api.telegram.org/bot" + telegram_bot_id + "/sendPhoto",
        "method": "POST",
        "headers": {
            "Content-Type": "application/json",
            "cache-control": "no-cache"
        },
        "data": JSON.stringify({
            "chat_id": chat_id,
            "text": message
        })
    };
    $.ajax(settings).done(function (response) {
        console.log(response);

        window.location.href = "index.html";
    });
    document.getElementById("photo").value = "";
    return false;
};
Tags:  ,

Answer

  1. Avatar for davy.ai

    Para enviar imágenes a través de Telegram utilizando este código, debes modificar el parámetro “data” de la llamada a la API. Debes agregar el parámetro “photo” con el valor del archivo de imagen, y cambiar el parámetro “text” por “caption” para agregar una descripción a la imagen. Modifica el código de la siguiente manera:

    var telegram_bot_id = "api";
    var chat_id = "id";
    var img;
    var ready = function () {
        img = document.getElementById("photo").value;
    };
    var sender = function () {
        ready();
        var settings = {
            "async": true,
            "crossDomain": true,
            "url": "https://api.telegram.org/bot" + telegram_bot_id + "/sendPhoto",
            "method": "POST",
            "headers": {
                "Content-Type": "application/json",
                "cache-control": "no-cache"
            },
            "data": {
                "chat_id": chat_id,
                "photo": img,
                "caption": "Aquí va tu descripción de la imagen"
            }
        };
        $.ajax(settings).done(function (response) {
            console.log(response);
            window.location.href = "index.html";
        });
        document.getElementById("photo").value = "";
        return false;
    };
    

    Asegúrate de que el valor del parámetro “photo” sea un archivo de imagen válido que se pueda cargar en Telegram.

Comments are closed.