mirror of
https://github.com/wiz0u/WTelegramClient.git
synced 2025-12-06 06:52:01 +01:00
Helpers for Contacts_ResolvedPeer. Fix some examples
This commit is contained in:
parent
af79bfa873
commit
08ba766e5c
11
EXAMPLES.md
11
EXAMPLES.md
|
|
@ -15,14 +15,17 @@ Remember that these are just simple example codes that you should adjust to your
|
||||||
```csharp
|
```csharp
|
||||||
using var client = new WTelegram.Client(Environment.GetEnvironmentVariable);
|
using var client = new WTelegram.Client(Environment.GetEnvironmentVariable);
|
||||||
await client.LoginUserIfNeeded();
|
await client.LoginUserIfNeeded();
|
||||||
var resolved = await client.Contacts_ResolveUsername("USERNAME");
|
var resolved = await client.Contacts_ResolveUsername("username");
|
||||||
await client.SendMessageAsync(resolved.users[resolved.peer.ID], "Hello!");
|
await client.SendMessageAsync(resolved, "Hello!");
|
||||||
```
|
```
|
||||||
|
*Note: This also works if the @username points to a chat, but you must join the chat before posting there.
|
||||||
|
You can check `resolved` properties to ensure it's a user or a chat. If the username is invalid/unused, the API call raises an exception.*
|
||||||
### Send a message to someone by phone number
|
### Send a message to someone by phone number
|
||||||
```csharp
|
```csharp
|
||||||
using var client = new WTelegram.Client(Environment.GetEnvironmentVariable);
|
using var client = new WTelegram.Client(Environment.GetEnvironmentVariable);
|
||||||
await client.LoginUserIfNeeded();
|
await client.LoginUserIfNeeded();
|
||||||
var contacts = await client.Contacts_ImportContacts(new[] { new InputPhoneContact { phone = "+PHONENUMBER" } });
|
var contacts = await client.Contacts_ImportContacts(new[] { new InputPhoneContact { phone = "+PHONENUMBER" } });
|
||||||
|
if (contacts.imported.Length > 0)
|
||||||
await client.SendMessageAsync(contacts.users[contacts.imported[0].user_id], "Hello!");
|
await client.SendMessageAsync(contacts.users[contacts.imported[0].user_id], "Hello!");
|
||||||
```
|
```
|
||||||
*Note: To prevent spam, Telegram may restrict your ability to add new phone numbers.*
|
*Note: To prevent spam, Telegram may restrict your ability to add new phone numbers.*
|
||||||
|
|
@ -95,7 +98,7 @@ For a simple Chat: (see Terminology in [ReadMe](README.md#Terminology-in-Telegra
|
||||||
using var client = new WTelegram.Client(Environment.GetEnvironmentVariable);
|
using var client = new WTelegram.Client(Environment.GetEnvironmentVariable);
|
||||||
await client.LoginUserIfNeeded();
|
await client.LoginUserIfNeeded();
|
||||||
var chatFull = await client.Messages_GetFullChat(1234567890); // the chat we want
|
var chatFull = await client.Messages_GetFullChat(1234567890); // the chat we want
|
||||||
foreach (var user in chatFull.users)
|
foreach (var (id, user) in chatFull.users)
|
||||||
Console.WriteLine(user);
|
Console.WriteLine(user);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -108,7 +111,7 @@ var channel = (Channel)chats.chats[1234567890]; // the channel we want
|
||||||
for (int offset = 0; ;)
|
for (int offset = 0; ;)
|
||||||
{
|
{
|
||||||
var participants = await client.Channels_GetParticipants(channel, null, offset, 1000, 0);
|
var participants = await client.Channels_GetParticipants(channel, null, offset, 1000, 0);
|
||||||
foreach (var user in participants.users)
|
foreach (var (id, user) in participants.users)
|
||||||
Console.WriteLine(user);
|
Console.WriteLine(user);
|
||||||
offset += participants.participants.Length;
|
offset += participants.participants.Length;
|
||||||
if (offset >= participants.count) break;
|
if (offset >= participants.count) break;
|
||||||
|
|
|
||||||
|
|
@ -99,6 +99,55 @@ namespace TL
|
||||||
protected override InputUserBase ToInputUser() => new InputUser { user_id = id, access_hash = access_hash };
|
protected override InputUserBase ToInputUser() => new InputUser { user_id = id, access_hash = access_hash };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
partial class Peer { public abstract long ID { get; } }
|
||||||
|
partial class PeerUser { public override long ID => user_id; public override string ToString() => "user " + user_id; }
|
||||||
|
partial class PeerChat { public override long ID => chat_id; public override string ToString() => "chat " + chat_id; }
|
||||||
|
partial class PeerChannel { public override long ID => channel_id; public override string ToString() => "channel " + channel_id; }
|
||||||
|
|
||||||
|
partial class Contacts_ResolvedPeer
|
||||||
|
{
|
||||||
|
public static implicit operator InputPeer(Contacts_ResolvedPeer resolved) => resolved.UserOrChat.ToInputPeer();
|
||||||
|
public UserBase User => peer is PeerUser pu ? users[pu.user_id] : null;
|
||||||
|
public ChatBase Chat => peer is PeerChat or PeerChannel ? chats[peer.ID] : null;
|
||||||
|
public IPeerInfo UserOrChat => peer switch
|
||||||
|
{
|
||||||
|
PeerUser pu => users[pu.user_id],
|
||||||
|
PeerChat pc => chats[pc.chat_id],
|
||||||
|
PeerChannel pch => chats[pch.channel_id],
|
||||||
|
_ => null
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
partial class DialogBase
|
||||||
|
{
|
||||||
|
public abstract Peer Peer { get; }
|
||||||
|
public abstract int TopMessage { get; }
|
||||||
|
}
|
||||||
|
partial class Dialog
|
||||||
|
{
|
||||||
|
public override Peer Peer => peer;
|
||||||
|
public override int TopMessage => top_message;
|
||||||
|
}
|
||||||
|
partial class DialogFolder
|
||||||
|
{
|
||||||
|
public override Peer Peer => peer;
|
||||||
|
public override int TopMessage => top_message;
|
||||||
|
}
|
||||||
|
|
||||||
|
partial class Messages_Dialogs
|
||||||
|
{
|
||||||
|
/// <summary>Find the matching User/Chat object for a dialog</summary>
|
||||||
|
/// <param name="dialog">The dialog which peer we want details on</param>
|
||||||
|
/// <returns>a UserBase or ChatBase derived instance</returns>
|
||||||
|
public IPeerInfo GetUserOrChat(DialogBase dialog) => dialog.Peer switch
|
||||||
|
{
|
||||||
|
PeerUser pu => users[pu.user_id],
|
||||||
|
PeerChat pc => chats[pc.chat_id],
|
||||||
|
PeerChannel pch => chats[pch.channel_id],
|
||||||
|
_ => null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
partial class MessageBase
|
partial class MessageBase
|
||||||
{
|
{
|
||||||
public abstract int ID { get; }
|
public abstract int ID { get; }
|
||||||
|
|
@ -263,41 +312,6 @@ namespace TL
|
||||||
public static implicit operator InputStickerSetID(StickerSet stickerSet) => new() { id = stickerSet.id, access_hash = stickerSet.access_hash };
|
public static implicit operator InputStickerSetID(StickerSet stickerSet) => new() { id = stickerSet.id, access_hash = stickerSet.access_hash };
|
||||||
}
|
}
|
||||||
|
|
||||||
partial class Peer { public abstract long ID { get; } }
|
|
||||||
partial class PeerUser { public override long ID => user_id; public override string ToString() => "user " + user_id; }
|
|
||||||
partial class PeerChat { public override long ID => chat_id; public override string ToString() => "chat " + chat_id; }
|
|
||||||
partial class PeerChannel { public override long ID => channel_id; public override string ToString() => "channel " + channel_id; }
|
|
||||||
|
|
||||||
partial class DialogBase
|
|
||||||
{
|
|
||||||
public abstract Peer Peer { get; }
|
|
||||||
public abstract int TopMessage { get; }
|
|
||||||
}
|
|
||||||
partial class Dialog
|
|
||||||
{
|
|
||||||
public override Peer Peer => peer;
|
|
||||||
public override int TopMessage => top_message;
|
|
||||||
}
|
|
||||||
partial class DialogFolder
|
|
||||||
{
|
|
||||||
public override Peer Peer => peer;
|
|
||||||
public override int TopMessage => top_message;
|
|
||||||
}
|
|
||||||
|
|
||||||
partial class Messages_Dialogs
|
|
||||||
{
|
|
||||||
/// <summary>Find the matching User/Chat object for a dialog</summary>
|
|
||||||
/// <param name="dialog">The dialog which peer we want details on</param>
|
|
||||||
/// <returns>a UserBase or ChatBase derived instance</returns>
|
|
||||||
public IPeerInfo GetUserOrChat(DialogBase dialog) => dialog.Peer switch
|
|
||||||
{
|
|
||||||
PeerUser pu => users[pu.user_id],
|
|
||||||
PeerChat pc => chats[pc.chat_id],
|
|
||||||
PeerChannel pch => chats[pch.channel_id],
|
|
||||||
_ => null,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
partial class JsonObjectValue { public override string ToString() => $"{HttpUtility.JavaScriptStringEncode(key, true)}:{value}"; }
|
partial class JsonObjectValue { public override string ToString() => $"{HttpUtility.JavaScriptStringEncode(key, true)}:{value}"; }
|
||||||
partial class JsonNull { public override string ToString() => "null"; }
|
partial class JsonNull { public override string ToString() => "null"; }
|
||||||
partial class JsonBool { public override string ToString() => value ? "true" : "false"; }
|
partial class JsonBool { public override string ToString() => value ? "true" : "false"; }
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue