|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.Recognizers.Text;
|
|
|
|
|
using Microsoft.Recognizers.Text.DateTime;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using S22.Xmpp;
|
|
|
|
|
using S22.Xmpp.Client;
|
|
|
|
|
using S22.Xmpp.Im;
|
|
|
|
|
|
|
|
|
|
namespace RhinoReminds
|
|
|
|
|
{
|
|
|
|
|
public class ClientListener
|
|
|
|
|
{
|
|
|
|
|
public readonly string Jid;
|
|
|
|
|
public string Username => Jid.Split('@')[0];
|
|
|
|
|
public string Hostname => Jid.Split('@')[1];
|
|
|
|
|
private readonly string password;
|
|
|
|
|
|
|
|
|
|
private ReminderList reminders = new ReminderList();
|
|
|
|
|
|
|
|
|
|
private XmppClient client;
|
|
|
|
|
|
|
|
|
|
public ClientListener(string inJid, string inPassword) {
|
|
|
|
|
Jid = inJid;
|
|
|
|
|
password = inPassword;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Start()
|
|
|
|
|
{
|
|
|
|
|
client = new XmppClient(Hostname, Username, password);
|
|
|
|
|
client.Error += errorHandler;
|
|
|
|
|
client.Message += messageHandler;
|
|
|
|
|
client.SubscriptionRequest += subscriptionRequestHandler;
|
|
|
|
|
|
|
|
|
|
// Connect to the server. This starts it's own thread that doesn't block the program exiting, apparently
|
|
|
|
|
client.Connect();
|
|
|
|
|
|
|
|
|
|
while (!client.Connected)
|
|
|
|
|
await Task.Delay(100);
|
|
|
|
|
|
|
|
|
|
Console.WriteLine($"[Rhino/Setup] Connected as {Jid}");
|
|
|
|
|
|
|
|
|
|
//client.SetStatus(Availability.Online);
|
|
|
|
|
|
|
|
|
|
await Task.Delay(Timeout.Infinite);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool subscriptionRequestHandler(Jid from) {
|
|
|
|
|
Console.WriteLine($"[Rhino/SubscriptionRequest] Approving subscription from {from}");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void errorHandler(object sender, ErrorEventArgs e) {
|
|
|
|
|
Console.Error.WriteLine($"Error {e.Reason}: {e.Exception}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void messageHandler(object sender, MessageEventArgs eventArgs) {
|
|
|
|
|
Console.WriteLine($"[Rhino/Reciever] [Message] {eventArgs.Jid} | {eventArgs.Message.Body}");
|
|
|
|
|
|
|
|
|
|
string message = eventArgs.Message.Body;
|
|
|
|
|
string[] parts = Regex.Split(eventArgs.Message.Body.Trim(), @"\s+");
|
|
|
|
|
string instruction = parts[0].ToLower();
|
|
|
|
|
|
|
|
|
|
if (parts.Select((string nextPart) => nextPart.ToLower()).Contains("remind")) {
|
|
|
|
|
Console.WriteLine("[Rhino/Reciever] Identified remind request");
|
|
|
|
|
|
|
|
|
|
List<ModelResult> dateAiResult = DateTimeRecognizer.RecognizeDateTime(message, Culture.English);
|
|
|
|
|
if (dateAiResult.Count == 0) {
|
|
|
|
|
sendChatReply(eventArgs.Message, "Sorry, but I didn't recognise any date or time in that message.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//DateTime dateTime = dateAiResult[0].Resolution["values"]
|
|
|
|
|
|
|
|
|
|
sendChatReply(
|
|
|
|
|
eventArgs.Message,
|
|
|
|
|
"#1: " + (dateAiResult[0].Resolution["values"] as List<object>)[0].ToString() + "\n" +
|
|
|
|
|
"JSON: " + JsonConvert.SerializeObject(
|
|
|
|
|
dateAiResult[0].Resolution["values"]
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void sendChatReply(Message originalMessage, string reply)
|
|
|
|
|
{
|
|
|
|
|
client.SendMessage(
|
|
|
|
|
originalMessage.From, reply,
|
|
|
|
|
null, originalMessage.Thread, MessageType.Chat
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool isContact(Jid jid)
|
|
|
|
|
{
|
|
|
|
|
foreach (RosterItem nextContact in client.GetRoster()) {
|
|
|
|
|
if (nextContact.Jid == jid)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|