Telegram Bot Event When Users Join To Channel
Asked Answered
M

5

25

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?

Melleta answered 12/7, 2016 at 9:7 Comment(0)
D
30

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.

Downandout answered 5/2, 2018 at 7:57 Comment(4)
@Downandout the problem is that you get updates with new_chat_members only for groups and supergroups, but not for channelsStandoffish
Is there any way to get the usernames for the new_chat_members?Chiaki
@Standoffish is there any way to get updates for channels?Periphery
@ИльяЗеленько well you can't get them for channels, as mentioned aboveDownandout
G
5

From docs:

new_chat_members New members that were added to the group or supergroup and information about them (the bot itself may be one of these members)

So i think you can't.

Gelhar answered 6/1, 2020 at 18:36 Comment(0)
N
3

To get updates on users join group or channel you must correctly setup webhook for your bot. This update type is disabled by default. See https://core.telegram.org/bots/api#setwebhook

allowed_updates

A JSON-serialized list of the update types you want your bot to receive. For example, specify [“message”, “edited_channel_post”, “callback_query”] to only receive updates of these types. See Update for a complete list of available update types. Specify an empty list to receive all update types except chat_member (default). If not specified, the previous setting will be used.

Please note that this parameter doesn't affect updates created before the call to the getUpdates, so unwanted updates may be received for a short period of time.

So, the request should look like this:

https://api.telegram.org/<BOT_TOKEN>/setWebhook?url=<WEBHOOK_URL>&allowed_updates=["chat_member"]
Nat answered 16/2, 2023 at 4:0 Comment(0)
K
0

If you want to get a message about join a member to channel you need to create invite link with request to join, and when user go to this link your bot see it

Kathline answered 21/2 at 22:56 Comment(0)
A
-3

Yes! You can use this:

https://api.telegram.org/bot[TOKEN]/promoteChatMember?chat_id=@[channelname]&user_id=[user_id]

if user is joined response is ok, else response is not ok.

Assagai answered 20/9, 2020 at 14:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.