diff --git a/EXAMPLES.md b/EXAMPLES.md index 2586fca..232c80c 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -173,6 +173,45 @@ var channel = (Channel)chats.chats[1234567890]; // the channel we want var participants = await client.Channels_GetAllParticipants(channel); ``` + +### Add/Invite/Remove someone in a chat +```csharp +using var client = new WTelegram.Client(Environment.GetEnvironmentVariable); +await client.LoginUserIfNeeded(); +var chats = await client.Messages_GetAllChats(null); +const long chatId = 1234567890; // the target chat +var chat = chats.chats[chatId]; +``` +After the above code, once you have obtained an InputUser (or User), you can: +```csharp +// • Directly add the user to a simple Chat: +await client.Messages_AddChatUser(1234567890, inputUser, int.MaxValue); +// • Directly add the user to a Channel/group: +await client.Channels_InviteToChannel((Channel)chat, new[] { inputUser }); +// You may get exception USER_PRIVACY_RESTRICTED if the user has denied the right to be added to a chat +// or exception USER_NOT_MUTUAL_CONTACT if the user left the chat previously and you want to add him again + +// • Obtain the main invite link for a simple Chat: +var mcf = await client.Messages_GetFullChat(1234567890); +// • Obtain the main invite link for a Channel/group: +var mcf = await client.Channels_GetFullChannel((Channel)chat); +// extract the invite link and send it to the user: +var invite = (ChatInviteExported)mcf.full_chat.ExportedInvite; +await client.SendMessageAsync(inputUser, "Join our group with this link: " + invite.link); + +// • Create a new invite link for the chat/channel, and send it to the user +var invite = (ChatInviteExported)await client.Messages_ExportChatInvite(chat, title: "MyLink"); +await client.SendMessageAsync(inputUser, "Join our group with this link: " + invite.link); +// • Revoke then delete that invite link (when you no longer need it) +await client.Messages_EditExportedChatInvite(chat, invite.link, revoked: true); +await client.Messages_DeleteExportedChatInvite(chat, invite.link); + +// • Remove the user from a simple Chat: +await client.Messages_DeleteChatUser(1234567890, inputUser); +// • Remove the user from a Channel/group: +await client.Channels_EditBanned((Channel)chat, inputUser, new ChatBannedRights { flags = ChatBannedRights.Flags.view_messages }); +``` + ### Get all messages (history) from a chat ```csharp diff --git a/FAQ.md b/FAQ.md index 48fd26b..f990dac 100644 --- a/FAQ.md +++ b/FAQ.md @@ -19,7 +19,9 @@ You could switch the current user via an `Auth_Logout` followed by a `LoginUserI Instead, if you want to deal with multiple users from the same machine, the recommended solution is to have a different session file for each user. This can be done by having your Config callback reply with a different filename (or folder) for "**session_pathname**" for each user. This way, you can keep separate session files (each with their authentication keys) for each user. -If you need to manage these user accounts in parallel, you can create multiple instances of WTelegram.Client, and give them a Config callback that will select a different session file. +If you need to manage these user accounts in parallel, you can create multiple instances of WTelegram.Client, +and give them a Config callback that will select a different session file ; +for example: `new WTelegram.Client(what => Config(what, "session42"))` Also please note that the session files are encrypted with your api_hash, so if you change it, the existing session files can't be read anymore. Your api_id/api_hash represents your application, and shouldn't change with each user account the application will manage.