-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_healthcheck.py
91 lines (77 loc) · 3.17 KB
/
run_healthcheck.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Use Web API to check health of 911bot
import datetime
import requests
import argparse
import json
import time
import os
import codecs
import sys
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
#logger.setLevel(logging.DEBUG)
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
sys.stderr = codecs.getwriter('utf8')(sys.stderr)
parser = argparse.ArgumentParser()
parser.add_argument("--target-bot",default="911bot",
help="The target bot")
parser.add_argument("--slack-token",
default=os.environ.get("HEALTHCHECK_SLACK_TOKEN",None),
help="Slack API token. Must be different " +\
"from the bot token. Generate here: https://api.slack.com/web")
args = parser.parse_args()
def slackCall(methodName,**params):
logger.debug(u"Calling method {}".format(methodName))
url = "https://slack.com/api/" + methodName
if params is None:
params = {}
logger.debug(u"Outputting params:")
for param in params:
logger.debug(u"{}={}".format(param,params[param]))
params['token'] = args.slack_token
result = requests.post(url,params=params).json()
if result['ok']: return result
else: raise RuntimeError,"Error calling %s: %s" % (methodName,result)
def sendMessage(channel, message,numMessages = 1, wait=1):
msg = slackCall("chat.postMessage",
channel=dm['channel']['id'],
text=message,
as_user=True
)
# Sleep so that the message has time to do the healthcheck -> slack ->
# 911bot -> filesystem -> slack round trip. I mean, it should really be
# more than 1 second to be safe. This is a hack.
time.sleep(wait)
messages = slackCall("im.history",channel=channel['channel']['id'],
count=numMessages)['messages']
# messages not always in time order, sort by timestamp
messages.sort(key = lambda item: item['ts'])
# for msg in messages:
# logger.info('msgline {}'.format(msg['text'].encode('utf8')))
if numMessages == 1:
return messages[0]['text']
else:
return messages[:numMessages]
auth = slackCall("auth.test")
users = slackCall('users.list',presence=1)['members']
bot = None
for user in users:
logger.debug(u"Examining user {}".format(user['name']))
if user['name'] == args.target_bot:
logger.debug(u"Found bot {}".format(args.target_bot))
bot = user
break
dm = slackCall('im.open',user=bot['id'])
logger.info(sendMessage(dm,"Hello, world"))
logger.info(sendMessage(dm," help "))
dt = str(datetime.datetime.now()) + u"\u0028\u256f\u00b0\u25a1\u00b0\uff09\u256f\ufe35 \u253b\u2501\u253b"
logger.info(sendMessage(dm," store-contact %s" % (dt)))
verify = sendMessage(dm," emergency {}".format(auth['user']))
if verify.find("verify") == -1:
raise RuntimeError,"Unexpected response to emergency: {}".format(verify)
# Bot sends 3 messages in response, we need all of them and want the first message
info = sendMessage(dm," YES ",numMessages=3, wait=5)[0]['text']
logger.info(info)
if info.find(dt) == -1:
raise RuntimeError,"Contact info not retrieved correctly: {}".format(info)