【问题标题】:python acknowlage in pushover using dictpython 在 pushover 中使用 dict 确认
【发布时间】:2016-05-25 04:00:48
【问题描述】:

我也无法使用 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 的样本是什么样的吗?

标签: python python-2.7 dictionary pushover


【解决方案1】:

在您的第一个测试循环中

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 again

【讨论】:

  • 伟大的一半。是的,我没有停下来想。请阅读更新
  • 关于我们如何实现这一目标的任何想法?
  • 上面的答案应该有效。请注意,即使是第二个循环也已更新。
  • 那么第二个循环放在while循环之后?
  • 得到它的工作看到 sugested 编辑您的帖子。稍后将使用多个 uid 进行测试。
猜你喜欢
  • 2016-01-16
  • 2019-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-01
  • 2015-04-01
  • 1970-01-01
  • 2019-06-12
相关资源
最近更新 更多