Added Example for Help_GetAppConfig / sending Dice

This commit is contained in:
Wizou 2021-11-14 15:35:41 +01:00
parent f901319ca4
commit 27ab343ad0
2 changed files with 28 additions and 28 deletions

View file

@ -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. See [MarkdownV2 formatting style](https://core.telegram.org/bots/api/#markdownv2-style) for details.
<br/>*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))* <br/>*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 ### List all chats (groups/channels) the user is in and send a message to one
```csharp ```csharp
using var client = new WTelegram.Client(Environment.GetEnvironmentVariable); using var client = new WTelegram.Client(Environment.GetEnvironmentVariable);

View file

@ -368,26 +368,10 @@ namespace TL
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 JSONValue { public abstract object ToNative(); } partial class JSONValue { public abstract object ToNative(); }
partial class JsonNull 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"; }
public override string ToString() => "null"; partial class JsonNumber { public override object ToNative() => value; public override string ToString() => value.ToString(CultureInfo.InvariantCulture); }
public override object ToNative() => null; partial class JsonString { public override object ToNative() => value; public override string ToString() => HttpUtility.JavaScriptStringEncode(value, true); }
}
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 JsonArray partial class JsonArray
{ {
public override string ToString() public override string ToString()
@ -402,13 +386,13 @@ namespace TL
{ {
if (value.Length == 0) return Array.Empty<object>(); if (value.Length == 0) return Array.Empty<object>();
var first = value[0].ToNative(); var first = value[0].ToNative();
var elementType = first.GetType(); var T = first.GetType();
var array = Array.CreateInstance(elementType, value.Length); var array = Array.CreateInstance(T, value.Length); // create an array T[] of the native type
array.SetValue(first, 0); array.SetValue(first, 0);
for (int i = 1; i < value.Length; i++) for (int i = 1; i < value.Length; i++)
{ {
var elem = value[i].ToNative(); 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); array.SetValue(elem, i);
} }
return array; return array;
@ -416,6 +400,7 @@ namespace TL
} }
partial class JsonObject partial class JsonObject
{ {
/// <summary>Returns a JSON serialization string for this object</summary>
public override string ToString() public override string ToString()
{ {
var sb = new StringBuilder().Append('{'); var sb = new StringBuilder().Append('{');
@ -423,18 +408,21 @@ namespace TL
sb.Append(i == 0 ? "" : ",").Append(value[i]); sb.Append(i == 0 ? "" : ",").Append(value[i]);
return sb.Append('}').ToString(); return sb.Append('}').ToString();
} }
/// <summary>Returns the given entry in native form (<see langword="bool"/>, <see langword="double"/>, <see langword="string"/>, <see cref="Dictionary{TKey, TValue}">Dictionary</see> or <see cref="Array"/>), or <see langword="null"/> if the key is not found</summary>
public object this[string key] => value.FirstOrDefault(v => v.key == key)?.value.ToNative();
/// <summary>Converts the entries to a Dictionary with keys and values in native form (<see langword="bool"/>, <see langword="double"/>, <see langword="string"/>, <see cref="Dictionary{TKey, TValue}">Dictionary</see> or <see cref="Array"/>)</summary>
public Dictionary<string, object> ToDictionary() => value.ToDictionary(v => v.key, v => v.value.ToNative()); public Dictionary<string, object> ToDictionary() => value.ToDictionary(v => v.key, v => v.value.ToNative());
public override object ToNative() public override object ToNative()
{ {
if (value.Length == 0) return new Dictionary<string, object>(); if (value.Length == 0) return new Dictionary<string, object>();
var first = value[0].value.ToNative(); var first = value[0].value.ToNative();
var elementType = first.GetType(); var T = first.GetType(); // create a Dictionary<string, T> of the native type T:
var dic = Activator.CreateInstance(typeof(Dictionary<,>).MakeGenericType(typeof(string), elementType)) as System.Collections.IDictionary; var dic = Activator.CreateInstance(typeof(Dictionary<,>).MakeGenericType(typeof(string), T)) as System.Collections.IDictionary;
dic.Add(value[0].key, first); dic.Add(value[0].key, first);
for (int i = 1; i < value.Length; i++) for (int i = 1; i < value.Length; i++)
{ {
var elem = value[i].value.ToNative(); var elem = value[i].value.ToNative();
if (elem.GetType() != elementType) return ToDictionary(); if (elem.GetType() != T) return ToDictionary(); // incompatible => return a Dictionary<string, object> instead
dic.Add(value[i].key, elem); dic.Add(value[i].key, elem);
} }
return dic; return dic;