skip to Main Content

I’m trying to pull the data, whether the user has a premium tg, but it does not work.

Сan you know the solution to this problem and help me?

2

Answers


  1. const { Telegraf } = require('telegraf')
    
    const bot = new Telegraf(process.env.BOT_TOKEN)
    
    bot.command('check_premium', async (ctx) => {
      const chatId = ctx.message.chat.id
      const userId = ctx.message.from.id
      
      try {
        const chatMember = await ctx.telegram.getChatMember(chatId, userId)
        const isPremium = chatMember.status === 'creator' || chatMember.status === 'administrator' || chatMember.status === 'member' && chatMember.is_member == true && chatMember.can_pin_messages == true
        
        if (isPremium) {
          ctx.reply('Congratulations! You have Telegram Premium.')
        } else {
          ctx.reply('Sorry, you do not have Telegram Premium.')
        }
      } catch (error) {
        console.error(error)
        ctx.reply('An error occurred while checking your subscription status.')
      }
    })
    
    bot.launch()
    Login or Signup to reply.
  2. The Telegram Bot Api provides you with an User object, representing a Telegram user.

    This object contains the following field:

    is_premium
    Type: True
    Optional. True, if this user is a Telegram Premium user

    This has been added in Bot API 6.1, released on June 20, 2022


    So you’ll just need to use Telegraf to get the User object, and check if the is_premium key is set.

    Example, the User should be available as from on ctx:

    bot.on(message('text'), async (ctx) => {
    
      const is_premium = ctx.from.is_premium;
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search