-
Notifications
You must be signed in to change notification settings - Fork 840
/
Copy pathTokenBot.cs
60 lines (52 loc) · 2.6 KB
/
TokenBot.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
// <copyright file="TokenBot.cs" company="Microsoft">
// Copyright (c) Microsoft. All rights reserved.
// </copyright>
namespace TokenApp.Bots
{
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;
using TokenApp.Extensions;
using TokenApp.Repository;
/// <summary>
/// Meeting Token Bot.
/// </summary>
public class TokenBot : ActivityHandler
{
private readonly ITenantInfoRepository tenantInfoRepository;
/// <summary>
/// Initializes a new instance of the <see cref="TokenBot"/> class.
/// </summary>
/// <param name="tenantInfoRepository">tenant information repository.</param>
public TokenBot(ITenantInfoRepository tenantInfoRepository)
{
this.tenantInfoRepository = tenantInfoRepository;
}
/// <inheritdoc/>
protected override Task OnConversationUpdateActivityAsync(ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
// NOTE: The implementation of ITenantInfoRepository included in this sample uses an in-memory store,
// so the service URL mapping is lost when the service is restarted.
// To set the service URL after an service restart, send the bot a message from the chat. The OnMessageActivityAsync handler
// below will set the service URL from the information in the message activity.
// Set the service url for the tenant based on the incoming activity when the app is installed
var activity = turnContext.Activity;
if (!activity.MembersAdded.IsNullOrEmpty() &&
activity.MembersAdded.Any(acct => acct.Id == activity.Recipient.Id))
{
// App was installed to a chat
this.tenantInfoRepository.SetServiceUrl(turnContext.Activity.Conversation.TenantId, turnContext.Activity.ServiceUrl);
}
return base.OnConversationUpdateActivityAsync(turnContext, cancellationToken);
}
/// <inheritdoc/>
protected override Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
// NOTE: This handler is included here only to make it easy to set the service URL information after a service restart.
this.tenantInfoRepository.SetServiceUrl(turnContext.Activity.Conversation.TenantId, turnContext.Activity.ServiceUrl);
return base.OnMessageActivityAsync(turnContext, cancellationToken);
}
}
}