我正在使用此功能通过 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));
});
但是我有这个错误
fn = function () { throw arg; };
^
StatusCodeError: 403 - [object Object]
最佳答案
我已经找到问题了。看起来您正在使用您自己的机器人的聊天 ID 发送无效的照片。因此,您会收到 403 禁止错误(引用 telegram bot errors api )
要使用 sendPhoto 功能,您必须使用用户的聊天 ID,而不是您的 bot 用户。我对您的代码进行了一些修改以使您清楚。此代码将从 message.chatid
变量中获取用户的聊天 ID。
只需在此代码中替换您的 token 并提及您的图像 url 并尝试一下。
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);
});
});
关于javascript - Telegram Bot 通过 node.js 发送照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41776000/