Debugging Telegram bot: Unhandled polling error when sending contact info

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:

error: [polling_error] {}

Here’s a simplified version of my code:

myBot.on('message', (message) => {
  const chatId = message.chat.id;
  const userName = message.from.firstName;

  if (message.text === '/begin') {
    myBot.sendMessage(chatId, `Hi ${userName}! Please share your phone number to start.`, {
      replyMarkup: {
        keyboard: [[{ text: 'Share Contact', requestContact: true }]]
      }
    });
  }
});

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:

myBot.on('contact', (msg) => {
  const chatId = msg.chat.id;
  const phoneNumber = msg.contact.phoneNumber;
  const name = msg.contact.firstName + ' ' + msg.contact.lastName;

  console.log(`Received contact: ${name}, ${phoneNumber}`);
  myBot.sendMessage(chatId, `Thanks, ${name}! I've got your number.`);
});

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.

yo mike, i had that issue too. the error’s prolly cuz ur not handling contact messages. try adding this:

myBot.on(‘contact’, (msg) => {
// handle contact here
});

should fix it. if not, maybe check ur bot library version. gl mate!