Created
December 6, 2019 07:53
-
-
Save abhishekkrthakur/be27574a12bf00a9ede304ed9f87b244 to your computer and use it in GitHub Desktop.
Slack notification from python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import requests | |
import json | |
SLACK_WEBHOOK= os.environ.get("SLACK_WEBHOOK") | |
def send_message(messages, channel="abhishek", username="beast"): | |
""" | |
:param messages: list of texts | |
:param channel: name of slack channel | |
:param username: username of the bot | |
""" | |
data = { | |
"username": username, | |
"channel": channel | |
} | |
data['text'] = '\n'.join(messages) | |
requests.post(SLACK_WEBHOOK, json.dumps(data)) | |
def send_dict(dictionary, channel="abhishek", username="beast"): | |
""" | |
:param dictionary: a dictionary (key, value) | |
:param channel: name of slack channel | |
:param username: username of the bot | |
""" | |
data = { | |
"username": username, | |
"channel": channel | |
} | |
values = [] | |
for k, v in dictionary.items(): | |
values.append(str(k) + ": " + str(v)) | |
data["text"] = "\n".join(values) | |
requests.post(SLACK_WEBHOOK, json.dumps(data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment