-
Notifications
You must be signed in to change notification settings - Fork 733
/
ProactiveMsgTo1to1Dialog.cs
79 lines (68 loc) · 3.42 KB
/
ProactiveMsgTo1to1Dialog.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
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using Microsoft.Bot.Schema;
using Microsoft.Bot.Schema.Teams;
using Microsoft.Extensions.Options;
using AppCompleteSample.src.dialogs;
using AppCompleteSample.Utility;
using AppCompleteSample;
using System;
using System.Threading.Tasks;
using System.Threading;
namespace AppCompleteSample.Dialogs
{
/// <summary>
/// This is Proactive Message Dialog Class. Main purpose of this class is to show the Send Proactive Message Example
/// </summary>
public class ProactiveMsgTo1to1Dialog : ComponentDialog
{
protected readonly IStatePropertyAccessor<RootDialogState> _conversationState;
private readonly IOptions<AzureSettings> azureSettings;
public ProactiveMsgTo1to1Dialog(IStatePropertyAccessor<RootDialogState> conversationState, IOptions<AzureSettings> azureSettings) : base(nameof(ProactiveMsgTo1to1Dialog))
{
this._conversationState = conversationState;
this.azureSettings = azureSettings;
InitialDialogId = nameof(WaterfallDialog);
AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
{
BeginProactiveMsgTo1to1DialogAsync,
}));
}
private async Task<DialogTurnResult> BeginProactiveMsgTo1to1DialogAsync(
WaterfallStepContext stepContext,
CancellationToken cancellationToken = default(CancellationToken))
{
await stepContext.Context.SendActivityAsync(Strings.Send1on1ConfirmMsg);
if (stepContext == null)
{
throw new ArgumentNullException(nameof(stepContext));
}
//Set the Last Dialog in Conversation Data
var currentState = await this._conversationState.GetAsync(stepContext.Context, () => new RootDialogState());
currentState.LastDialogKey = Strings.LastDialogSend1on1Dialog;
await this._conversationState.SetAsync(stepContext.Context, currentState);
var userId = stepContext.Context.Activity.From.Id;
var botId = stepContext.Context.Activity.Recipient.Id;
var botName = stepContext.Context.Activity.Recipient.Name;
var channelData = stepContext.Context.Activity.GetChannelData<TeamsChannelData>();
var connectorClient = new ConnectorClient(new Uri(stepContext.Context.Activity.ServiceUrl), this.azureSettings.Value.MicrosoftAppId, this.azureSettings.Value.MicrosoftAppPassword);
var parameters = new ConversationParameters
{
Bot = new ChannelAccount(botId, botName),
Members = new ChannelAccount[] { new ChannelAccount(userId) },
ChannelData = new TeamsChannelData
{
Tenant = channelData.Tenant
}
};
var conversationResource = await connectorClient.Conversations.CreateConversationAsync(parameters);
var message = Activity.CreateMessageActivity();
message.From = new ChannelAccount(botId, botName);
message.Conversation = new ConversationAccount(id: conversationResource.Id.ToString());
message.Text = Strings.Send1on1Prompt;
await connectorClient.Conversations.SendToConversationAsync((Activity)message);
return await stepContext.EndDialogAsync(null, cancellationToken);
}
}
}