├── slackweb ├── __init__.py └── slackweb.py ├── MANIFEST.ini ├── setup.py ├── .gitignore └── README.md /slackweb/__init__.py: -------------------------------------------------------------------------------- 1 | from .slackweb import Slack 2 | -------------------------------------------------------------------------------- /MANIFEST.ini: -------------------------------------------------------------------------------- 1 | include info.py 2 | include version.py 3 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | 3 | try: 4 | import setuptools 5 | from setuptools import setup 6 | except ImportError: 7 | print("Please install setuptools.") 8 | 9 | setup_options = dict( 10 | name = "slackweb", 11 | description = "slack bot for incomming webhook", 12 | author = "satoshi03", 13 | author_email = "innamisatoshi@gmail.com", 14 | license = "MIT License", 15 | url = "https://github.com/satoshi03/slack-python-webhook", 16 | classifiers = [ 17 | "Programming Language :: Python :: 2.7", 18 | "Programming Language :: Python :: 3", 19 | "Programming Language :: Python :: 3.3", 20 | "License :: OSI Approved :: MIT License" 21 | ] 22 | ) 23 | setup_options["version"] = "1.0.5" 24 | setup_options.update(dict( 25 | packages = ['slackweb'], 26 | )) 27 | 28 | setup(**setup_options) 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | #### joe made this: https://goel.io/joe 2 | 3 | #####=== Python ===##### 4 | 5 | # Byte-compiled / optimized / DLL files 6 | __pycache__/ 7 | *.py[cod] 8 | 9 | # C extensions 10 | *.so 11 | 12 | # Distribution / packaging 13 | .Python 14 | env/ 15 | build/ 16 | develop-eggs/ 17 | dist/ 18 | downloads/ 19 | eggs/ 20 | lib/ 21 | lib64/ 22 | parts/ 23 | sdist/ 24 | var/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .coverage 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | 47 | # Translations 48 | *.mo 49 | *.pot 50 | 51 | # Django stuff: 52 | *.log 53 | 54 | # Sphinx documentation 55 | docs/_build/ 56 | 57 | # PyBuilder 58 | target/ 59 | 60 | # Redis 61 | # *.rdb 62 | -------------------------------------------------------------------------------- /slackweb/slackweb.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | 4 | # 3rd party library 5 | try: 6 | from urllib.parse import urljoin 7 | from urllib.parse import urlencode 8 | import urllib.request as urlrequest 9 | except ImportError: 10 | from urlparse import urljoin 11 | from urllib import urlencode 12 | import urllib2 as urlrequest 13 | import json 14 | 15 | 16 | class Slack(): 17 | 18 | def __init__(self, url=""): 19 | self.url = url 20 | self.opener = urlrequest.build_opener(urlrequest.HTTPHandler()) 21 | 22 | def notify(self, **kwargs): 23 | """ 24 | Send message to slack API 25 | """ 26 | return self.send(kwargs) 27 | 28 | def send(self, payload): 29 | """ 30 | Send payload to slack API 31 | """ 32 | payload_json = json.dumps(payload) 33 | data = urlencode({"payload": payload_json}) 34 | req = urlrequest.Request(self.url) 35 | response = self.opener.open(req, data.encode('utf-8')).read() 36 | return response.decode('utf-8') 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Slack Bot for WebHook 2 | 3 | Very simple slack client by using incoming-webhook. 4 | 5 | ## How to install 6 | 7 | To install slack-python-webhook, simply: 8 | 9 |
10 | $ sudo pip install slackweb 11 |12 | 13 | or from source: 14 | 15 |
16 | $ sudo python setup.py install 17 |18 | 19 | ## Getting started 20 | 21 | Get a token of slack webhook on [slack page](https://my.slack.com/services/new/incoming-webhook/). 22 | 23 | Instantiate: 24 |
25 | > import slackweb 26 | > slack = slackweb.Slack(url="https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX") 27 |28 | 29 | In case that you want to send a simple message: 30 | 31 |
32 | > slack.notify(text="Maguro is a sushi") 33 |34 | 35 | In case that you want to send a custom message: 36 | 37 |
38 | > slack.notify(text="Tako is a sushi", channel="#sushi", username="sushi-bot", icon_emoji=":sushi:") 39 |40 | 41 | If you want to use richly-formatted massages: 42 | 43 |
44 | > attachments = []
45 | > attachment = {"title": "Sushi", "pretext": "Sushi _includes_ gunkanmaki", "text": "Eating *right now!*", "mrkdwn_in": ["text", "pretext"]}
46 | > attachments.append(attachment)
47 | > slack.notify(attachments=attachments)
48 |
49 |
50 | More detail description of message formatting, refer according pages:
51 |
52 | - [Message Formatting](https://api.slack.com/docs/formatting)
53 | - [Attachments](https://api.slack.com/docs/attachments)
54 |
55 |
--------------------------------------------------------------------------------