admin管理员组

文章数量:1279009

i'm using this function for sending photo via node.js but that not work. 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'm using this function for sending photo via node.js but that not work. telegram-bot-api https://www.npmjs./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]
Share Improve this question edited Jan 21, 2017 at 9:54 Saeed Heidarizarei asked Jan 21, 2017 at 5:18 Saeed HeidarizareiSaeed Heidarizarei 8,93623 gold badges66 silver badges111 bronze badges 2
  • 1 Are you using any node package to implement telegram bot api? If you aren't using any telegram bot node packages, then the sendPhoto function should include parameters like destination user ID along with photo url like sendPhoto(userId, photo_url) – Sravan Commented Jan 21, 2017 at 5:46
  • i'm using telegram-bot-api moudules but i have this error fn = function () { throw arg; }; ^ StatusCodeError: 403 - [object Object] – Saeed Heidarizarei Commented Jan 21, 2017 at 9:46
Add a ment  | 

2 Answers 2

Reset to default 5

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/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));
    });

本文标签: javascriptTelegram Bot sendPhoto via nodejsStack Overflow