-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathFunction.cs
269 lines (248 loc) · 11.2 KB
/
Function.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
using Alexa.NET.Request;
using Alexa.NET.Request.Type;
using Alexa.NET.Response;
using Amazon.Lambda.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace MachOneSoftware.PizzaBuddy
{
public class Function
{
private const string LaunchMessage = "Welcome to pizza buddy. You can say, give me a pizza, or, give me a pizza with a number of toppings.";
private const string StopMessage = "bon appétit";
private const string HelpMessage = "Stuck on what to order? I can give you a random pizza. Just say, give me a pizza, or, give me a pizza with a number of toppings. You can ask for a pizza with up to 10 toppings.";
private const string HelpReprompt = "You can say, give me a pizza, or, give me a pizza with a number of toppings. For example, give me a pizza with 3 toppings.";
private const string Error_TooManyToppings = "Sorry, that's too many toppings. You can ask for a pizza with up to 10 toppings.";
private const string Error_NegativeToppings = "Sorry, I can't make a pizza with negative toppings. You can ask for a pizza with up to 10 toppings.";
private const string Error_BadSlot = "Sorry, I didn't understand that. Try using a number between 0 and 10 when asking for a pizza with toppings.";
private const string Error_Unknown = "Sorry, something went wrong. You can say, give me a pizza, or, give me a pizza with a number of toppings.";
private ILambdaLogger _log;
private string _requestId;
private void Log(string msg)
{
_log.LogLine(msg);
}
public SkillResponse FunctionHandler(SkillRequest input, ILambdaContext context)
{
_requestId = context.AwsRequestId;
_log = context.Logger;
var response = new SkillResponse()
{
Version = "1.0.0",
Response = new ResponseBody() { ShouldEndSession = true }
};
IOutputSpeech output = null;
try
{
var requestType = input.GetRequestType();
if (requestType == typeof(LaunchRequest))
{
response.Response.ShouldEndSession = false;
output = GetOutput(LaunchMessage);
}
else if (requestType == typeof(SessionEndedRequest))
output = GetOutput(StopMessage);
else if (requestType == typeof(IntentRequest))
{
var intentRequest = (IntentRequest)input.Request;
switch (intentRequest.Intent.Name)
{
case "AMAZON.CancelIntent":
output = GetOutput(StopMessage);
break;
case "AMAZON.StopIntent":
output = GetOutput(StopMessage);
break;
case "AMAZON.HelpIntent":
response.Response.ShouldEndSession = false;
output = GetOutput(HelpMessage);
break;
case "RandomPizzaIntent":
response.Response.ShouldEndSession = HandleRandomPizzaIntent(intentRequest, out output);
break;
default:
response.Response.ShouldEndSession = false;
output = GetOutput(HelpReprompt);
break;
}
}
}
catch (Exception ex)
{
Log($"....Error: FunctionHandler\r\nRequestId: {_requestId}\r\n{ex.Message}\r\nParamaters: input = {input}; context = {context}\r\n{ex}");
output = GetOutput(Error_Unknown);
response.Response.ShouldEndSession = false;
}
finally
{
response.Response.OutputSpeech = output;
}
return response;
}
/// <summary>
/// Handler for RandomPizzaIntent. Returns a value indicating whether to end the session. Returns the inner response as an out parameter.
/// </summary>
/// <param name="intentRequest">IntentRequest to handle.</param>
/// <param name="output">Response output speech.</param>
/// <returns></returns>
private bool HandleRandomPizzaIntent(IntentRequest intentRequest, out IOutputSpeech output)
{
try
{
if (intentRequest.Intent.Slots["toppingCount"].Value == null)
{
output = GetOutput(GetRandomPizza());
return true;
}
var valid = int.TryParse(intentRequest.Intent.Slots["toppingCount"].Resolution.Authorities[0].Values[0].Value.Id, out var count);
if (valid && count <= 10 && count >= 0)
{
output = GetOutput(GetRandomPizza(count));
return true;
}
else if (count > 10)
{
output = GetOutput(Error_TooManyToppings);
return false;
}
else if (count < 0)
{
output = GetOutput(Error_NegativeToppings);
return false;
}
else if (!valid && decimal.TryParse(intentRequest.Intent.Slots["toppingCount"].Value, out var countD))
{
output = GetOutput(GetRandomPizza((int)Math.Floor(countD)));
return true;
}
else
{
output = GetOutput(Error_BadSlot);
return false;
}
}
catch (Exception ex)
{
Log($"....Error: HandleRandomPizzaIntent\r\nRequestId: {_requestId}\r\n{ex.Message}\r\nParamaters: intentRequest = {intentRequest}\r\n{ex}");
output = GetOutput(Error_Unknown);
return false;
}
}
private PlainTextOutputSpeech GetOutput(string text)
{
return new PlainTextOutputSpeech() { Text = text };
}
private string GetRandomPizza(int toppingCount = -1, PizzaType pizzaType = PizzaType.All)
{
try
{
if (toppingCount == 0)
return "For no toppings, I recommend a cheese pizza.";
IEnumerable<KeyValuePair<string, string[]>> pizzas = null;
string[] toppings = null;
if (pizzaType == PizzaType.Meat)
{
pizzas = Repo.MeatPizzas;
toppings = Repo.Meats;
}
else if (pizzaType == PizzaType.Veggie)
{
pizzas = Repo.VeggiePizzas;
toppings = Repo.Veggies;
}
else if (pizzaType == PizzaType.All)
{
pizzas = Repo.AllPizzas;
toppings = Repo.AllToppings;
}
var r = new Random(DateTime.Now.Millisecond);
var opt1 = "";
if (toppingCount > 1)
{
try
{
var available = pizzas.Where(p => p.Value.Length == toppingCount);
var i = new Random(DateTime.Now.Millisecond).Next(available.Count() - 1);
opt1 = $"For {toppingCount} toppings, I recommend the {available.ElementAt(i).Key} pizza. Its toppings are {ConcatToppings(available.ElementAt(i).Value)}.";
}
catch { }
}
else if (toppingCount < 1)
{
var i = new Random(DateTime.Now.Millisecond).Next(pizzas.Count() - 1);
opt1 = $"I recommend the {pizzas.ElementAt(i).Key} pizza. Its toppings are {ConcatToppings(pizzas.ElementAt(i).Value)}.";
}
if (opt1 != "" && r.Next() % 2 == 0)
return opt1;
else
return GetRandomizedPizza(toppings, toppingCount);
}
catch (Exception ex)
{
Log($"....Error: GetRandomPizza\r\nRequestId: {_requestId}\r\n{ex.Message}\r\nParamaters: toppingCount = {toppingCount}; pizzaType = {pizzaType}\r\n{ex}");
throw;
}
}
private string GetRandomizedPizza(string[] toppings, int toppingCount)
{
try
{
var r = new Random(DateTime.Now.Millisecond);
var random = false;
// Up to 5 toppings if not specified
if (toppingCount < 1)
{
toppingCount = r.Next(6);
random = true;
}
var theToppings = toppings;
var myToppings = new List<string>();
for (var i = 0; i < toppingCount; i++)
{
while (myToppings.Count == i)
{
var topping = theToppings[r.Next(theToppings.Length)];
if (!myToppings.Contains(topping))
myToppings.Add(topping);
}
}
if (toppingCount == 1)
return $"I recommend a {myToppings[0]} pizza.";
else
{
if (!random)
return $"For {toppingCount} toppings, I recommend a pizza with {ConcatToppings(myToppings.ToArray())}";
else
return $"OK, here's a pizza with {toppingCount} toppings. It has {ConcatToppings(myToppings.ToArray())}";
}
}
catch (Exception ex)
{
Log($"....Error: GetRandomizedPizza\r\nRequestId: {_requestId}\r\n{ex.Message}\r\nParamaters: toppings = {toppings}; toppingCount = {toppingCount}\r\n{ex}");
throw;
}
}
private string ConcatToppings(string[] toppings)
{
try
{
var sb = new StringBuilder(toppings[0]);
for (var i = 1; i < toppings.Length - 1; i++)
{
sb.Append($", {toppings[i]}");
}
sb.Append($", and {toppings[toppings.Length - 1]}.");
return sb.ToString();
}
catch (Exception ex)
{
Log($"....Error: ConcatToppings\r\nRequestId: {_requestId}\r\n{ex.Message}\r\nParamaters: toppings = {toppings}\r\n{ex}");
throw;
}
}
}
}