private void SendTelegramAlert(AlertEventArgs e) { TelegramBot bot = new TelegramBot(); StringBuilder Areas = new StringBuilder(); foreach (var alert in e.Alert.data) { Areas.Append(alert + "\n"); } bot.Message = String.Format("{0}\n{1}\n{2}", e.Alert.title, e.AlertDate.ToString("dd/MM/yyyy HH:mm"), Areas); bot.SendMessage(); try { bot.Dispose(); } catch { } }
//When there is alert, event is rising and you will catch it here void _listener_OnAlert(object sender, AlertEventArgs e) { if (IsMqttEnabled) { PublishMqttTopic(e); } if (IsTelegramEnabled) { SendTelegramAlert(e); } if (IsUdpEnabled) { SendUpdAlert(e); } }
void timer_Tick(object sender, EventArgs e) { using (var _webClient = new WebClient()) { try { //Setting The Http Headers needed to make successful request _webClient.Headers.Add("Referer", "https://www.oref.org.il/"); //Using User-Agent Header as Chrome Web Browser ontop of Windows 10 _webClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36"); _webClient.Headers.Add("X-Requested-With", "XMLHttpRequest"); _webClient.Encoding = Encoding.UTF8; var jsonResult = _webClient.DownloadString(this.OrefApiUrl); //If there is no alert, the json response is Empty if (!String.IsNullOrEmpty(jsonResult)) { //Deserializing Json Result into Alert Object Alert alert = JsonConvert.DeserializeObject <Alert>(jsonResult); bool AlertFired = alerts.Any(item => item.id == alert.id); //if someone registerd to OnAlert Event, rise it if (this.OnAlert != null && !AlertFired) { AlertEventArgs args = new AlertEventArgs(alert, DateTime.Now); this.OnAlert(this, args); alerts.Add(alert); } } } catch (Exception ex) { Logger(ex); //In case there is a response error or Exception sleep for 3 seconds and than retry. Thread.Sleep(3000); } } }
private void SendUpdAlert(AlertEventArgs e) { try { Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPAddress serverAddr = IPAddress.Parse(ConfigurationManager.AppSettings["UdpAddress"]); IPEndPoint endPoint = new IPEndPoint(serverAddr, int.Parse(ConfigurationManager.AppSettings["UdpPort"])); string City = ConfigurationManager.AppSettings["City"]; StringBuilder Areas = new StringBuilder(); foreach (var alert in e.Alert.data) { if (City == alert) { byte[] send_buffer = Encoding.ASCII.GetBytes("RedAlert\n"); sock.SendTo(send_buffer, endPoint); } } sock.Close(); } catch (Exception ex) { this.Logger(ex); } }
private void PublishMqttTopic(AlertEventArgs e) { _mqttPublisher.Publish(e.Alert); }