Hey everyone, I’m having trouble with my Telegram bot. It’s working fine overall, but I’m getting a weird error in the console when a user sends their contact info. The message still goes through, but I see this:
Everything else works fine. It’s just this contact sharing bit that’s causing issues. I’ve looked online and found some mentions of unhandled errors, but I’m not sure what’s wrong here. Any ideas on how to fix this? Thanks in advance!
I’ve encountered a similar issue with Telegram bots and contact sharing. The error you’re seeing is likely due to the bot not having a specific handler for contact messages. To resolve this, you should add an explicit handler for the ‘contact’ event.
Try modifying your code like this:
myBot.on('contact', (msg) => {
const chatId = msg.chat.id;
const phoneNumber = msg.contact.phoneNumber;
// Process the contact info here
myBot.sendMessage(chatId, `Thanks for sharing your number: ${phoneNumber}`);
});
This should catch the contact message and prevent the unhandled polling error. Also, make sure you’re using the latest version of your bot library, as older versions might have issues with certain message types. If the problem persists, you might want to implement a general error handler to log these issues more comprehensively.
I’ve dealt with this exact issue before, and it can be frustrating. The problem lies in how Telegram handles contact sharing. When a user shares their contact, it’s not treated as a regular message, but as a special ‘contact’ event.
To fix this, you need to add a specific handler for contact events. Here’s what worked for me:
This should resolve the unhandled polling error. Also, make sure you’re properly closing your bot connection when you’re done. I’ve found that leaving connections open can sometimes lead to weird errors popping up.
If you’re still having issues after this, it might be worth checking your bot’s permissions or even recreating your bot token. Sometimes that can solve mysterious errors that persist.