How can i get the chat invite link through which a member joins in node telegram bot api #chatmemberupdated

359 Views Asked by At

I am trying to make a telegram bot and i want the chat invite link through which the member joins the group in telegram. I am using the node telegram bot api in NodeJS and while using the "chatMemberUpdated" method as specified in the official telegram bot api https://core.telegram.org/bots/api#chatmemberupdated. But i am not getting any updates back, here is the code.

bot.on("chatMemberUpdated", async (update) => {
  try {
    const chatId = await update.chat.id;
    const newMember = await update.message.new_chat_member;

    // Check if a new member joined
    if (newMember) {
      const chatInviteLink = await update.message.chat_invite_link;

      if (chatInviteLink) {
        console.log(
          `New member ${newMember.first_name} joined in chat ${chatId}`
        );
        console.log(`Chat Invite Link: ${chatInviteLink}`);
      } else {
        console.log(`Chat Invite Link not available for chat ${chatId}`);
      }
    }
  } catch (error) {
    console.log(error)
  }
});

I have tried using 'new_chat_members' and 'left_chat_member' methods and they are returning the data back about the members joining and leaving but i want the invite_link for that i am not able to understand which method to use. Any suggestions will be appreciated and if there are any alternate methods which i can achieve the same thing above, i am open to those suggestions as well.

1

There are 1 best solutions below

0
Parvez Alam On BEST ANSWER

I got the answer after that it was a silly mistake on my end, the "chatMemberUpdated" is a method type, the way we can get the invite_link through which a member joins. The method here to use is "chat_member" while the {allowed_updates : chat_member} option is availed to the bot as mentioned in the telegram api official documentation, The bot must be an administrator in the chat and must explicitly specify "chat_member" in the list of allowed_updates to receive these updates. A sample code using telegraf (i thought this method was not supported by node-telegram-bot-api npm package) is shown below:-

const { Telegraf } = require('telegraf');
const { message } =  require('telegraf/filters')

const botToken = 'YOUR_TOKEN_GOTTEN_THROUGH_BOTFATHER';

const bot = new Telegraf(botToken);

bot.on('chat_member', (ctx) => {
  console.log(ctx.chatMember.invite_link)
})

// Start the bot
bot.launch({
  allowedUpdates : ['chat_member']
}).then(() => {
  console.log('Bot is running');
});

invite_link can be found in chatMember object. I tried this with telegraf but one can achieve the same results from node-telegram-bot-api as well.