-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslack.py
More file actions
129 lines (117 loc) · 5.48 KB
/
slack.py
File metadata and controls
129 lines (117 loc) · 5.48 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from slack import WebClient
from slack.errors import SlackApiError
import logging
"""
########################################################################################################################################################
# CONSTANT_SLACK_CHANNEL : The slack channel name where the updates would be sent
########################################################################################################################################################
"""
CONSTANT_LOGGING_SLACK_CHANNEL = "test-private"
"""
########################################################################################################################################################
# CONSTANT_SLACK_TOKEN : Slack token required to send msg to any given channel
# https://howchoo.com/python/python-send-slack-messages-slackclient
# https://api.slack.com/reference/surfaces/formatting
########################################################################################################################################################
"""
CONSTANT_SLACK_TOKEN = "iaminvalidtokenabcd-203458234085-09234852039845-alskdfk20934lksdjf09r"
class SlackClass(object):
"""
########################################################################################################################################################
# SLACK CLASS
# --> Contains methods to send slack messages to slack channel in different formats
########################################################################################################################################################
"""
slack_token = ""
channel_name = ""
client = None
def __init__(self, channel_name, slack_token):
logging.info("---------------- INFO ---------------------------------------------SLACKCLASS constuctor ------------------------------------------------ ")
self.channel_name = channel_name
self.slack_token = slack_token
self.client = WebClient(token=self.slack_token)
def update_on_slack_channel_neutral(self, message):
"""
update_on_slack_channel_neutral : Will be used to give neutral message on slack channel
:param message: The message to be conveyed
:return: None
"""
logging.info("---------------- INFO ---------------------------------------------SLACKCLASS update_on_slack_channel_neutral ------------------------------------------------ ")
try:
self.client.chat_postMessage(
channel=self.channel_name,
attachments=[
{
"color": "#FF8C00",
"text": message,
}
]
)
except SlackApiError as e:
print(e.response["error"])
assert e.response["error"] # str
# https://api.slack.com/reference/messaging/attachments
def update_on_slack_channel_success(self, message):
"""
update_on_slack_channel_success : To give success message on the slack channel
:param message: The message to be conveyed
:return:None
"""
logging.info("---------------- INFO ---------------------------------------------SLACKCLASS update_on_slack_channel_neutral ------------------------------------------------ ")
try:
self.client.chat_postMessage(
channel=self.channel_name,
attachments=[
{
"color": "#36a64f",
"text": message,
}
]
)
except SlackApiError as e:
print(e.response["error"])
assert e.response["error"] # str
def update_on_slack_channel_failure(self, message):
"""
update_on_slack_channel_success : To give success message on the slack channel
:param message: The message to be conveyed
:return:None
"""
logging.info("---------------- INFO ---------------------------------------------SLACKCLASS update_on_slack_channel_neutral ------------------------------------------------ ")
try:
self.client.chat_postMessage(
channel=self.channel_name,
attachments=[
{
"color": "#FF0000",
"text": message,
}
]
)
except SlackApiError as e:
print(e.response["error"])
assert e.response["error"] # str
def update_on_slack_channel(self, message):
"""
update_on_slack_channel : Used to update on the slack channel
:param message: The message to be converyed to the slack channel
:return: None
"""
logging.info("---------------- INFO ---------------------------------------------SLACKCLASS update_on_slack_channel_neutral ------------------------------------------------ ")
try:
self.client.chat_postMessage(
channel=self.channel_name,
blocks=[
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": message,
}
}
]
)
except SlackApiError as e:
print(e.response["error"])
assert e.response["error"] # str
slack_logging_class_obj = SlackClass(CONSTANT_LOGGING_SLACK_CHANNEL, CONSTANT_SLACK_TOKEN)