-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
53 lines (44 loc) · 2.16 KB
/
utils.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
import logging
from slack_sdk.errors import SlackApiError
def send_message_to_channel(client, channel_name, text):
"""
Send a message to slack channel of your choice.
Args:
client (slack WebClient): This client will be used to send the message. Should be authenticated with the token.
channel_name (string): Name of the channel to send the message to without the #. Channel should already exists.
text (string): Text for the message to send to the channel.
"""
logging.info(f"Trying to send message to {channel_name} channel...")
# This is a message without buttons and stuff. We are assuming #alert-bot-test already exists and the bot is added to it
try:
response = client.chat_postMessage(
channel=f"#{channel_name}",
text= channel_name,
blocks =[ {
"type": "section",
"text": {
"type": "mrkdwn",
"text": text
}
}
]
)
logging.info("Done")
except SlackApiError as e:
logging.warning("Could post message. Error: ", e.response["error"])
def create_new_channel(client, channel_name):
"""
Create a new channel on your slack workspace
Args:
client (slack WebClient): This client will be used to create the channel. Should be authenticated with the token.
channel_name (string): Name of the channel that needs to be created without the #.
"""
logging.info(f"Trying to create a new channel: {channel_name}...")
try:
response = client.conversations_create(name=channel_name)
logging.info("Done")
except SlackApiError as e:
if e.response["error"] == "name_taken":
logging.info("Done")
else:
logging.warning("Could not create new channel. Error: ", e.response["error"])