microsoft/teams.net

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
aamirj/StackOverflowTest

Branches

Tags

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

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore/AspNetCorePlugin.cs

142lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4using Microsoft.AspNetCore.Builder;
5using Microsoft.Teams.Api.Activities;
6using Microsoft.Teams.Api.Auth;
7using Microsoft.Teams.Api.Clients;
8using Microsoft.Teams.Apps;
9using Microsoft.Teams.Apps.Events;
10using Microsoft.Teams.Apps.Plugins;
11using Microsoft.Teams.Common.Http;
12using Microsoft.Teams.Common.Logging;
13
14namespace Microsoft.Teams.Plugins.AspNetCore;
15
16[Plugin]
17public partial class AspNetCorePlugin : ISenderPlugin, IAspNetCorePlugin
18{
19 [Dependency]
20 public ILogger Logger { get; set; }
21
22 [Dependency("Token", optional: true)]
23 public IToken? Token { get; set; }
24
25 [Dependency]
26 public IHttpClient Client { get; set; }
27
28 public event EventFunction Events;
29
30 public IApplicationBuilder Configure(IApplicationBuilder builder)
31 {
32 return builder;
33 }
34
35 public Task OnInit(App app, CancellationToken cancellationToken = default)
36 {
37 return Task.CompletedTask;
38 }
39
40 public Task OnStart(App app, CancellationToken cancellationToken = default)
41 {
42 Logger.Debug("OnStart");
43 return Task.CompletedTask;
44 }
45
46 public Task OnError(App app, IPlugin plugin, ErrorEvent @event, CancellationToken cancellationToken = default)
47 {
48 Logger.Debug("OnError");
49 return Task.CompletedTask;
50 }
51
52 public Task OnActivity(App app, ISenderPlugin sender, ActivityEvent @event, CancellationToken cancellationToken = default)
53 {
54 Logger.Debug("OnActivity");
55 return Task.CompletedTask;
56 }
57
58 public Task OnActivitySent(App app, ISenderPlugin sender, ActivitySentEvent @event, CancellationToken cancellationToken = default)
59 {
60 Logger.Debug("OnActivitySent");
61 return Task.CompletedTask;
62 }
63
64 public Task OnActivityResponse(App app, ISenderPlugin sender, ActivityResponseEvent @event, CancellationToken cancellationToken = default)
65 {
66 Logger.Debug("OnActivityResponse");
67 return Task.CompletedTask;
68 }
69
70 public Task<IActivity> Send(IActivity activity, Api.ConversationReference reference, CancellationToken cancellationToken = default)
71 {
72 return Send<IActivity>(activity, reference, cancellationToken);
73 }
74
75 public async Task<TActivity> Send<TActivity>(TActivity activity, Api.ConversationReference reference, CancellationToken cancellationToken = default) where TActivity : IActivity
76 {
77 var client = new ApiClient(reference.ServiceUrl, Client, cancellationToken);
78
79 activity.Conversation = reference.Conversation;
80 activity.From = reference.Bot;
81 activity.Recipient = reference.User;
82
83 if (activity.Id is not null && !activity.IsStreaming)
84 {
85 await client
86 .Conversations
87 .Activities
88 .UpdateAsync(reference.Conversation.Id, activity.Id, activity);
89
90 return activity;
91 }
92
93 var res = await client
94 .Conversations
95 .Activities
96 .CreateAsync(reference.Conversation.Id, activity);
97
98 activity.Id = res?.Id;
99 return activity;
100 }
101
102 public IStreamer CreateStream(Api.ConversationReference reference, CancellationToken cancellationToken = default)
103 {
104 return new Stream()
105 {
106 Send = async activity =>
107 {
108 var res = await Send(activity, reference, cancellationToken);
109 return res;
110 }
111 };
112 }
113
114 public async Task<Response> Do(ActivityEvent @event, CancellationToken cancellationToken = default)
115 {
116 try
117 {
118 var @out = await Events(
119 this,
120 "activity",
121 @event,
122 cancellationToken
123 );
124
125 var res = (Response?)@out ?? throw new Exception("expected activity response");
126 Logger.Debug(res);
127 return res;
128 }
129 catch (Exception ex)
130 {
131 Logger.Error(ex);
132 await Events(
133 this,
134 "error",
135 new ErrorEvent() { Exception = ex },
136 cancellationToken
137 );
138
139 return new Response(System.Net.HttpStatusCode.InternalServerError, ex.ToString());
140 }
141 }
142}