通过node.js电报Bot sendPhoto

Sed*_*rei 3 javascript java node.js telegram telegram-bot

我正在使用此功能通过node.js发送照片,但这不起作用。 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));
});
Run Code Online (Sandbox Code Playgroud)

但是我有这个错误

 fn = function () { throw arg; };
                           ^
StatusCodeError: 403 - [object Object]
Run Code Online (Sandbox Code Playgroud)

Sra*_*van 5

我已经解决了问题。看起来您正在使用自己的漫游器的聊天ID发送无效的照片。因此,您将收到403禁止的错误(请参阅电报漫游器错误api

要使用sendPhoto功能,您将必须使用该用户而不是您的机器人用户的聊天ID。我对您的代码做了很少的修改,以使您清楚。此代码将从message.chatid变量获取用户的聊天ID 。只需替换此代码中的令牌并提及您的图片网址,然后尝试即可。

PS:向该机器人发送任何消息,它将发送照片作为响应。

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);
});
});
Run Code Online (Sandbox Code Playgroud)