forked from mrchainman/Gotify-Nextcloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_msg.py
executable file
·50 lines (45 loc) · 1.61 KB
/
push_msg.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
#!/usr/bin/env python3
import requests
import json
import time
from settings import *
# set nextcloud header
headers = {'OCS-APIRequest': 'true',
'Content-Type': 'application/json',
'Accept': 'application/json'
}
# set gotify header
headerspush = {'X-Gotify-Key': token}
# create empty messages list
notifications = []
# start infinite loop for listening
if __name__ == "__main__":
while True:
# retrieve json data from the notifications endpoint
r = requests.get(url, headers=headers, auth=(user, pw))
# load the json data
m = (r.json())
# Iterate through the notifications
for i in range(len(m["ocs"]["data"])):
notification_id = m["ocs"]["data"][i]["notification_id"] # id
title = m["ocs"]["data"][i]["subject"]
date = m["ocs"]["data"][i]["datetime"]
msg = m["ocs"]["data"][i]["message"] or " "
# check if message was already pushed
if notification_id in notifications:
continue
# else send push notification
else:
requests.post(
urlpush,
headers=headerspush,
data={
'id': notification_id,
'date': date,
'title': title,
'message': msg,
'priority': notification_priority})
# Add the message to the list
notifications.append(notification_id)
# wait before checking again
time.sleep( delay )