diff --git a/README.md b/README.md index 942d752..a64083e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ # pyBMNotify -Monitors a defined set of Brandmeister talkgroups and callsigns for activity. It then sends push notifications via Pushover and/or Telegram for any transmission in / of the monitored talk groups / call signs. +Monitors a defined set of Brandmeister talkgroups and callsigns for activity. It then sends push notifications via the following services for any transmission in / of the monitored talk groups / call signs: + +* Pushover (https://pushover.net) +* Telegram (https://telegram.org) +* DAPNET (https://hampager.de) In order to prevent message flooding, the script only notifes you again after 300 (configurable) seconds of silence in a TG or from a monitored call sign. @@ -15,13 +19,15 @@ Inspired by https://github.com/klinquist/bmPushNotification If you want to be notified via Telegram, the following libraries need to be installed: - * telebot (install with _sudo pip3 install telebot_) - * telethon (install with _sudo pip3 install telethon_) +* telebot (install with _sudo pip3 install telebot_) +* telethon (install with _sudo pip3 install telethon_) ## Configuration -Configure _config.py_ to your needs. If you don't want push notifications, leave the corresponding variables empty. +Configure _config.py_ to your needs. If you don't want push notifications, set the corresponding variables to False. ## Execution -_# python3 pyBMNotify.py_ +``` +# python3 pyBMNotify.py +``` diff --git a/config.py b/config.py index 5837f56..62b30b1 100644 --- a/config.py +++ b/config.py @@ -7,11 +7,23 @@ noisy_calls = ["L1DHAM"] # Noisy calls signs that will be ignored min_duration = 2 # Min. duration of a QSO to qualify for a push notification min_silence = 300 # Min. time in seconds after the last QSO before a new push notification will be send verbose = True # Enable extra messages (console only) + # Pushover configuration -pushover_token = "" # Your Pushover API token -pushover_user = "" # Your Pushover user key +pushover = False # Enable or disable notifications via Pushover +pushover_token = "1234567890" # Your Pushover API token +pushover_user = "abcdefghijklm" # Your Pushover user key + # Telegram configuration -telegram_api_id = "" # Your Telegram API ID -telegram_api_hash = "" # Your Telegram API Hash -telegram_username = "" # The username you registered with @BotFather -phone = "" # Your phone number, e.g. +491234567890 +telegram = False # Enable or disable notifications via Telegram +telegram_api_id = "1234567" +telegram_api_hash = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +telegram_username = "foo_bot" +phone = "+491234567890" + +# DAPNet configuration +dapnet = False # Enable or disable notifications via dapnet +dapnet_user = "mycall" +dapnet_pass = "xxxxxxxxxxxxxxxxxxxx" +dapnet_url = 'http://www.hampager.de:8080/calls' +dapnet_callsigns = ["MYCALL"] +dapnet_txgroup = "dl-all" diff --git a/pyBMNotify.py b/pyBMNotify.py index 5d98aa1..4a00b75 100644 --- a/pyBMNotify.py +++ b/pyBMNotify.py @@ -7,12 +7,17 @@ import config as cfg import http.client, urllib # libraries only needed if Telegram is configured in config.py -if cfg.telegram_api_id != "" and cfg.telegram_api_hash != "" and cfg.telegram_username != "": +if cfg.telegram: import telebot from telethon.sync import TelegramClient from telethon.tl.types import InputPeerUser, InputPeerChannel from telethon import TelegramClient, sync, events +# libraries only needed if dapnet is configured in config.py +if cfg.dapnet: + import requests + from requests.auth import HTTPBasicAuth + last_TG_activity = {} last_OM_activity = {} @@ -25,31 +30,35 @@ def on_disconnect(): def on_reconnect(): print('Reconnecting') -# Send push notification -def push_message(msg): - # Push notification via Pushover. Disabled if not configured in config.py - if cfg.pushover_token != "" and cfg.pushover_user != "": - conn = http.client.HTTPSConnection("api.pushover.net:443") - conn.request("POST", "/1/messages.json", - urllib.parse.urlencode({ - "token": cfg.pushover_token, - "user": cfg.pushover_user, - "message": msg, - }), { "Content-type": "application/x-www-form-urlencoded" }) - conn.getresponse() - # Push notification via Telegram. Disabled if not configured in config.py - if cfg.telegram_api_id != "" and cfg.telegram_api_hash != "" and cfg.telegram_username != "" and cfg.phone != "": - client = TelegramClient('bm_bot', cfg.telegram_api_id, cfg.telegram_api_hash) - client.connect() - if not client.is_user_authorized(): - client.send_code_request(cfg.phone) - client.sign_in(cfg.phone, input('Please enter the code which has been sent to your phone: ')) - try: - receiver = InputPeerUser('user_id', 'user_hash') - client.send_message(cfg.telegram_username, msg) - except Exception as e: - print(e); - client.disconnect() +# Send push notification via Pushover. Disabled if not configured in config.py +def push_pushover(msg): + conn = http.client.HTTPSConnection("api.pushover.net:443") + conn.request("POST", "/1/messages.json", + urllib.parse.urlencode({ + "token": cfg.pushover_token, + "user": cfg.pushover_user, + "message": msg, + }), { "Content-type": "application/x-www-form-urlencoded" }) + conn.getresponse() + +# Send push notification via Telegram. Disabled if not configured in config.py +def push_telegram(msg): + client = TelegramClient('bm_bot', cfg.telegram_api_id, cfg.telegram_api_hash) + client.connect() + if not client.is_user_authorized(): + client.send_code_request(cfg.phone) + client.sign_in(cfg.phone, input('Please enter the code which has been sent to your phone: ')) + try: + receiver = InputPeerUser('user_id', 'user_hash') + client.send_message(cfg.telegram_username, msg) + except Exception as e: + print(e); + client.disconnect() + +# send pager notification via DAPNET. Disabled if not configured in config.py +def push_dapnet(msg): + dapnet_json = json.dumps({"text": msg, "callSignNames": cfg.dapnet_callsigns, "transmitterGroupNames": [cfg.dapnet_txgroup], "emergency": True}) + response = requests.post(cfg.dapnet_url, data=dapnet_json, auth=HTTPBasicAuth(cfg.dapnet_user,cfg.dapnet_pass)) # assemble the text message def construct_message(c): @@ -109,7 +118,12 @@ def on_mqtt(*args): if notify: msg = construct_message(call) print(msg) - push_message(msg) + if cfg.pushover: + push_pushover(msg) + if cfg.telegram: + push_telegram(msg) + if cfg.dapnet: + push_dapnet(msg) socket = SocketIO('https://api.brandmeister.network/lh') socket.on('connect', on_connect)