Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

892
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!