-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.py
44 lines (36 loc) · 1.14 KB
/
slack.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
"""Slack
This module contains code for sending messages to Slack via webhooks.
"""
import json
from typing import Dict
import requests
def slack_message(message: str, webhook: str) -> None:
"""Send plain message to Slack
Parameters
----------
message : str
Single string message to be sent to Slack
webhook : str
WebHook for sending the message to
"""
message_json = json.dumps({"text": message})
_post_slack(data=message_json, webhook=webhook)
def slack_block_message(message: Dict[str, str], webhook: str) -> None:
"""Send formatted block message to Slack
Parameters
----------
message : Dict[str, str]
Dictionary containing the formatted block message to be sent to Slack
webhook : str
WebHook for sending the message to
"""
message_json = json.dumps(message)
_post_slack(data=message_json, webhook=webhook)
def _post_slack(data: str, webhook: str) -> None:
"""Send POST request to Slack Webhook for message"""
headers = {"Content-type": "application/json"}
resp = requests.post(
webhook,
headers=headers,
data=data
)