javascripttelegram-bottelegram-api

How to get the invite_link object with Telegram API? I want to check through which invite link a new member joins the group chat


I want to check through which invite link a new member joins the group chat. According to the Telegram API docs, there should be an invite_link type when listening to the Chat_member update.

ChatMemberUpdated
This object represents changes in the status of a chat member.

Field   Type    Description
chat    Chat    Chat the user belongs to
from    User    Performer of the action, which resulted in the change
date    Integer Date The change was done in Unix time
old_chat_member ChatMember  Previous information about the chat member
new_chat_member ChatMember  New information about the chat member
invite_link ChatInviteLink  Optional. Chat invite link, which was used by the user to join the chat; for joining by invite link events only.
via_join_request    Boolean Optional. True, if the user joined the chat after sending a direct join request without using an invite link and being approved by an administrator
via_chat_folder_invite_link Boolean Optional. True, if the user joined the chat via a chat folder invite link

But I failed to get the invite_link object with the following code:

const TelegramBot = require('node-telegram-bot-api');
require ('dotenv').config();

const token = process.env.BOT_TOKEN;
const bot = new TelegramBot(token, { polling: true });

bot.on('chat_member', (msg) => {
  const newMemberInviteLink = msg.invite_link ? msg.invite_link : "Unknown Invite Link";
  console.log(newMemberInviteLink);
})

When I try to console log the entire msg, I get the entire ChatMemberUpdated type, but there is no invite_link even though I am sure the new member joined the group chat with a generated invite link other than the primary invite link:

Received update: {
  chat: {
    id: -1002225700353,
    title: 'TestReferralChatgroup',
    username: 'TestReferralChatgroup',
    type: 'supergroup'
  },
  from: {
    id: 5189356713,
    is_bot: false,
    first_name: 'rii',
    last_name: 'un',
    username: 'LilKimbom'
  },
  date: 1721558379,
  old_chat_member: {
    user: {
      id: 5189356713,
      is_bot: false,
      first_name: 'rii',
      last_name: 'un',
      username: 'LilKimbom'
    },
    status: 'left'
  },
  new_chat_member: {
    user: {
      id: 5189356713,
      is_bot: false,
      first_name: 'rii',
      last_name: 'un',
      username: 'LilKimbom'
    },
    status: 'member'
  }
}

When I try to console log the entire msg, I get the entire ChatMemberUpdated type, but there is no invite_link even though I am sure the new member joined the group chat with a generated invite link other than the primary invite link:

Received update: {
  chat: {
    id: -1002225700353,
    title: 'TestReferralChatgroup',
    username: 'TestReferralChatgroup',
    type: 'supergroup'
  },
  from: {
    id: 5189356713,
    is_bot: false,
    first_name: 'rii',
    last_name: 'un',
    username: 'LilKimbom'
  },
  date: 1721558379,
  old_chat_member: {
    user: {
      id: 5189356713,
      is_bot: false,
      first_name: 'rii',
      last_name: 'un',
      username: 'LilKimbom'
    },
    status: 'left'
  },
  new_chat_member: {
    user: {
      id: 5189356713,
      is_bot: false,
      first_name: 'rii',
      last_name: 'un',
      username: 'LilKimbom'
    },
    status: 'member'
  }
}

Solution

  • I'm actually wondering why you're even getting the chat_member update. According to documentation, you need to explicitly specify chat_member in allowed_updates parameter of /getUpdates method to be able to receive ChatMemberUpdated events.

    Can you change the bot definition like this and check if it fixes the issue?

    const bot = new TelegramBot("TOKEN", {
        polling: {
            params: {
                allowed_updates: [
                    "message", "chat_member", // list any other update type...
                ]
            }
        }
    });
    

    Give it a try, I'm not sure if this is the faulty factor though.

    Hope this helps!