i'm using this function for sending photo via node.js but that not work.
telegram-bot-api
https://www.npmjs.com/package/telegram-bot-api
var telegram = require('telegram-bot-api');
var api = new telegram({
token: '<PUT YOUR TOKEN HERE>',
});
api.sendPhoto({
chat_id: <YOUR CHAT ID>,
caption: 'This is my test image',
// you can also send file_id here as string (as described in telegram bot api documentation)
photo: '/path/to/file/test.jpg'
})
.then(function(data)
{
console.log(util.inspect(data, false, null));
});
but i have this error
fn = function () { throw arg; };
^
StatusCodeError: 403 - [object Object]
I have figured out the problem. Looks like you are using the chat ID of your own bot to send the photo which is invalid. Thus, you are getting 403 forbidden error(refer telegram bot errors api)
To use sendPhoto function you will have to use the chat ID of the user not your bot's user. I have made few modifications to your code to make it clear for you. This code will get the user's chat Id from message.chatid
variable.
Just replace your token in this code and mention your image url and try it.
PS: Send any message to this bot and it will send a photo in response.
var telegram = require('telegram-bot-api');
var api = new telegram({
token: 'Your BOT token',
updates: {
enabled: true,
get_interval: 1000
}
});
api.on('message', function(message)
{
var chat_id = message.chat.id;
console.log("This is the user's chat id"+chat_id);
api.sendPhoto({
chat_id : message.chat.id,
caption: 'This is my test image',
photo: 'image.jpeg'//replace your image url here
})
.then(function(data)
{
console.log(data);
});
});
as described in telegram bot api documentation you can send a file with two different ways:
1- sending image by image url :
so you should set photo parameter to an image url, something like below
api.sendPhoto({
chat_id: <YOUR CHAT ID>,
caption: 'image sent by uploading from url',
// first you upload image on a url and send url as a parameter
photo: 'https://whatbook.org/wp-content/uploads/2015/06/Download-Telegram-App-For-PC-Laptop-Windows-XP-7-8-MAC-OS.png'
})
.then(function(data)
{
console.log(util.inspect(data, false, null));
});
2- sending image by file_id
each file that upload in telegram server have an id that you can use this id to avoid reuploading the image to telegram server so in api you should pass the file_id of an image file, something like below :
api.sendPhoto({
chat_id: <YOUR CHAT ID>,
caption: 'the image sent by file_id',
// it is a file_id that you get when someone upload an image to
photo: 'AgADBAADZbo1G14XZAfdtXnWB5anFpRbYRkABMRWzQmdc4EQbPcCAAEC'
})
.then(function(data)
{
console.log(util.inspect(data, false, null));
});