我也很难用python从pushover那里得到确认。
在我的脚本中,我使用dict向2个人发送相同的消息,并在消息被确认后进行记录。我之所以这样做,而不是在一个群体中,是因为如果一个人承认了,那么它就取消了休息的召唤,所以如果一个人看到并确认了,而另一个人没有,那么这个组的警报就停止了。
到目前为止,我的代码将为这两个uid发送消息,但在它们确认后不会打印。
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)更新
使用下面提供的代码,现在重新检查while语句,但仍然只确认第二个dict条目。
查看代码,我相信
v = requests.get("https://api.pushover.net/1/receipts/"+ d + ".json?token=" + app)只是检查第二个dict条目,而不是两者都检查。
发布于 2016-02-14 06:41:12
在你的第一个测试循环中
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在第二部分中,您将需要将其转换为一个循环,在每个人都做出确认后终止该循环。
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要收集每个人的确认信息,您需要在dict中为每个用户提供一个额外的条目,不管该用户是否承认,也需要另一个数据结构来跟踪所有确认(对于任意多个用户)。
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
}一旦我们添加了ack字段,我们就可以在第二个循环中更新它并创建函数,因此
def all_acknowledged(dict):
for k in dict:
if not dict[k]['ack']:
return False
return True所以,我们最终会有这样的结果:
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 againhttps://stackoverflow.com/questions/35389106
复制相似问题