Browse Source

Add support for deleting reminders

master
Starbeamrainbowlabs 4 years ago
parent
commit
6fafd3b480
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
  1. 9
      RhinoReminds/AIRecogniser.cs
  2. 28
      RhinoReminds/ClientListener.cs
  3. 12
      RhinoReminds/ReminderList.cs

9
RhinoReminds/AIRecogniser.cs

@ -4,6 +4,7 @@ using System.Linq; @@ -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 @@ -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);

28
RhinoReminds/ClientListener.cs

@ -110,6 +110,33 @@ namespace RhinoReminds @@ -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 @@ -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;

12
RhinoReminds/ReminderList.cs

@ -19,12 +19,14 @@ namespace RhinoReminds @@ -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 @@ -34,7 +36,6 @@ namespace RhinoReminds
return result;
}
public Reminder GetNextReminder()
{
if (Reminders.Count == 0)
@ -43,6 +44,11 @@ namespace RhinoReminds @@ -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);

Loading…
Cancel
Save