I'm having trouble trying too get an acknowledgement from pushover using python.
In my script I'm using a dict to send the same message to 2 people and record once the message has been acknowledged. The reason I'm doing it this way and not in a group is because if one person acknowledges then it cancels the call for rest so if one person sees it and acknowledges and another doesn't then the alert stops for the group.
So far my code will send the message for both uid but will not print once they have acknowledged
import time
import requests
import datetime
dict = {'u56pt7jQXxgmtGnX5MBgsnUz4kgqKS': 'User1', 'uoREW3cuvy3SbSnyc7Ra737nVbrBQh': 'user2'}
app = "app id"
for k in dict:
user = k
params = {
'token': app,
'user': user,
'title': 'lala',
'message': 'test',
'retry': 300,
'expire': 40,
'priority': 2 ,
'sound': 'siren',
}
msg = requests.post('https://api.pushover.net/1/messages.json', data=params)
print "POSTed message to " + k
json_data = msg.json()
print json_data['receipt']
time.sleep(5)
d = json_data['receipt']
v = requests.get("https://api.pushover.net/1/receipts/"+ d + ".json?token=" + app)
out = v.json()
while out['acknowledged'] is 0:
print "not yet" #placed for debugging
time.sleep(5)
v = requests.get("https://api.pushover.net/1/receipts/"+ d + ".json?token=" + app)
if out['acknowledged'] is 1:
ack = out['acknowledged_by']
for k in dict:
if ack in k:
acked = dict[k]
t = datetime.datetime.strftime(datetime.datetime.now(), '%H:%M')
print (acked + " acknowledged at " + t)
UPDATE
Using code provided below this now recheck the while statement but still only acknowledges the second dict entry.
Looking over the code I believe the
v = requests.get("https://api.pushover.net/1/receipts/"+ d + ".json?token=" + app)
is only checking the second dict entry not both.
In your first test loop
while out['acknowledged'] is 0:
print "not yet" #placed for debugging
time.sleep(5)
v = requests.get("https://api.pushover.net/1/receipts/"+ d + ".json?token=" + app)
out = v.json() #update the out, so you can check again
And in the second part you will need to convert it into a loop that terminates after everyone has made an acknowledgement
if out['acknowledged'] is 1:
while not all_acknowledged(dict): # custom function to check whether all users have made an acknowledgement
ack = out['acknowledged_by']
for k in dict:
if ack in k:
acked = dict[k]
dict[k]['ack'] = True #We must update this when we come across an acknowledged user
t = datetime.datetime.strftime(datetime.datetime.now(), '%H:%M')
print (acked + " acknowledged at " + t)
v = requests.get("https://api.pushover.net/1/receipts/"+ d + ".json?token=" + app)
out = v.json() #update the out, so you can check again
To collect everyones acknowledgement you will need either an additional entry in your dict for each user that holds whether that user acknowledges or another data structure to keep track of all acknowledgements (for arbitrary many users).
for k in dict:
user = k
params = {
'token': app,
'user': user,
'title': 'lala',
'message': 'test',
'retry': 300,
'expire': 40,
'priority': 2 ,
'sound': 'siren',
'ack': False
}
Once we add the ack
field we can update it in our second loop and create the function, so
def all_acknowledged(dict):
for k in dict:
if not dict[k]['ack']:
return False
return True
So, in the end we will have this:
import time
import requests
import datetime
dict = {'u56pt7jQXxgmtGnX5MBgsnUz4kgqKS': 'User1', 'uoREW3cuvy3SbSnyc7Ra737nVbrBQh': 'user2'}
app = "app id"
for k in dict:
user = k
params = {
'token': app,
'user': user,
'title': 'lala',
'message': 'test',
'retry': 300,
'expire': 40,
'priority': 2 ,
'sound': 'siren',
'ack': False
}
msg = requests.post('https://api.pushover.net/1/messages.json', data=params)
print "POSTed message to " + dict[k]
json_data = msg.json()
print json_data['receipt']
time.sleep(5)
d = json_data['receipt']
v = requests.get("https://api.pushover.net/1/receipts/"+ d + ".json?token=" + app)
out = v.json()
while out['acknowledged'] is 0:
print "not yet" #placed for debugging
time.sleep(5)
v = requests.get("https://api.pushover.net/1/receipts/"+ d + ".json?token=" + app)
out = v.json() #update the out, so you can check again
def all_acknowledged(dict):
for user in params:
if not params['ack']:
return False
return True
# Line below is commented out because if we got this far we have at least one acknowledgement
# if out['acknowledged'] is 1:
while not all_acknowledged(dict): # custom function to check whether all users have made an acknowledgement
ack = out['acknowledged_by']
for k in dict:
if ack in k:
acked = dict[k]
params['ack'] = True # We must update this when we come across an acknowledged user
t = datetime.datetime.strftime(datetime.datetime.now(), '%H:%M')
print (acked + " acknowledged at " + t)
v = requests.get("https://api.pushover.net/1/receipts/"+ d + ".json?token=" + app)
out = v.json() #update the out, so you can check again