Skip to content

Instantly share code, notes, and snippets.

@abhishekkrthakur
Created December 6, 2019 07:53
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save abhishekkrthakur/be27574a12bf00a9ede304ed9f87b244 to your computer and use it in GitHub Desktop.
Save abhishekkrthakur/be27574a12bf00a9ede304ed9f87b244 to your computer and use it in GitHub Desktop.
Slack notification from python
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