2016-02-14 15 views
1

我在嘗試時遇到了問題,也得到了使用python pushover的確認。使用字典的pushover中的python確認

在我的腳本中,我使用字典將相同的消息發送給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) 

UPDATE

使用低於此提供的代碼現在重新檢查while語句,但仍然只承認第二字典條目。

回顧過去的代碼,我相信

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

只檢查第二字典項不能同時使用。

+1

我們可以看看樣本「dict」的樣子嗎? –

回答

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 

後,收集大家的承認你將需要在您的詞典中爲每個持有該用戶確認的用戶或其他數據結構跟蹤所有確認(針對任意多個用戶)的附加條目。

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 
+0

那裏很棒的一半。是的,我沒有停下來想到這一點。請閱讀更新 – shaggs

+0

關於我們如何實現這一目標的任何想法? – shaggs

+0

上面的答案應該可行。請注意,即使第二個循環已更新。 –