Hey everyone! I’m trying to send photos using a Telegram bot with Node.js, but I’m running into some trouble. I’m using the telegram-bot-api package, and here’s what my code looks like:
const TelegramBot = require('telegram-bot-api');
const bot = new TelegramBot({
token: 'MY_BOT_TOKEN',
});
bot.sendPhoto({
chat_id: 123456789,
caption: 'Check out this cool pic!',
photo: './images/awesome.jpg'
})
.then(response => {
console.log('Photo sent successfully:', response);
})
.catch(error => {
console.error('Error sending photo:', error);
});
When I run this, I get a 403 error. Any ideas what I might be doing wrong? Is there a different way to send photos with a Telegram bot in Node.js? Thanks for any help!
hey mate, had similar issue. try using fs.createReadStream() for the photo instead of a direct path. like this:
const fs = require(‘fs’);
bot.sendPhoto({
…
photo: fs.createReadStream(‘./images/awesome.jpg’)
})
shud work. lemme know if u need more help!
I encountered a similar issue when working with the Telegram bot API. The problem might be related to how you’re specifying the photo path. Instead of using a relative path, try using an absolute path to the image file. You can achieve this by using the path module in Node.js:
const path = require(‘path’);
const photoPath = path.join(__dirname, ‘images’, ‘awesome.jpg’);
bot.sendPhoto({
chat_id: 123456789,
caption: ‘Check out this cool pic!’,
photo: photoPath
});
This approach ensures that Node.js can locate the file correctly, regardless of the current working directory. If you’re still experiencing issues, double-check your bot token and make sure you have the necessary permissions to send messages in the specified chat.
I’ve dealt with this exact problem before, and it can be frustrating. One thing that worked for me was using a URL instead of a local file path. If your image is hosted online, you can directly use the URL in the ‘photo’ field:
bot.sendPhoto({
chat_id: 123456789,
caption: ‘Check out this cool pic!’,
photo: ‘https://example.com/path/to/your/image.jpg’
})
This bypasses any issues with file paths and permissions. If you must use a local file, make sure you have the correct read permissions set. Also, double-check your bot token - sometimes a 403 error can occur if the token is invalid or has been revoked. Hope this helps!