microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Apps/Contexts/Client/FunctionContext.cs
101lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Teams.Api; |
| 5 | using Microsoft.Teams.Api.Activities; |
| 6 | using Microsoft.Teams.Api.Clients; |
| 7 | using Microsoft.Teams.Cards; |
| 8 | using Microsoft.Teams.Common.Logging; |
| 9 | |
| 10 | namespace Microsoft.Teams.Apps; |
| 11 | |
| 12 | /// <summary> |
| 13 | /// context that comes from client (tab/embed) requests |
| 14 | /// for remote function calls |
| 15 | /// </summary> |
| 16 | public interface IFunctionContext<T> : IClientContext |
| 17 | { |
| 18 | /// <summary> |
| 19 | /// the api client |
| 20 | /// </summary> |
| 21 | public ApiClient Api { get; } |
| 22 | |
| 23 | /// <summary> |
| 24 | /// the app logger instance |
| 25 | /// </summary> |
| 26 | public ILogger Log { get; } |
| 27 | |
| 28 | /// <summary> |
| 29 | /// the function payload |
| 30 | /// </summary> |
| 31 | public T Data { get; } |
| 32 | |
| 33 | /// <summary> |
| 34 | /// send an activity to the conversation |
| 35 | /// </summary> |
| 36 | /// <param name="activity">activity activity to send</param> |
| 37 | /// <param name="cancellationToken">optional cancellation token</param> |
| 38 | public Task<TActivity> Send<TActivity>(TActivity activity, CancellationToken cancellationToken = default) where TActivity : IActivity; |
| 39 | |
| 40 | /// <summary> |
| 41 | /// send a message activity to the conversation |
| 42 | /// </summary> |
| 43 | /// <param name="text">the text to send</param> |
| 44 | /// <param name="cancellationToken">optional cancellation token</param> |
| 45 | public Task<MessageActivity> Send(string text, CancellationToken cancellationToken = default); |
| 46 | |
| 47 | /// <summary> |
| 48 | /// send a message activity with a card attachment |
| 49 | /// </summary> |
| 50 | /// <param name="card">the card to send as an attachment</param> |
| 51 | /// <param name="cancellationToken">optional cancellation token</param> |
| 52 | public Task<MessageActivity> Send(AdaptiveCard card, CancellationToken cancellationToken = default); |
| 53 | } |
| 54 | |
| 55 | /// <summary> |
| 56 | /// context that comes from client (tab/embed) requests |
| 57 | /// for remote function calls |
| 58 | /// </summary> |
| 59 | public class FunctionContext<T>(App app) : ClientContext, IFunctionContext<T> |
| 60 | { |
| 61 | public required ApiClient Api { get; set; } |
| 62 | public required ILogger Log { get; set; } |
| 63 | public required T Data { get; set; } |
| 64 | |
| 65 | public async Task<TActivity> Send<TActivity>(TActivity activity, CancellationToken cancellationToken = default) where TActivity : IActivity |
| 66 | { |
| 67 | var conversationId = ConversationId ?? activity.Conversation?.Id; |
| 68 | |
| 69 | // Conversation ID can be missing if the app is running in a personal scope. In this case, create |
| 70 | // a conversation between the bot and the user. This will either create a new conversation or return |
| 71 | // a pre-existing one. |
| 72 | if (conversationId is null) |
| 73 | { |
| 74 | var res = await Api.Conversations.CreateAsync(new() |
| 75 | { |
| 76 | TenantId = TenantId, |
| 77 | Members = [ |
| 78 | new() |
| 79 | { |
| 80 | Id = UserId, |
| 81 | Name = UserName, |
| 82 | } |
| 83 | ] |
| 84 | }).ConfigureAwait(false); |
| 85 | |
| 86 | conversationId = res.Id; |
| 87 | } |
| 88 | |
| 89 | return await app.Send(conversationId, activity, cancellationToken: cancellationToken).ConfigureAwait(false); |
| 90 | } |
| 91 | |
| 92 | public Task<MessageActivity> Send(string text, CancellationToken cancellationToken = default) |
| 93 | { |
| 94 | return Send(new MessageActivity(text), cancellationToken); |
| 95 | } |
| 96 | |
| 97 | public Task<MessageActivity> Send(AdaptiveCard card, CancellationToken cancellationToken = default) |
| 98 | { |
| 99 | return Send(new MessageActivity().AddAttachment(card), cancellationToken); |
| 100 | } |
| 101 | } |