Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

889
Vistas
how to send photo from telegram bot api (javascript)

I have a problem with sending the api via telegram text messages that arrive but the pictures are not arriving and I don't understand where the problem is Is it possible to modify this code and make it send images?

var telegram_bot_id = "api";
var chat_id = "id";
var img;
var ready = function () {
    img = document.getElementById("photo").value;
    message = "photo: " + 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;
};

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

In order to be able to send a photo via sendPhoto method, you have to send a proper picture file or a string represent a valid URL.

According to code you posted, you're sending into the HTTP request as body parameters the chat_id, the text message itself and that's it.

The sendPhoto method does not expect a text field, that's used in sendMessage method.

In your case, since you're trying to send via Telegram chatbot a picture taken from the HTML entity, which in this case is not the picture itself but its path on your webserver, you have to change the POST request body and to set the picture URL as parameter of the request itself.

To conclude, if you want to add the text message, the sendPhoto method allows you to add it but as caption.

For instance:

var telegram_bot_id = "api";
var chat_id = "id";
var img;
var ready = function () {    
    img = "http://your-website.com/imagepath/imagename.png";

};
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,
            "photo": img,
            "caption": message

        })
    };
    $.ajax(settings).done(function (response) {
        console.log(response);
    
        window.location.href = "index.html";
    });

    return false;
    
};
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda