microsoft/teams.net

Public

mirrored fromhttps://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

Libraries/Microsoft.Teams.Apps/Contexts/Client/FunctionContext.cs

106lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4using Microsoft.Teams.Api;
5using Microsoft.Teams.Api.Activities;
6using Microsoft.Teams.Api.Clients;
7using Microsoft.Teams.Cards;
8using Microsoft.Teams.Common.Logging;
9
10namespace Microsoft.Teams.Apps;
11
12/// <summary>
13/// context that comes from client (tab/embed) requests
14/// for remote function calls
15/// </summary>
16public 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 public Task<TActivity> Send<TActivity>(TActivity activity) where TActivity : IActivity;
38
39 /// <summary>
40 /// send a message activity to the conversation
41 /// </summary>
42 /// <param name="text">the text to send</param>
43 public Task<MessageActivity> Send(string text);
44
45 /// <summary>
46 /// send a message activity with a card attachment
47 /// </summary>
48 /// <param name="card">the card to send as an attachment</param>
49 public Task<MessageActivity> Send(AdaptiveCard card);
50}
51
52/// <summary>
53/// context that comes from client (tab/embed) requests
54/// for remote function calls
55/// </summary>
56public class FunctionContext<T>(App app) : ClientContext, IFunctionContext<T>
57{
58 public required ApiClient Api { get; set; }
59 public required ILogger Log { get; set; }
60 public required T Data { get; set; }
61
62 public async Task<TActivity> Send<TActivity>(TActivity activity) where TActivity : IActivity
63 {
64 var conversationId = ConversationId ?? activity.Conversation?.Id;
65
66 // Conversation ID can be missing if the app is running in a personal scope. In this case, create
67 // a conversation between the bot and the user. This will either create a new conversation or return
68 // a pre-existing one.
69 if (conversationId is null)
70 {
71 var res = await Api.Conversations.CreateAsync(new()
72 {
73 TenantId = TenantId,
74 IsGroup = false,
75 Bot = new()
76 {
77 Id = app.Id,
78 Name = app.Name,
79 Role = Role.Bot
80 },
81 Members = [
82 new()
83 {
84 Id = UserId,
85 Name = UserName,
86 Role = Role.User,
87 }
88 ]
89 });
90
91 conversationId = res.Id;
92 }
93
94 return await app.Send(conversationId, activity);
95 }
96
97 public Task<MessageActivity> Send(string text)
98 {
99 return Send(new MessageActivity(text));
100 }
101
102 public Task<MessageActivity> Send(AdaptiveCard card)
103 {
104 return Send(new MessageActivity().AddAttachment(card));
105 }
106}