Telegram Bot Event When Users Join To Channel

Ali Mohammadi picture Ali Mohammadi · Jul 12, 2016 · Viewed 24.5k times · Source

After create telegram bot, access and admin this bot to channel. how to get channel members list or event when users join to this channel?

Answer

watzon picture watzon · Feb 5, 2018

Pretty disappointed with the current answers, so I'll leave an updated (as of February 2018) answer that explains how to do this with the Telegram API itself, as well as with the framework I am using, Telegraf for Node.

The Telegram API is both very powerful, and pretty simple as far as API's go. If you are using the polling method of getting updates, and not websockets which are a whole other issue, checking if someone new has been added to a group or channel is very easy.

The API method getUpdates returns an array of Update objects, which contain all of the possible information you could want including any messages sent, inline queries, and new chat members. To get any new chat members you simply need to access update.message.new_chat_members which will contain an array of new users. For reference you can look in the API documentation here.

To fetch the update objects in browser, or with curl, all you have to do is send a GET or POST request to https://api.telegram.org/botYOUR-BOT-TOKEN/getUpdates. Then just look for messages->new_chat_members.

If you are using the Telegraf bot framework with NodeJs you can use the bot.on method with the event new_chat_members.

Example:

bot.on('new_chat_members', (ctx) => console.log(ctx.message.new_chat_members))

I know this was asked a while ago, but I hope this helps other people searching.