-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooker.py
64 lines (59 loc) · 1.84 KB
/
hooker.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import json
import requests
import botconfig
logging.basicConfig()
LOG = logging.getLogger(__name__)
LOG.setLevel(botconfig.DEFAULT_LOGGING)
# return send_request('UNCAUGHT Exception', tb, field_arr)
class Hooker(object):
def __init__(
self,
channel,
):
self.field_arr = []
if not channel:
raise ValueError("channel argument cannot be empty")
self.channel = channel
def add_field(self, title, body, short=False):
self.field_arr.append({
'title': title,
'value': body,
'short': short,
})
def send(self, title, body):
if len(body) > botconfig.HOOK_MAX_BODY:
arg = {
'title': "Message Body Truncated",
'body': "shaving off {} bytes, sorry".format(
len(body) - botconfig.HOOK_MAX_BODY
),
'short': False,
}
self.add_field(**arg)
body = body[:botconfig.HOOK_MAX_BODY]
send_json = {
"attachments": [
{
"title": title,
"text": body,
"fields": self.field_arr,
}
],
"channel": self.channel,
"username": botconfig.HOOK_USER,
"icon_emoji": botconfig.HOOK_ICON
}
response = requests.post(
botconfig.HOOK_URL,
data=json.dumps(send_json),
headers={'Content-Type': 'application/json'}
)
if response.status_code != 200:
LOG.error('Request to slack returned an error {}, the response is:\n{}'.format(
response.status_code,
response.text,
))
return True # Prevent invocation retry