Add support for deleting reminders

This commit is contained in:
Starbeamrainbowlabs 2018-11-17 17:58:25 +00:00
parent bcc6c82c59
commit 6fafd3b480
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
3 changed files with 46 additions and 3 deletions

View file

@ -4,6 +4,7 @@ using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.Recognizers.Text;
using Microsoft.Recognizers.Text.DateTime;
using Microsoft.Recognizers.Text.Number;
using SBRL.Geometry;
namespace RhinoReminds
@ -11,6 +12,14 @@ namespace RhinoReminds
public static class AIRecogniser
{
public static IEnumerable<int> RecogniseNumbers(string source)
{
List<ModelResult> aiResults = NumberRecognizer.RecognizeNumber(source, Culture.English);
foreach (ModelResult nextResult in aiResults) {
yield return int.Parse(nextResult.Text);
}
}
public static (DateTime, TimeSpan) RecogniseDateTimeRange(string source, out string rawString)
{
List<ModelResult> aiResults = DateTimeRecognizer.RecognizeDateTime(source, Culture.English);

View file

@ -110,6 +110,33 @@ namespace RhinoReminds
switch (parts[0].ToLower())
{
case "delete":
case "remove":
List<int> failed = new List<int>(),
succeeded = new List<int>();
foreach (int nextId in AIRecogniser.RecogniseNumbers(message.Body)) {
Reminder nextReminder = reminderList.GetById(nextId);
if (nextReminder.JidObj != message.From.GetBareJid()) {
failed.Add(nextId);
continue;
}
reminderList.DeleteReminder(nextReminder);
succeeded.Add(nextId);
}
if (failed.Count > 0) {
string response = string.Join(", ", failed.Select((int nextId) => $"#{nextId}"));
response = $"Sorry! I can't delete reminder{(failed.Count != 1 ? "s" : "")} {response}, as you didn't create {(failed.Count != 1 ? "them":"it")}.";
sendChatReply(message, response);
}
if (succeeded.Count > 0) {
string response = string.Join(", ", succeeded.Select((int nextId) => $"#{nextId}"));
response = $"Deleted reminder{(succeeded.Count != 1 ? "s" : "")} {response} successfully.";
sendChatReply(message, response);
}
break;
case "list":
case "show":
if (parts.Select((n) => n.ToLower()).Contains("all")) {
@ -165,6 +192,7 @@ namespace RhinoReminds
Reminder newReminder = reminderList.CreateReminder(message.From, dateTime, reminder);
reminderList.Save(ReminderFilePath);
break;
default:
sendChatReply(message, "I don't understand that. Try rephrasing it or asking for help.");
break;

View file

@ -19,12 +19,14 @@ namespace RhinoReminds
public event OnReminderListUpdateHandler OnReminderListUpdate;
public ReminderList() {
public ReminderList()
{
}
public Reminder CreateReminder(Jid inJid, DateTime time, string message) {
public Reminder CreateReminder(Jid inJid, DateTime time, string message)
{
Reminder result = new Reminder(nextId++, $"{inJid.Node}@{inJid.Domain}", time, message);
Console.WriteLine($"[Rhino/ReminderList] Created reminder {result}");
while (Reminders.ContainsKey(time))
@ -34,7 +36,6 @@ namespace RhinoReminds
return result;
}
public Reminder GetNextReminder()
{
if (Reminders.Count == 0)
@ -43,6 +44,11 @@ namespace RhinoReminds
return Reminders.Values[0];
}
public Reminder GetById(int targetId)
{
return Reminders.First((KeyValuePair<DateTime, Reminder> nextPair) => nextPair.Value.Id == targetId).Value;
}
public void DeleteReminder(Reminder nextReminder)
{
Reminders.Remove(nextReminder.Time);