forked from n8acl/DAPNETNotifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdapnetnotifier.py
97 lines (76 loc) · 2.54 KB
/
dapnetnotifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#############################
##### Import Libraries
import config as cfg
import re
import requests
import time
import http.client, urllib
from time import sleep
# libary only needed if Discord is configured in config.py
if cfg.discord:
from discord_webhook import DiscordWebhook, DiscordEmbed
# libraries only needed if Telegram is configured in config.py
if cfg.telegram:
import telegram
#############################
##### Define Variables
value = ''
first_run = True
#############################
##### Define Functions
def checkMSG():
r = requests.get(cfg.mmdvm_ip)
strip1 = re.findall("!important;.>\w{4,9}:\s[\w\s]{2,69}", str(r.content))
try:
message_content = re.sub('!important;.>', '', str(strip1[0]))
return message_content
except:
return 'Error'
##### Define Service Functions
def send_discord(msg):
webhook = DiscordWebhook(url=cfg.discord_wh)
embed = DiscordEmbed(title="New DAPNET Message", description=msg)
webhook.add_embed(embed)
response = webhook.execute()
def send_pushover(msg):
connn = http.client.HTTPSConnection("api.pushover.net:443")
connn.request("POST", "/1/messages.json",
urllib.parse.urlencode({
"token": cfg.pushover_token,
"user": cfg.pushover_userkey,
"message": msg,
}), { "Content-type": "application/x-www-form-urlencoded" })
connn.getresponse()
def post_to_webhook(msg, wh_url):
# used for Slack and Mattermost
response = requests.post(
wh_url, data=json.dumps(msg),
headers={'Content-Type': 'application/json'}
)
def send_telegram(msg):
bot = telegram.Bot(token=cfg.telegram_bot_token)
bot.sendMessage(chat_id=cfg.telegram_chat_id, text=msg)
#############################
##### Main Program
try:
while True:
if first_run:
first_run = False
old_value = checkMSG()
else:
sleep(cfg.wait_time)
old_value, value = value, checkMSG()
if value != 'Error':
if value != old_value:
if cfg.discord:
send_discord(value)
if cfg.telegram:
send_telegram(value)
if cfg.mattermost:
post_to_webhook({'text': value}, cfg.mattermost_wh)
if cfg.slack:
post_to_webhook({'text': value}, cfg.slack_wh)
if cfg.pushover:
send_pushover(value)
except Exception as e:
print(str(e))