microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Apps/Contexts/Context.Send.cs
138lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Teams.Api.Activities; |
| 5 | |
| 6 | namespace Microsoft.Teams.Apps; |
| 7 | |
| 8 | public 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 | /// <param name="isTargeted">whether the activity is targeted</param> |
| 15 | /// <remarks> |
| 16 | /// <para>The <paramref name="isTargeted"/> parameter is in preview.</para> |
| 17 | /// <para>Targeted messages are delivered privately to the recipient specified in the activity's Recipient property.</para> |
| 18 | /// </remarks> |
| 19 | public Task<T> Send<T>(T activity, bool isTargeted = false) where T : IActivity; |
| 20 | |
| 21 | /// <summary> |
| 22 | /// send a message activity to the conversation |
| 23 | /// </summary> |
| 24 | /// <param name="text">the text to send</param> |
| 25 | /// <param name="isTargeted">whether the activity is targeted</param> |
| 26 | /// <remarks> |
| 27 | /// <para>The <paramref name="isTargeted"/> parameter is in preview.</para> |
| 28 | /// <para>Targeted messages are delivered privately to the recipient specified in the activity's Recipient property.</para> |
| 29 | /// </remarks> |
| 30 | public Task<MessageActivity> Send(string text, bool isTargeted = false); |
| 31 | |
| 32 | /// <summary> |
| 33 | /// send a message activity with a card attachment |
| 34 | /// </summary> |
| 35 | /// <param name="card">the card to send as an attachment</param> |
| 36 | /// <param name="isTargeted">whether the activity is targeted</param> |
| 37 | /// <remarks> |
| 38 | /// <para>The <paramref name="isTargeted"/> parameter is in preview.</para> |
| 39 | /// <para>Targeted messages are delivered privately to the recipient specified in the activity's Recipient property.</para> |
| 40 | /// </remarks> |
| 41 | public Task<MessageActivity> Send(Cards.AdaptiveCard card, bool isTargeted = false); |
| 42 | |
| 43 | /// <summary> |
| 44 | /// send an activity to the conversation as a reply |
| 45 | /// </summary> |
| 46 | /// <param name="activity">activity activity to send</param> |
| 47 | /// <param name="isTargeted">whether the activity is targeted</param> |
| 48 | /// <remarks> |
| 49 | /// <para>The <paramref name="isTargeted"/> parameter is in preview.</para> |
| 50 | /// <para>Targeted messages are delivered privately to the recipient specified in the activity's Recipient property.</para> |
| 51 | /// </remarks> |
| 52 | public Task<T> Reply<T>(T activity, bool isTargeted = false) where T : IActivity; |
| 53 | |
| 54 | /// <summary> |
| 55 | /// send a message activity to the conversation as a reply |
| 56 | /// </summary> |
| 57 | /// <param name="text">the text to send</param> |
| 58 | /// <param name="isTargeted">whether the activity is targeted</param> |
| 59 | /// <remarks> |
| 60 | /// <para>The <paramref name="isTargeted"/> parameter is in preview.</para> |
| 61 | /// <para>Targeted messages are delivered privately to the recipient specified in the activity's Recipient property.</para> |
| 62 | /// </remarks> |
| 63 | public Task<MessageActivity> Reply(string text, bool isTargeted = false); |
| 64 | |
| 65 | /// <summary> |
| 66 | /// send a message activity with a card attachment as a reply |
| 67 | /// </summary> |
| 68 | /// <param name="card">the card to send as an attachment</param> |
| 69 | /// <param name="isTargeted">whether the activity is targeted</param> |
| 70 | /// <remarks> |
| 71 | /// <para>The <paramref name="isTargeted"/> parameter is in preview.</para> |
| 72 | /// <para>Targeted messages are delivered privately to the recipient specified in the activity's Recipient property.</para> |
| 73 | /// </remarks> |
| 74 | public Task<MessageActivity> Reply(Cards.AdaptiveCard card, bool isTargeted = false); |
| 75 | |
| 76 | /// <summary> |
| 77 | /// send a typing activity |
| 78 | /// </summary> |
| 79 | public Task<TypingActivity> Typing(string? text = null); |
| 80 | } |
| 81 | |
| 82 | public partial class Context<TActivity> : IContext<TActivity> |
| 83 | { |
| 84 | public async Task<T> Send<T>(T activity, bool isTargeted = false) where T : IActivity |
| 85 | { |
| 86 | var res = await Sender.Send(activity, Ref, isTargeted, CancellationToken); |
| 87 | await OnActivitySent(res, ToActivityType<IActivity>()); |
| 88 | return res; |
| 89 | } |
| 90 | |
| 91 | public Task<MessageActivity> Send(string text, bool isTargeted = false) |
| 92 | { |
| 93 | return Send(new MessageActivity(text), isTargeted); |
| 94 | } |
| 95 | |
| 96 | public Task<MessageActivity> Send(Cards.AdaptiveCard card, bool isTargeted = false) |
| 97 | { |
| 98 | return Send(new MessageActivity().AddAttachment(card), isTargeted); |
| 99 | } |
| 100 | |
| 101 | public Task<T> Reply<T>(T activity, bool isTargeted = false) where T : IActivity |
| 102 | { |
| 103 | activity.Conversation = Ref.Conversation.Copy(); |
| 104 | activity.Conversation.Id = Ref.Conversation.ThreadId; |
| 105 | |
| 106 | if (activity is MessageActivity message) |
| 107 | { |
| 108 | message.Text = string.Join("\n", [ |
| 109 | Activity.ToQuoteReply(), |
| 110 | message.Text != string.Empty ? $"<p>{message.Text}</p>" : string.Empty |
| 111 | ]); |
| 112 | } |
| 113 | |
| 114 | return Send(activity, isTargeted); |
| 115 | } |
| 116 | |
| 117 | public Task<MessageActivity> Reply(string text, bool isTargeted = false) |
| 118 | { |
| 119 | return Reply(new MessageActivity(text), isTargeted); |
| 120 | } |
| 121 | |
| 122 | public Task<MessageActivity> Reply(Cards.AdaptiveCard card, bool isTargeted = false) |
| 123 | { |
| 124 | return Reply(new MessageActivity().AddAttachment(card), isTargeted); |
| 125 | } |
| 126 | |
| 127 | public Task<TypingActivity> Typing(string? text = null) |
| 128 | { |
| 129 | var activity = new TypingActivity(); |
| 130 | |
| 131 | if (text is not null) |
| 132 | { |
| 133 | activity.Text = text; |
| 134 | } |
| 135 | |
| 136 | return Send(activity); |
| 137 | } |
| 138 | } |