-
Notifications
You must be signed in to change notification settings - Fork 0
/
Santa.cs
65 lines (48 loc) · 2.26 KB
/
Santa.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
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using SwedishPersonLookup;
namespace SMSServer
{
public static class Santa
{
private static readonly string[] Formats =
{
"Dear {0} of {2}:\nIf you've been good since you last sat on my knee, you will find a {1} under your tree.\nBest wishes,\nSanta",
"Dear {0}: If you've been good this year, Santa will deliver your {1} to {2} on Christmas Eve!",
"Dear {0}, Santa will deliver your {1} on Christmas Eve!",
"Dear {0}, Santa will deliver your {1} to {2}",
"{0}: Santa will deliver your {1}"
};
private static readonly Regex SwedishNumberRegex = new Regex(@"^\+46[\d\s]*$", RegexOptions.Compiled);
private static readonly Regex NounPrefixRegex = new Regex(@"^\s*(?:a|an)\s+", RegexOptions.IgnoreCase);
public static async Task<string> Reply(ShortMessage sms)
{
if (!SwedishNumberRegex.IsMatch(sms.From))
return "Sorry, Santa only works for Swedish numbers (+46)";
Person person = null;
try {
var lookup = new PersonLookupClient();
person = await lookup.ByPhoneNumber(sms.From);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return "Sorry, Santa is having some technical difficulties. The elves will check the error logs shortly!";
}
// person not known
if (person == null)
return "Sorry, you are not in Santa's book. Try from another number?";
// clean the input a little
var firstName = person.FullName.Split(' ')[0];
var where = string.IsNullOrWhiteSpace(person.City) ? "your town" : person.City;
var gift = NounPrefixRegex.Replace(sms.Message, "");
// find the first (largest) message that fits an arbitrary limit of a hundred chars.
var message =
Formats.Select(format => String.Format(format, firstName, gift, where))
.FirstOrDefault(generated => generated.Length < 160);
return message ?? "You ask for too much!";
}
}
}