This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
MessagesController.cs
314 lines (276 loc) · 15 KB
/
MessagesController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using Autofac;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Dialogs.Internals;
using Microsoft.Bot.Connector;
using Microsoft.Bot.Connector.Teams;
using Microsoft.Bot.Connector.Teams.Models;
using Microsoft.Teams.TemplateBotCSharp.Dialogs;
using Microsoft.Teams.TemplateBotCSharp.Properties;
using Microsoft.Teams.TemplateBotCSharp.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
namespace Microsoft.Teams.TemplateBotCSharp
{
[BotAuthentication]
public class MessagesController : ApiController
{
/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity activity, CancellationToken cancellationToken)
{
var connectorClient = new ConnectorClient(new Uri(activity.ServiceUrl));
if (activity.Type == ActivityTypes.Message)
{
// Special handling for a command to simulate a reset of the bot chat
if (!(activity.Conversation.IsGroup ?? false) && (activity.Text == "/resetbotchat"))
{
return await HandleResetBotChatAsync(activity, cancellationToken);
}
//Set the Locale for Bot
activity.Locale = TemplateUtility.GetLocale(activity);
//Strip At mention from incoming request text
activity = Middleware.StripAtMentionText(activity);
//Convert incoming activity text to lower case, to match the intent irrespective of incoming text case
activity = Middleware.ConvertActivityTextToLower(activity);
//Set the OFFICE_365_TENANT_FILTER key in web.config file with Tenant Information
//Validate bot for specific teams tenant if any
if (Middleware.RejectMessageBasedOnTenant(activity, activity.GetTenantId()))
{
Activity replyActivity = activity.CreateReply();
replyActivity.Text = Strings.TenantLevelDeniedAccess;
await connectorClient.Conversations.ReplyToActivityAsync(replyActivity);
return Request.CreateResponse(HttpStatusCode.OK);
}
// Set activity text if request is from an adaptive card submit action
activity = Middleware.AdaptiveCardSubmitActionHandler(activity);
await Conversation.SendAsync(activity, () => new RootDialog());
}
else if (activity.Type == ActivityTypes.MessageReaction)
{
var reactionsAdded = activity.ReactionsAdded;
var reactionsRemoved = activity.ReactionsRemoved;
var replytoId = activity.ReplyToId;
Activity reply;
if (reactionsAdded != null && reactionsAdded.Count > 0)
{
reply = activity.CreateReply(Strings.LikeMessage);
await connectorClient.Conversations.ReplyToActivityAsync(reply);
}
else if (reactionsRemoved != null && reactionsRemoved.Count > 0)
{
reply = activity.CreateReply(Strings.RemoveLike);
await connectorClient.Conversations.ReplyToActivityAsync(reply);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
else if (activity.Type == ActivityTypes.Invoke) // Received an invoke
{
// Handle ComposeExtension query
if (activity.IsComposeExtensionQuery())
{
WikipediaComposeExtension wikipediaComposeExtension = new WikipediaComposeExtension();
HttpResponseMessage httpResponse = null;
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
{
var botDataStore = scope.Resolve<IBotDataStore<BotData>>();
// Handle compose extension selected item
if (activity.Name == "composeExtension/selectItem")
{
// This handler is used to process the event when a user in Teams selects wiki item from wiki result
ComposeExtensionResponse selectedItemResponse = await wikipediaComposeExtension.HandleComposeExtensionSelectedItem(activity, botDataStore);
httpResponse = Request.CreateResponse<ComposeExtensionResponse>(HttpStatusCode.OK, selectedItemResponse);
}
else
{
// Handle the wiki compose extension request and returned the wiki result response
ComposeExtensionResponse composeExtensionResponse = await wikipediaComposeExtension.GetComposeExtensionResponse(activity, botDataStore);
httpResponse = Request.CreateResponse<ComposeExtensionResponse>(HttpStatusCode.OK, composeExtensionResponse);
}
var address = Address.FromActivity(activity);
await botDataStore.FlushAsync(address, CancellationToken.None);
}
return httpResponse;
}
//Actionable Message
else if (activity.IsO365ConnectorCardActionQuery())
{
// this will handle the request coming any action on Actionable messages
return await HandleO365ConnectorCardActionQuery(activity);
}
//PopUp SignIn
else if (activity.Name == "signin/verifyState")
{
// this will handle the request coming from PopUp SignIn
return await PopUpSignInHandler(activity);
}
// Handle rest of the invoke request
else
{
var messageActivity = (IMessageActivity)null;
//this will parse the invoke value and change the message activity as well
messageActivity = InvokeHandler.HandleInvokeRequest(activity);
await Conversation.SendAsync(messageActivity, () => new Dialogs.RootDialog());
return Request.CreateResponse(HttpStatusCode.OK);
}
}
else
{
await HandleSystemMessageAsync(activity, connectorClient, cancellationToken);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
private async Task HandleSystemMessageAsync(Activity message, ConnectorClient connectorClient, CancellationToken cancellationToken)
{
if (message.Type == ActivityTypes.DeleteUserData)
{
// Implement user deletion here
// If we handle user deletion, return a real message
}
else if (message.Type == ActivityTypes.ConversationUpdate)
{
// This shows how to send a welcome message in response to a conversationUpdate event
// We're only interested in member added events
if (message.MembersAdded?.Count > 0)
{
// Determine if the bot was added to the team/conversation
var botId = message.Recipient.Id;
var botWasAdded = message.MembersAdded.Any(member => member.Id == botId);
// Create the welcome message to send
Activity welcomeMessage = message.CreateReply();
welcomeMessage.Text = Strings.BotWelcomeMessage;
if (!(message.Conversation.IsGroup ?? false))
{
// 1:1 conversation event
// If the user hasn't received a first-run message yet, then send a message to the user
// introducing your bot and what it can do. Do NOT send this blindly, as your bot can receive
// spurious conversationUpdate events, especially if you use proactive messaging.
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
{
var address = Address.FromActivity(message);
var botDataStore = scope.Resolve<IBotDataStore<BotData>>();
var botData = await botDataStore.LoadAsync(address, BotStoreType.BotUserData, cancellationToken);
if (!botData.GetProperty<bool>("IsFreSent"))
{
await connectorClient.Conversations.ReplyToActivityWithRetriesAsync(welcomeMessage, cancellationToken);
// Remember that we sent the welcome message already
botData.SetProperty("IsFreSent", true);
await botDataStore.SaveAsync(address, BotStoreType.BotUserData, botData, cancellationToken);
}
else
{
// First-run message has already been sent, so skip sending it again.
// Do not remove the check for IsFreSent above. Your bot can receive spurious conversationUpdate
// activities from chat service, so if you always respond to all of them, you will send random
// welcome messages to users who have already received the welcome.
}
}
}
else
{
// Not 1:1 chat event (bot or user was added to a team or group chat)
if (botWasAdded)
{
// Bot was added to the team
// Send a message to the team's channel, introducing your bot and what you can do
await connectorClient.Conversations.ReplyToActivityWithRetriesAsync(welcomeMessage, cancellationToken);
}
else
{
// Other users were added to the team/conversation
}
}
}
}
else if (message.Type == ActivityTypes.ContactRelationUpdate)
{
// Handle add/remove from contact lists
// Activity.From + Activity.Action represent what happened
}
else if (message.Type == ActivityTypes.Typing)
{
// Handle knowing that the user is typing
}
else if (message.Type == ActivityTypes.Ping)
{
}
}
/// <summary>
/// Handles a request from the user to simulate a new chat.
/// </summary>
/// <param name="message">The incoming message requesting the reset</param>
/// <param name="cancellationToken">The cancellation token</param>
/// <returns></returns>
private async Task<HttpResponseMessage> HandleResetBotChatAsync(Activity message, CancellationToken cancellationToken)
{
// Forget everything we know about the user
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
{
var address = Address.FromActivity(message);
var botDataStore = scope.Resolve<IBotDataStore<BotData>>();
await botDataStore.SaveAsync(address, BotStoreType.BotUserData, new BotData("*"), cancellationToken);
await botDataStore.SaveAsync(address, BotStoreType.BotConversationData, new BotData("*"), cancellationToken);
await botDataStore.SaveAsync(address, BotStoreType.BotPrivateConversationData, new BotData("*"), cancellationToken);
}
// If you need to reset the user state in other services your app uses, do it here.
// Synthesize a conversation update event and simulate the bot receiving it
// Note that this is a fake event, as Teams does not support deleting a 1:1 conversation and re-creating it
var conversationUpdateMessage = new Activity {
Type = ActivityTypes.ConversationUpdate,
Id = message.Id,
ServiceUrl = message.ServiceUrl,
From = message.From,
Recipient = message.Recipient,
Conversation = message.Conversation,
ChannelData = message.ChannelData,
ChannelId = message.ChannelId,
Timestamp = message.Timestamp,
MembersAdded = new List<ChannelAccount> { message.From, message.Recipient },
};
return await this.Post(conversationUpdateMessage, cancellationToken);
}
/// <summary>
/// Handles O365 connector card action queries.
/// </summary>
/// <param name="activity">Incoming request from Bot Framework.</param>
/// <param name="connectorClient">Connector client instance for posting to Bot Framework.</param>
/// <returns>Task tracking operation.</returns>
private static async Task<HttpResponseMessage> HandleO365ConnectorCardActionQuery(Activity activity)
{
var connectorClient = new ConnectorClient(new Uri(activity.ServiceUrl));
// Get O365 connector card query data.
O365ConnectorCardActionQuery o365CardQuery = activity.GetO365ConnectorCardActionQueryData();
Activity replyActivity = activity.CreateReply();
replyActivity.TextFormat = "xml";
replyActivity.Text = $@"
<h2>Thanks, {activity.From.Name}</h2><br/>
<h3>Your input action ID:</h3><br/>
<pre>{o365CardQuery.ActionId}</pre><br/>
<h3>Your input body:</h3><br/>
<pre>{o365CardQuery.Body}</pre>";
await connectorClient.Conversations.ReplyToActivityWithRetriesAsync(replyActivity);
return new HttpResponseMessage(HttpStatusCode.OK);
}
/// <summary>
/// Handle the PopUp SignIn requests
/// </summary>
/// <param name="activity"></param>
/// <returns></returns>
private static async Task<HttpResponseMessage> PopUpSignInHandler(Activity activity)
{
var connectorClient = new ConnectorClient(new Uri(activity.ServiceUrl));
Activity replyActivity = activity.CreateReply();
replyActivity.Text = $@"Authentication Successful";
await connectorClient.Conversations.ReplyToActivityWithRetriesAsync(replyActivity);
return new HttpResponseMessage(HttpStatusCode.OK);
}
}
}