Skip to content

Instantly share code, notes, and snippets.

@Ken-Kuroki
Last active September 29, 2020 13:25
Show Gist options
  • Save Ken-Kuroki/66e8c041150b9a22618710b3f62bc0a9 to your computer and use it in GitHub Desktop.
Save Ken-Kuroki/66e8c041150b9a22618710b3f62bc0a9 to your computer and use it in GitHub Desktop.
Slack Notify
from datetime import datetime
import subprocess
import json
import requests
class SlackNotifier:
def __init__(self, slack_url, msg='Done!'):
self.start = datetime.now()
self.slack_url = slack_url
self.msg = msg
def __enter__(self):
self.start = datetime.now()
def __exit__(self, exc_type, exc_value, traceback):
end = datetime.now()
elapsed = end - self.start
text = '{}\nStart:\t{}\nEnd:\t{}\nElapsed:\t{}'.format(
self.msg, self.start, end, elapsed,
)
data = json.dumps({'text': text})
response = requests.post(self.slack_url, data=data)
response.raise_for_status()
def default_notifier(msg='Done!'):
proc = subprocess.run([
'gsutil', 'cate', 'gs://kenkuroki-secrets/slack',
], stdout=subprocess.PIPE)
slack_secret = proc.stdout.decode()
return SlackNotifier(slack_secret, msg)
if __name__ == '__main__':
with default_notifier('Hello world!'):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment