microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v2.0.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

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

112lines · 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 activity.From ??= Ref.Bot;
57 activity.Recipient ??= Ref.User;
58 activity.Conversation ??= Ref.Conversation;
59
60 var res = await Sender.Send(activity, Ref, CancellationToken);
61 await OnActivitySent(res, ToActivityType<IActivity>());
62 return res;
63 }
64
65 public Task<MessageActivity> Send(string text)
66 {
67 return Send(new MessageActivity(text));
68 }
69
70 public Task<MessageActivity> Send(Cards.AdaptiveCard card)
71 {
72 return Send(new MessageActivity().AddAttachment(card));
73 }
74
75 public Task<T> Reply<T>(T activity) where T : IActivity
76 {
77 activity.Conversation = Ref.Conversation.Copy();
78 activity.Conversation.Id = Ref.Conversation.ThreadId;
79
80 if (activity is MessageActivity message)
81 {
82 message.Text = string.Join("\n", [
83 Activity.ToQuoteReply(),
84 message.Text != string.Empty ? $"<p>{message.Text}</p>" : string.Empty
85 ]);
86 }
87
88 return Send(activity);
89 }
90
91 public Task<MessageActivity> Reply(string text)
92 {
93 return Reply(new MessageActivity(text));
94 }
95
96 public Task<MessageActivity> Reply(Cards.AdaptiveCard card)
97 {
98 return Reply(new MessageActivity().AddAttachment(card));
99 }
100
101 public Task<TypingActivity> Typing(string? text = null)
102 {
103 var activity = new TypingActivity();
104
105 if (text is not null)
106 {
107 activity.Text = text;
108 }
109
110 return Send(activity);
111 }
112}