added support for DAPNET

restructured configuration file
This commit is contained in:
Michael Clemens 2020-12-08 19:21:00 +01:00
parent 3bd119e899
commit 8802fd69b4
3 changed files with 70 additions and 38 deletions

View File

@ -1,6 +1,10 @@
# pyBMNotify # 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. 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: If you want to be notified via Telegram, the following libraries need to be installed:
* telebot (install with _sudo pip3 install telebot_) * telebot (install with _sudo pip3 install telebot_)
* telethon (install with _sudo pip3 install telethon_) * telethon (install with _sudo pip3 install telethon_)
## Configuration ## 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 ## Execution
_# python3 pyBMNotify.py_ ```
# python3 pyBMNotify.py
```

View File

@ -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_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 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) verbose = True # Enable extra messages (console only)
# Pushover configuration # Pushover configuration
pushover_token = "" # Your Pushover API token pushover = False # Enable or disable notifications via Pushover
pushover_user = "" # Your Pushover user key pushover_token = "1234567890" # Your Pushover API token
pushover_user = "abcdefghijklm" # Your Pushover user key
# Telegram configuration # Telegram configuration
telegram_api_id = "" # Your Telegram API ID telegram = False # Enable or disable notifications via Telegram
telegram_api_hash = "" # Your Telegram API Hash telegram_api_id = "1234567"
telegram_username = "" # The username you registered with @BotFather telegram_api_hash = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
phone = "" # Your phone number, e.g. +491234567890 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"

View File

@ -7,12 +7,17 @@ import config as cfg
import http.client, urllib import http.client, urllib
# libraries only needed if Telegram is configured in config.py # 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 import telebot
from telethon.sync import TelegramClient from telethon.sync import TelegramClient
from telethon.tl.types import InputPeerUser, InputPeerChannel from telethon.tl.types import InputPeerUser, InputPeerChannel
from telethon import TelegramClient, sync, events 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_TG_activity = {}
last_OM_activity = {} last_OM_activity = {}
@ -25,31 +30,35 @@ def on_disconnect():
def on_reconnect(): def on_reconnect():
print('Reconnecting') print('Reconnecting')
# Send push notification # Send push notification via Pushover. Disabled if not configured in config.py
def push_message(msg): def push_pushover(msg):
# Push notification via Pushover. Disabled if not configured in config.py conn = http.client.HTTPSConnection("api.pushover.net:443")
if cfg.pushover_token != "" and cfg.pushover_user != "": conn.request("POST", "/1/messages.json",
conn = http.client.HTTPSConnection("api.pushover.net:443") urllib.parse.urlencode({
conn.request("POST", "/1/messages.json", "token": cfg.pushover_token,
urllib.parse.urlencode({ "user": cfg.pushover_user,
"token": cfg.pushover_token, "message": msg,
"user": cfg.pushover_user, }), { "Content-type": "application/x-www-form-urlencoded" })
"message": msg, conn.getresponse()
}), { "Content-type": "application/x-www-form-urlencoded" })
conn.getresponse() # Send push notification via Telegram. Disabled if not configured in config.py
# Push notification via Telegram. Disabled if not configured in config.py def push_telegram(msg):
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 = TelegramClient('bm_bot', cfg.telegram_api_id, cfg.telegram_api_hash) client.connect()
client.connect() if not client.is_user_authorized():
if not client.is_user_authorized(): client.send_code_request(cfg.phone)
client.send_code_request(cfg.phone) client.sign_in(cfg.phone, input('Please enter the code which has been sent to your phone: '))
client.sign_in(cfg.phone, input('Please enter the code which has been sent to your phone: ')) try:
try: receiver = InputPeerUser('user_id', 'user_hash')
receiver = InputPeerUser('user_id', 'user_hash') client.send_message(cfg.telegram_username, msg)
client.send_message(cfg.telegram_username, msg) except Exception as e:
except Exception as e: print(e);
print(e); client.disconnect()
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 # assemble the text message
def construct_message(c): def construct_message(c):
@ -109,7 +118,12 @@ def on_mqtt(*args):
if notify: if notify:
msg = construct_message(call) msg = construct_message(call)
print(msg) 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 = SocketIO('https://api.brandmeister.network/lh')
socket.on('connect', on_connect) socket.on('connect', on_connect)