BosServer/BosServerTelegram/Program.cs
2026-08-02 23:33:31 +02:00

127 lines
5.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
using BlubbFish.IoT.Pocsag.BosServerDatatypes.Mqtt;
using BlubbFish.Utils;
using BlubbFish.Utils.IoT.Bots;
using BlubbFish.Utils.IoT.Connector;
using BlubbFish.Utils.IoT.Events;
namespace BlubbFish.IoT.Pocsag.BosServerTelegram {
class Program : ABot {
private readonly ABackend bmqtt;
private readonly ABackend btelegram;
private readonly Dictionary<String, String> bosconf;
static void Main(String[] args) => new Program(args, false, "bstelegram");
public Program(String[] args, Boolean fileLogging, String configSearchPath) : base(args, fileLogging, configSearchPath) {
this.bmqtt = ABackend.GetInstance(InIReader.GetInstance("settings").GetSection("backend"));
this.bmqtt.MessageIncomming += this.Mqtt_MessageIncomming;
this.bmqtt.MessageSending += this.Mqtt_MessageSending;
this.btelegram = ABackend.GetInstance(InIReader.GetInstance("settings").GetSection("usernotification"));
this.btelegram.MessageIncomming += this.Telegram_MessageIncomming;
this.btelegram.MessageSending += this.Telegram_MessageSending;
this.bosconf = InIReader.GetInstance("settings").GetSection("bosservertelegram");
this.SendMqtt(new Message("Anwendung BosServerTelegram Version " + Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion + " gestartet."));
this.WaitForShutdown();
this.SendMqtt(new Message("Anwendung BosServerTelegram wurde beendet... Ob das so soll?"));
System.Threading.Thread.Sleep(5000);
this.Dispose();
}
private void Telegram_MessageSending(Object sender, BackendEvent telegram) {
try {
Console.WriteLine("=> Telegram [" + telegram.From.ToString() + "]: " + telegram.Message);
} catch(Exception e) {
Helper.WriteError("Backend_MessageSending(): " + e.Message + "\n\n" + e.StackTrace);
}
}
private void Mqtt_MessageSending(Object sender, BackendEvent mqtt) {
try {
Console.WriteLine("=> MQTT [" + mqtt.From.ToString() + "]: " + mqtt.Message);
} catch(Exception e) {
Helper.WriteError("Backend_MessageSending(): " + e.Message + "\n\n" + e.StackTrace);
}
}
private void Telegram_MessageIncomming(Object sender, BackendEvent telegram) {
try {
if(telegram.From is Tuple<Int64, String, Int64, String> t) {
Console.WriteLine("<= Telegram-API [" + t.Item1.ToString() + "]: " + telegram.Message);
String task = telegram.Message.ToLower();
if(task == "idea" || task == "leer") {
if(!(t.Item4 is null)) {
Match x = Regex.Match(t.Item4, @"^\[[A-Z]+\] ([0-9]+): .*?$");
if(x.Success) {
if(t.Item1 == Int64.Parse(this.bosconf["special"])) {
this.SendMqtt(new AddFilter(x.Groups[1].ToString(), task, t.Item2));
} else {
this.SendTelegram(new Message("Hallo " + t.Item2 + ", https://xkcd.com/838/"), t.Item3.ToString());
}
} else {
this.SendTelegram(new Message("Hallo " + t.Item2 + ", ich kann die RIC in der zitierten Nachricht nicht finden."), t.Item3.ToString());
}
} else {
this.SendTelegram(new Message("Hallo " + t.Item2 + ", du hast keine Nachricht zitiert."), t.Item3.ToString());
}
}
}
} catch(Exception e) {
Helper.WriteError("Backend_MessageIncomming(): " + e.Message + "\n\n" + e.StackTrace);
}
}
private void Mqtt_MessageIncomming(Object sender, BackendEvent mqtt) {
try {
Console.WriteLine("<= MQTT [" + mqtt.From.ToString() + "]: " + mqtt.Message);
if(AMqttMessage.CheckJson(mqtt.Message, typeof(PocsagParsed))) {
PocsagParsed p = PocsagParsed.ConvertJson(mqtt.Message);
if(p.Isprivate) {
this.SendTelegram(p, this.bosconf["special"]);
}
if(p.Sendonline) {
this.SendTelegram(p, this.bosconf["group"]);
}
} else if(AMqttMessage.CheckJson(mqtt.Message, typeof(Message))) {
this.SendTelegram(Message.ConvertJson(mqtt.Message), this.bosconf["group"]);
}
} catch(Exception e) {
Helper.WriteError("Backend_MessageIncomming(): " + e.Message + "\n\n" + e.StackTrace);
}
}
private void SendMqtt(AMqttMessage msg) {
try {
((Utils.IoT.Connector.Data.Mqtt)this.bmqtt).Send(this.bosconf["topic"] + "/" + msg.Datatype, msg.ToJson());
} catch(Exception e) {
Helper.WriteError("SendMqtt(): " + e.Message + "\n\n" + e.StackTrace);
}
}
private void SendTelegram(AMqttMessage msg, String target) {
((Utils.IoT.Connector.User.Telegram)this.btelegram).Send(msg.ToString(), target);
if(msg is PocsagParsed p && p.Geo is GeoData) {
((Utils.IoT.Connector.User.Telegram)this.btelegram).SendLocationWithText(p.ToTitleString(), p.ToTextString(), p.Geo.Latitude, p.Geo.Longitude, target);
}
}
public override void Dispose() {
if(this.bmqtt != null) {
this.bmqtt.Dispose();
}
if(this.btelegram != null) {
this.btelegram.Dispose();
}
base.Dispose();
}
}
}