rubenlagus / TelegramBots

Java library to create bots using Telegram Bots API
https://telegram.me/JavaBotsApi
MIT License
4.68k stars 1.18k forks source link

Channel members #236

Closed Pie133 closed 7 years ago

Pie133 commented 7 years ago

Hi i want to force users to join a channel before they can use my bot is there any way that i can get channel members chat_id in a array?

Clevero commented 7 years ago

It's not possible to get all ChatMembers of channel.

You can get only the Administrators (see offical Bot API docs getChatAdministrators)

But what you could do is to check if a specific user is in your channel by using getChatMember (see offical Bot API docs here)

Also I recommend you to look through the offical Bot API docs. Anything what is offical described there is also implemented/possible with this library.

Pie133 commented 7 years ago

Sorry but how can use getChatMember in your API ?

Clevero commented 7 years ago

All methods of the offical Bot API are basically also just methods in @rubenlagus's lib.

And you should also know an example for that. sendMessage sendMessage is a method in the API, here also sendMessage -> sendMessage(message);

The same with sendAudio sendAudio -> sendAudio(audio);

And the list goes on, and on, and on...^^

So for getChatMember

GetChatMember getChatMember = new GetChatMember():

//id of the chat (in your case the channel id)
getChatMember.setChatId(chatId);

//id of the user
getChatMember.setUserId(userId);

                   //run the actual method getChatMember
ChatMember member = getChatMember(getChatMember);
fx2xf commented 7 years ago

what happens if a user isn't a member of a channel, does it return an exception? I tried this method and it seems it's not working :-(

Clevero commented 7 years ago

Don't have tested it yet, but it should return null if the user isn't a member of the targeted chat.

Returns a ChatMember object on success. getChatMember

Fotomultman commented 7 years ago

Please, can you make an example with getChatMembersCount ... Dont inderstand how it works

Clevero commented 7 years ago

@Fotomultman try to do the same scheme like in this example https://github.com/rubenlagus/TelegramBots/issues/236#issuecomment-302244907

Fotomultman commented 7 years ago
   GetChatMemberCount getChatMemberCount = new GetChatMemberCount();
   getChatMemberCount.setChatId(ChatID);
   String  members_count = getChatMemberCount (getChatMemberCount).toString(); // gets members count 

Thank you. Nice.

then I tried to get List of ChatMember to getChatAdministrators

GetChatAdministrators getChatAdministrators = new GetChatAdministrators();
getChatAdministrators.setChatId(ChatID);
List<ChatMember> lst = getChatAdministrators (getChatAdministrators);
int admin_count = lst.size(); //gets admins count if they are exists

Nice!