forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SavedAddressDialog.cs
100 lines (89 loc) · 3.74 KB
/
SavedAddressDialog.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
namespace ContosoFlowers.BotAssets.Dialogs
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Location;
using Microsoft.Bot.Connector;
using Properties;
[Serializable]
public class SavedAddressDialog : IDialog<SavedAddressDialog.SavedAddressResult>
{
private readonly IDictionary<string, string> savedAddresses;
private readonly IEnumerable<string> saveOptionNames;
private readonly string prompt;
private readonly string useSavedAddressPrompt;
private readonly string saveAddressPrompt;
private readonly IDialogFactory dialogFactory;
private string currentAddress;
public SavedAddressDialog(
string prompt,
string useSavedAddressPrompt,
string saveAddressPrompt,
IDictionary<string, string> savedAddresses,
IEnumerable<string> saveOptionNames,
IDialogFactory dialogFactory)
{
this.savedAddresses = savedAddresses ?? new Dictionary<string, string>();
this.saveOptionNames = saveOptionNames;
this.prompt = prompt;
this.useSavedAddressPrompt = useSavedAddressPrompt;
this.saveAddressPrompt = saveAddressPrompt;
this.dialogFactory = dialogFactory;
}
public async Task StartAsync(IDialogContext context)
{
if (this.savedAddresses.Any())
{
PromptDialog.Choice(context, this.AfterSelectSavedAddress, this.savedAddresses.Values.Concat(new[] { Resources.SavedAddressDialog_AddNewAddress }), this.useSavedAddressPrompt);
}
else
{
this.AddressPrompt(context);
}
}
private void AddressPrompt(IDialogContext context)
{
// BotBuilder's LocationDialog
// Leverage DI to inject other parameters
var locationDialog = this.dialogFactory.Create<LocationDialog>(
new Dictionary<string, object>()
{
{ "prompt", this.prompt },
{ "channelId", context.Activity.ChannelId }
});
context.Call(locationDialog, this.AfterAddressPrompt);
}
private async Task AfterAddressPrompt(IDialogContext context, IAwaitable<Place> result)
{
var place = await result;
this.currentAddress = place.GetPostalAddress().FormattedAddress;
PromptDialog.Choice(context, this.AfterSelectToSaveAddress, this.saveOptionNames.Concat(new[] { Resources.SavedAddressDialog_NotThisTime }), this.saveAddressPrompt);
}
private async Task AfterSelectToSaveAddress(IDialogContext context, IAwaitable<string> result)
{
var saveOptionName = await result;
saveOptionName = saveOptionName == Resources.SavedAddressDialog_NotThisTime ? null : saveOptionName;
context.Done(new SavedAddressResult { Value = this.currentAddress, SaveOptionName = saveOptionName });
}
private async Task AfterSelectSavedAddress(IDialogContext context, IAwaitable<string> result)
{
this.currentAddress = await result;
if (this.currentAddress == Resources.SavedAddressDialog_AddNewAddress)
{
this.AddressPrompt(context);
}
else
{
context.Done(new SavedAddressResult { Value = this.currentAddress });
}
}
public class SavedAddressResult
{
public string Value { get; set; }
public string SaveOptionName { get; set; }
}
}
}