microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.Apps/Schema/Entities/ClientInfoEntity.cs
119lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Text.Json.Serialization; |
| 5 | |
| 6 | namespace Microsoft.Teams.Bot.Apps.Schema.Entities; |
| 7 | |
| 8 | |
| 9 | /// <summary> |
| 10 | /// Extension methods for Activity to handle client info. |
| 11 | /// </summary> |
| 12 | public static class ActivityClientInfoExtensions |
| 13 | { |
| 14 | /// <summary> |
| 15 | /// Adds client information to the activity's entity collection. |
| 16 | /// </summary> |
| 17 | /// <param name="activity">The activity to add client information to. Cannot be null.</param> |
| 18 | /// <param name="platform">The platform identifier (e.g., "web", "desktop", "mobile").</param> |
| 19 | /// <param name="country">The country code (e.g., "US", "GB").</param> |
| 20 | /// <param name="timeZone">The time zone identifier (e.g., "America/New_York").</param> |
| 21 | /// <param name="locale">The locale identifier (e.g., "en-US", "fr-FR").</param> |
| 22 | /// <returns>The created ClientInfoEntity that was added to the activity.</returns> |
| 23 | public static ClientInfoEntity AddClientInfo(this TeamsActivity activity, string platform, string country, string timeZone, string locale) |
| 24 | { |
| 25 | ArgumentNullException.ThrowIfNull(activity); |
| 26 | |
| 27 | ClientInfoEntity clientInfo = new(platform, country, timeZone, locale); |
| 28 | activity.Entities ??= []; |
| 29 | activity.Entities.Add(clientInfo); |
| 30 | activity.Rebase(); |
| 31 | return clientInfo; |
| 32 | } |
| 33 | |
| 34 | /// <summary> |
| 35 | /// Retrieves the client information entity from the activity's entity collection. |
| 36 | /// </summary> |
| 37 | /// <param name="activity">The activity to extract client information from. Cannot be null.</param> |
| 38 | /// <returns>The ClientInfoEntity if found in the activity's entities; otherwise, null.</returns> |
| 39 | public static ClientInfoEntity? GetClientInfo(this TeamsActivity activity) |
| 40 | { |
| 41 | ArgumentNullException.ThrowIfNull(activity); |
| 42 | if (activity.Entities == null) |
| 43 | { |
| 44 | return null; |
| 45 | } |
| 46 | ClientInfoEntity? clientInfo = activity.Entities.FirstOrDefault(e => e is ClientInfoEntity) as ClientInfoEntity; |
| 47 | |
| 48 | return clientInfo; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /// <summary> |
| 53 | /// Client info entity. |
| 54 | /// </summary> |
| 55 | public class ClientInfoEntity : Entity |
| 56 | { |
| 57 | /// <summary> |
| 58 | /// Creates a new instance of <see cref="ClientInfoEntity"/>. |
| 59 | /// </summary> |
| 60 | public ClientInfoEntity() : base("clientInfo") |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /// <summary> |
| 66 | /// Initializes a new instance of the <see cref="ClientInfoEntity"/> class with specified client information. |
| 67 | /// </summary> |
| 68 | /// <param name="platform">The platform identifier (e.g., "web", "desktop", "mobile").</param> |
| 69 | /// <param name="country">The country code (e.g., "US", "GB").</param> |
| 70 | /// <param name="timezone">The time zone identifier (e.g., "America/New_York").</param> |
| 71 | /// <param name="locale">The locale identifier (e.g., "en-US", "fr-FR").</param> |
| 72 | public ClientInfoEntity(string platform, string country, string timezone, string locale) : base("clientInfo") |
| 73 | { |
| 74 | Locale = locale; |
| 75 | Country = country; |
| 76 | Platform = platform; |
| 77 | Timezone = timezone; |
| 78 | } |
| 79 | |
| 80 | /// <summary> |
| 81 | /// Gets or sets the locale information. |
| 82 | /// </summary> |
| 83 | [JsonPropertyName("locale")] |
| 84 | public string? Locale |
| 85 | { |
| 86 | get => base.Properties.TryGetValue("locale", out object? value) ? value?.ToString() : null; |
| 87 | set => base.Properties["locale"] = value; |
| 88 | } |
| 89 | |
| 90 | /// <summary> |
| 91 | /// Gets or sets the country information. |
| 92 | /// </summary> |
| 93 | [JsonPropertyName("country")] |
| 94 | public string? Country |
| 95 | { |
| 96 | get => base.Properties.TryGetValue("country", out object? value) ? value?.ToString() : null; |
| 97 | set => base.Properties["country"] = value; |
| 98 | } |
| 99 | |
| 100 | /// <summary> |
| 101 | /// Gets or sets the platform information. |
| 102 | /// </summary> |
| 103 | [JsonPropertyName("platform")] |
| 104 | public string? Platform |
| 105 | { |
| 106 | get => base.Properties.TryGetValue("platform", out object? value) ? value?.ToString() : null; |
| 107 | set => base.Properties["platform"] = value; |
| 108 | } |
| 109 | |
| 110 | /// <summary> |
| 111 | /// Gets or sets the timezone information. |
| 112 | /// </summary> |
| 113 | [JsonPropertyName("timezone")] |
| 114 | public string? Timezone |
| 115 | { |
| 116 | get => base.Properties.TryGetValue("timezone", out object? value) ? value?.ToString() : null; |
| 117 | set => base.Properties["timezone"] = value; |
| 118 | } |
| 119 | } |
| 120 | |