microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.Apps/Context.cs
64lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Teams.Bot.Apps.Schema; |
| 5 | using Microsoft.Teams.Bot.Core; |
| 6 | |
| 7 | namespace Microsoft.Teams.Bot.Apps; |
| 8 | |
| 9 | |
| 10 | /// <summary> |
| 11 | /// Context for a bot turn. |
| 12 | /// </summary> |
| 13 | /// <param name="botApplication"></param> |
| 14 | /// <param name="activity"></param> |
| 15 | public class Context<TActivity>(TeamsBotApplication botApplication, TActivity activity) where TActivity : TeamsActivity |
| 16 | { |
| 17 | /// <summary> |
| 18 | /// Base bot application. |
| 19 | /// </summary> |
| 20 | public TeamsBotApplication TeamsBotApplication { get; } = botApplication; |
| 21 | |
| 22 | /// <summary> |
| 23 | /// Current activity. |
| 24 | /// </summary> |
| 25 | public TActivity Activity { get; } = activity; |
| 26 | |
| 27 | /// <summary> |
| 28 | /// Sends a message activity as a reply. |
| 29 | /// </summary> |
| 30 | /// <param name="text"></param> |
| 31 | /// <param name="cancellationToken"></param> |
| 32 | /// <returns></returns> |
| 33 | public Task<SendActivityResponse?> SendActivityAsync(string text, CancellationToken cancellationToken = default) |
| 34 | => TeamsBotApplication.SendActivityAsync( |
| 35 | new TeamsActivityBuilder() |
| 36 | .WithConversationReference(Activity) |
| 37 | .WithText(text) |
| 38 | .Build(), cancellationToken); |
| 39 | |
| 40 | /// <summary> |
| 41 | /// Sends Activity |
| 42 | /// </summary> |
| 43 | /// <param name="activity"></param> |
| 44 | /// <param name="cancellationToken"></param> |
| 45 | /// <returns></returns> |
| 46 | public Task<SendActivityResponse?> SendActivityAsync(TeamsActivity activity, CancellationToken cancellationToken = default) |
| 47 | => TeamsBotApplication.SendActivityAsync( |
| 48 | new TeamsActivityBuilder(activity) |
| 49 | .WithConversationReference(Activity) |
| 50 | .Build(), cancellationToken); |
| 51 | |
| 52 | |
| 53 | /// <summary> |
| 54 | /// Sends a typing activity to the conversation asynchronously. |
| 55 | /// </summary> |
| 56 | /// <param name="cancellationToken"></param> |
| 57 | /// <returns></returns> |
| 58 | public Task<SendActivityResponse?> SendTypingActivityAsync(CancellationToken cancellationToken = default) |
| 59 | => TeamsBotApplication.SendActivityAsync( |
| 60 | new TeamsActivityBuilder() |
| 61 | .WithType(TeamsActivityType.Typing) |
| 62 | .WithConversationReference(Activity) |
| 63 | .Build(), cancellationToken); |
| 64 | } |
| 65 | |