microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
feature/user-agent-header

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

core/src/Microsoft.Teams.Bot.Apps/Context.cs

67lines · modecode

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