ReVancedBuilder/Notifications.py

87 lines
3.4 KiB
Python
Raw Normal View History

import json
import re
import requests as req
2023-08-10 13:07:00 -05:00
import subprocess
def send_notif(appstate, error=False):
2023-08-10 14:50:42 -05:00
print = appstate['logger'].info
timestamp = appstate['timestamp']
2023-08-10 14:50:42 -05:00
if error:
msg = f"There was an error during build! Please check the logs.\nTimestamp: {timestamp}"
else:
notification_config = appstate['notification_config']
build_config = appstate['build_config']
present_vers = appstate['present_vers']
flag = appstate['flag']
msg = json.dumps(present_vers, indent=0)
msg = re.sub('("|\{|\}|,)', '', msg).strip('\n')
for app in build_config:
if not build_config[app].getboolean('build'):
continue
msg = msg.replace(build_config[app]['apk'], build_config[app]['pretty_name'])
msg += '\nTimestamp: ' + timestamp
2023-08-10 13:07:00 -05:00
if appstate['microg_updated']:
msg += '\nVanced microG was updated.'
config = appstate['notification_config']
for entry in config:
if not config[entry].getboolean('enabled'):
continue
encoded_title = '⚙⚙⚙ ReVanced Build ⚙⚙⚙'.encode('utf-8')
match entry:
case 'ntfy':
2023-08-10 13:07:00 -05:00
print('Sending notification through ntfy.sh...')
try:
url = config[entry]['url']
topic = config[entry]['topic']
except:
print('URL or TOPIC not provided!')
continue
headers = {'Icon': 'https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Revanced-logo-round.svg/240px-Revanced-logo-round.svg.png',
'Title': encoded_title}
try:
req.post(f"{url}/{topic}", msg, headers=headers)
except Exception as e:
print('Failed!' + str(e))
case 'gotify':
2023-08-10 13:07:00 -05:00
print('Sending notification through Gotify...')
try:
url = config[entry]['url']
token = config[entry]['token']
except:
print('URL or TOKEN not provided!')
continue
data = {'Title': encoded_title, 'message': msg, 'priority': '5'}
try:
req.post(f"{url}/message?token={token}", data)
except Exception as e:
print('Failed!' + str(e))
case 'telegram':
# TODO: Finish this!
2023-08-10 13:07:00 -05:00
print('Sending notification through Telegram...')
try:
chat = config[entry]['chat']
token = config[entry]['token']
except:
print('CHAT or TOKEN not provided!')
continue
2023-08-10 13:07:00 -05:00
cmd = f"./telegram.sh -t {token} -c {chat} -T {encoded_title} -M \"{msg}\""
try:
2023-08-10 14:50:42 -05:00
with subprocess.Popen(cmd, shell=True, bufsize=0, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).stdout as output:
for line in output:
line_utf = line.decode('utf-8').strip('\n')
if line_utf:
print(line_utf)
except Exception as e:
2023-08-10 14:50:42 -05:00
clean_exit(f"Failed!\n{e}", appstate)
case _:
print('Don\'t know how to send notifications to ' + entry)