microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/fix-typo-in-documentation

Branches

Tags

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

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.Apps/Contexts/Context.Send.cs

108lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4using Microsoft.Teams.Api.Activities;
5
6namespace Microsoft.Teams.Apps;
7
8public partial interface IContext<TActivity>
9{
10 /// <summary>
11 /// send an activity to the conversation
12 /// </summary>
13 /// <param name="activity">activity activity to send</param>
14 public Task<T> Send<T>(T activity) where T : IActivity;
15
16 /// <summary>
17 /// send a message activity to the conversation
18 /// </summary>
19 /// <param name="text">the text to send</param>
20 public Task<MessageActivity> Send(string text);
21
22 /// <summary>
23 /// send a message activity with a card attachment
24 /// </summary>
25 /// <param name="card">the card to send as an attachment</param>
26 public Task<MessageActivity> Send(Cards.AdaptiveCard card);
27
28 /// <summary>
29 /// send an activity to the conversation as a reply
30 /// </summary>
31 /// <param name="activity">activity activity to send</param>
32 public Task<T> Reply<T>(T activity) where T : IActivity;
33
34 /// <summary>
35 /// send a message activity to the conversation as a reply
36 /// </summary>
37 /// <param name="text">the text to send</param>
38 public Task<MessageActivity> Reply(string text);
39
40 /// <summary>
41 /// send a message activity with a card attachment as a reply
42 /// </summary>
43 /// <param name="card">the card to send as an attachment</param>
44 public Task<MessageActivity> Reply(Cards.AdaptiveCard card);
45
46 /// <summary>
47 /// send a typing activity
48 /// </summary>
49 public Task<TypingActivity> Typing(string? text = null);
50}
51
52public partial class Context<TActivity> : IContext<TActivity>
53{
54 public async Task<T> Send<T>(T activity) where T : IActivity
55 {
56 var res = await Sender.Send(activity, Ref, CancellationToken);
57 await OnActivitySent(res, ToActivityType<IActivity>());
58 return res;
59 }
60
61 public Task<MessageActivity> Send(string text)
62 {
63 return Send(new MessageActivity(text));
64 }
65
66 public Task<MessageActivity> Send(Cards.AdaptiveCard card)
67 {
68 return Send(new MessageActivity().AddAttachment(card));
69 }
70
71 public Task<T> Reply<T>(T activity) where T : IActivity
72 {
73 activity.Conversation = Ref.Conversation.Copy();
74 activity.Conversation.Id = Ref.Conversation.ThreadId;
75
76 if (activity is MessageActivity message)
77 {
78 message.Text = string.Join("\n", [
79 Activity.ToQuoteReply(),
80 message.Text != string.Empty ? $"<p>{message.Text}</p>" : string.Empty
81 ]);
82 }
83
84 return Send(activity);
85 }
86
87 public Task<MessageActivity> Reply(string text)
88 {
89 return Reply(new MessageActivity(text));
90 }
91
92 public Task<MessageActivity> Reply(Cards.AdaptiveCard card)
93 {
94 return Reply(new MessageActivity().AddAttachment(card));
95 }
96
97 public Task<TypingActivity> Typing(string? text = null)
98 {
99 var activity = new TypingActivity();
100
101 if (text is not null)
102 {
103 activity.Text = text;
104 }
105
106 return Send(activity);
107 }
108}