Skip to content

Instantly share code, notes, and snippets.

@takelushi
Created December 15, 2020 23:23
Show Gist options
  • Select an option

  • Save takelushi/60a9e85116a0f9131d380b20e5e67528 to your computer and use it in GitHub Desktop.

Select an option

Save takelushi/60a9e85116a0f9131d380b20e5e67528 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import socket\n",
"\n",
"import IPython.core.magic\n",
"import requests\n",
"\n",
"# e.g. https://hooks.slack.com/services/XXXXXX/YYYYYY/abcdefg\n",
"WEBHOOKS_URL = '<INCOMING_WEB_HOOK_URL>'\n",
"MAX_LENGTH = 600\n",
"\n",
"\n",
"@IPython.core.magic.register_line_magic\n",
"def slack(message=None):\n",
" text = '[jupyter]'\n",
" if message:\n",
" text += ' ' + message\n",
" else:\n",
" text += ' A cell that was running on *{}* has finished.'.format(socket.gethostname())\n",
"\n",
" if len(text) > MAX_LENGTH:\n",
" text = text[:MAX_LENGTH] + '...'\n",
"\n",
" payload = {'text': text}\n",
" res = requests.post(WEBHOOKS_URL, data=json.dumps(payload))\n",
" try:\n",
" res.raise_for_status()\n",
" except requests.exceptions.HTTPError as err:\n",
" print(err.args[0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Notify default message.\n",
"%slack"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Notify value.\n",
"%slack {123}\n",
"\n",
"%slack Message"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment