Conditions | 5 |
Total Lines | 30 |
Lines | 0 |
Ratio | 0 % |
1 | import json |
||
14 | def run(self, message, channel, user=None, whisper=False): |
||
15 | endpoint = self.config['endpoint'] |
||
16 | |||
17 | if not endpoint: |
||
18 | raise ValueError('Missing "endpoint" config option') |
||
19 | |||
20 | url = urljoin(endpoint, "/hubot/st2") |
||
21 | |||
22 | headers = {} |
||
23 | headers['Content-Type'] = 'application/json' |
||
24 | body = { |
||
25 | 'channel': channel, |
||
26 | 'message': message |
||
27 | } |
||
28 | |||
29 | if user: |
||
30 | body['user'] = user |
||
31 | |||
32 | if whisper: |
||
33 | body['whisper'] = whisper |
||
34 | |||
35 | data = json.dumps(body) |
||
36 | response = requests.post(url=url, headers=headers, data=data) |
||
37 | |||
38 | if response.status_code == httplib.OK: |
||
39 | self.logger.info('Message successfully posted') |
||
40 | else: |
||
41 | self.logger.exception('Failed to post message: %s' % (response.text)) |
||
42 | |||
43 | return True |
||
44 |