Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telegram Bot Event When Users Join To Channel

Tags:

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?

like image 830
Ali Mohammadi Avatar asked Jul 12 '16 09:07

Ali Mohammadi


People also ask

How do I know if a Telegram user joined my channel?

Here is a small trick: You can check the updates (messages) came from Telegram to your webhook, if new_chat_members field has a value and the chat_id field indicates that it's from your channel, then you may save the information about the recent users who joined your channel.

Can Telegram bots join channels?

It was possible to add a bot to a group/channel with just an invite link. This was achieved by allowing a bot to access the mtproto methods like messages. importChatInvite for private links and channels. joinChannel.

Can Telegram bot send message to channel?

Create a Telegram bot NOTE: Bots can't message people first, but can message channels.


1 Answers

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.

like image 88
watzon Avatar answered Sep 20 '22 20:09

watzon