diff --git a/EXAMPLES.md b/EXAMPLES.md index 48a5cc1..aeae702 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -42,6 +42,18 @@ await client.SendMessageAsync(InputPeer.Self, text, entities: entities); See [MarkdownV2 formatting style](https://core.telegram.org/bots/api/#markdownv2-style) for details.
*Note: For the `tg://user?id=` notation to work, that user's access hash must have been collected first ([see below](#Collect-Access-Hash-and-save-them-for-later-use))* +### Send a random dice (or other "game of chance" animated emoji) +```csharp +using var client = new WTelegram.Client(Environment.GetEnvironmentVariable); +var user = await client.LoginUserIfNeeded(); +var appConfig = await client.Help_GetAppConfig(); +if (appConfig["emojies_send_dice"] is string[] emojies_send_dice) // get list of animated "dice" emojies +{ + var which = new Random().Next(emojies_send_dice.Length); // choose one of the available emojies + await client.SendMessageAsync(InputPeer.Self, null, new InputMediaDice { emoticon = emojies_send_dice[which] }); +} +``` + ### List all chats (groups/channels) the user is in and send a message to one ```csharp using var client = new WTelegram.Client(Environment.GetEnvironmentVariable); diff --git a/src/TL.Helpers.cs b/src/TL.Helpers.cs index 8e567c2..f966f75 100644 --- a/src/TL.Helpers.cs +++ b/src/TL.Helpers.cs @@ -366,28 +366,12 @@ namespace TL public InputSecureFileLocation ToFileLocation() => new() { id = id, access_hash = access_hash }; } - partial class JsonObjectValue { public override string ToString() => $"{HttpUtility.JavaScriptStringEncode(key, true)}:{value}"; } - partial class JSONValue { public abstract object ToNative(); } - partial class JsonNull - { - public override string ToString() => "null"; - public override object ToNative() => null; - } - partial class JsonBool - { - public override string ToString() => value ? "true" : "false"; - public override object ToNative() => value; - } - partial class JsonNumber - { - public override string ToString() => value.ToString(CultureInfo.InvariantCulture); - public override object ToNative() => value; - } - partial class JsonString - { - public override string ToString() => HttpUtility.JavaScriptStringEncode(value, true); - public override object ToNative() => value; - } + partial class JsonObjectValue { public override string ToString() => $"{HttpUtility.JavaScriptStringEncode(key, true)}:{value}"; } + partial class JSONValue { public abstract object ToNative(); } + partial class JsonNull { public override object ToNative() => null; public override string ToString() => "null"; } + partial class JsonBool { public override object ToNative() => value; public override string ToString() => value ? "true" : "false"; } + partial class JsonNumber { public override object ToNative() => value; public override string ToString() => value.ToString(CultureInfo.InvariantCulture); } + partial class JsonString { public override object ToNative() => value; public override string ToString() => HttpUtility.JavaScriptStringEncode(value, true); } partial class JsonArray { public override string ToString() @@ -402,13 +386,13 @@ namespace TL { if (value.Length == 0) return Array.Empty(); var first = value[0].ToNative(); - var elementType = first.GetType(); - var array = Array.CreateInstance(elementType, value.Length); + var T = first.GetType(); + var array = Array.CreateInstance(T, value.Length); // create an array T[] of the native type array.SetValue(first, 0); for (int i = 1; i < value.Length; i++) { var elem = value[i].ToNative(); - if (elem.GetType() != elementType) return ToNativeArray(); + if (elem.GetType() != T) return ToNativeArray(); // incompatible => return an object[] instead array.SetValue(elem, i); } return array; @@ -416,6 +400,7 @@ namespace TL } partial class JsonObject { + /// Returns a JSON serialization string for this object public override string ToString() { var sb = new StringBuilder().Append('{'); @@ -423,18 +408,21 @@ namespace TL sb.Append(i == 0 ? "" : ",").Append(value[i]); return sb.Append('}').ToString(); } + /// Returns the given entry in native form (, , , Dictionary or ), or if the key is not found + public object this[string key] => value.FirstOrDefault(v => v.key == key)?.value.ToNative(); + /// Converts the entries to a Dictionary with keys and values in native form (, , , Dictionary or ) public Dictionary ToDictionary() => value.ToDictionary(v => v.key, v => v.value.ToNative()); public override object ToNative() { if (value.Length == 0) return new Dictionary(); var first = value[0].value.ToNative(); - var elementType = first.GetType(); - var dic = Activator.CreateInstance(typeof(Dictionary<,>).MakeGenericType(typeof(string), elementType)) as System.Collections.IDictionary; + var T = first.GetType(); // create a Dictionary of the native type T: + var dic = Activator.CreateInstance(typeof(Dictionary<,>).MakeGenericType(typeof(string), T)) as System.Collections.IDictionary; dic.Add(value[0].key, first); for (int i = 1; i < value.Length; i++) { var elem = value[i].value.ToNative(); - if (elem.GetType() != elementType) return ToDictionary(); + if (elem.GetType() != T) return ToDictionary(); // incompatible => return a Dictionary instead dic.Add(value[i].key, elem); } return dic;