我有几个Python函数。如果发生任何错误,我想将错误消息发送到松弛状态。为此,我在代码的除了代码段之外的行下面添加了
except Exception as ex:
msg = 'There is a problem with csv generation due to: {}'.format(ex)
logger.info(msg)
send_message("web_hook_url",msg)
我的send_message()如下所示-
def send_message(webhook_url, message):
response = requests.post(
webhook_url, data=json.dumps(message),
headers={'Content-Type': 'application/json'}
)
if response.status_code != 200:
raise ValueError(
'Request to slack returned an error %s, the response is:\n%s'
% (response.status_code, response.text)
)
我看到了以下链接slack webhook post,但是我对如何在我的除外代码块中使用send_message()感到困惑?
有人可以帮我实现这一目标吗?
答案 0 :(得分:2)
请求中的数据必须采用正确的格式,所以。
logger.info(msg)
slackmsg = {"text": msg}
send_message(self, slackmsg)
答案 1 :(得分:1)
您的有效负载中必须包含一个名为“ text”的键。
y
对于您如何使用它的问题,您可以像这样设置消息格式:
def send_message(self, message):
payload = {"text": message}
try:
return requests.post(self.url, data=json.dumps(payload), headers={'Content-Type': 'application/json'})
except requests.exceptions.RequestException as e:
print e.message
return False
也请看一下该工具Sentry