gram-js / gramjs

NodeJS/Browser MTProto API Telegram client library,
MIT License
1.31k stars 178 forks source link

Managing Close Groups #324

Open rafaelfaria opened 2 years ago

rafaelfaria commented 2 years ago

This is a question/issue I am having.

I am building a close group, they make the payment, they enter their username, I save this info in the database, then there are 3 ways this person can join the channel, through the bot who will auto-invite this person, they can join through a link or an admin can add a person.

For this, I am using the following, which works

const userInfo = await client.invoke(new Api.contacts.ResolveUsername({ username }))

The auto-invite I am struggling with a bit. I tried the following:

  var response = await client.invoke(
    new Api.channels.InviteToChannel({
      channel: <CHANNEL_ID>,
      users: [telegramId],
    })
  );

However, I was getting an issue because apparently this can't be done using a bot.

message: '400: BOT_METHOD_INVALID (caused by channels.InviteToChannel)',
  name: 'RPCError',
  code: 400,
  errorMessage: 'BOT_METHOD_INVALID'

So I change strategy and generated a session using my username so I can actually use the method (the trade-off is that the session only seems to work for 6 months)

ok. So, got the telegramId, managed to invite, and now I need to check people that are in the channel or came through any other method I listed above, if they are valid, they have paid their subscription.

Note: I need to do this in a few channels and there are groups and channels.

I first tried to set up a webhook to listen to events, however, I do not get the event "Joined", the only other way I can think of, is to keep pulling every minute to check. However, It's 6 channels, and 5 thousand people. It's not scalable.

What is the best way to keep checking for participants, even if I were through this path, using channels.getParticipants to get the participants only seems to work on supergroups and channels according to the docs, (doesn't say anything about groups)

I would love to get the insight from the community :)

and apologies if this is not exactly the "issue" on using the apis itself, but still an issue :)

painor commented 2 years ago

if you want to check if a specific person is in the group you can use the following https://gram.js.org/tl/channels/GetParticipant#channelsgetparticipant preferably with a bot not a user. Using getParticipants is not very reliable as it might not return all users. if the group has less than 1000 users you can use it fine (https://gram.js.org/beta/classes/TelegramClient.html#getParticipants) otherwise it's better to call it per person.

rafaelfaria commented 2 years ago

@painor The problem with that approach is that i would have to pull this data every minute or so, to make sure no one enter the group through a shared link ad able to see the content of it. And its 5-6 groups, with 5k users i need to check.

The best way would be through the webhook. or listen to it somehow (although I'm using lambdas)

painor commented 2 years ago

the other choice is to revoke all links and make the newly created links only per person.

I haven't worked with something like that before maybe someone else has a better idea :/

rafaelfaria commented 2 years ago

I also though about that, but when i create a new link it doesn't restrict to just one person (not sure if its a bug on telegram) but, still doesn't solve the problem of knowing who are in the channel and if they have a paid subscription.