Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

893
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda