gpt4 book ai didi

使用 dict 在 Pushover 中进行 python 确认

转载 作者:太空宇宙 更新时间:2023-11-03 16:56:21 26 4
gpt4 key购买 nike

我在尝试使用 python 从 Pushover 获得确认时也遇到了麻烦。

在我的脚本中,我使用字典向 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 语句,但仍然只确认第二个字典条目。

查看代码我相信

v = requests.get("https://api.pushover.net/1/receipts/"+ d + ".json?token=" + app)

仅检查第二个字典条目,而不是两者。

最佳答案

在你的第一个测试循环中

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

要收集每个人的确认,您需要在字典中为每个用户添加一个附加条目来保存该用户是否确认,或者使用另一个数据结构来跟踪所有确认(对于任意多个用户)。

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 again

关于使用 dict 在 Pushover 中进行 python 确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35389106/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com