Skip to content

Commit

Permalink
Merge pull request #1 from mdsrosa/mdsrosa/update-readme
Browse files Browse the repository at this point in the history
Add missing argument token and update README with examples
  • Loading branch information
mdsrosa committed Oct 7, 2018
2 parents ca36111 + 8866cc9 commit a97873b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
27 changes: 25 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ To quickly start using Flask-Slacker, simply create a ``Slacker`` instance:
from flask_slacker import Slacker
app = Flask(__name__)
slacker = Slacker(app)
slacker = Slacker(app, token='my-token')
Alternatively, if you're using the `application factory`_ pattern:

.. code-block:: python
from flask_slacker import Slacker
slacker = Slacker()
slacker = Slacker(token='my-token')
and then later call ``init_app`` where you create your application object:

Expand All @@ -57,3 +57,26 @@ and then later call ``init_app`` where you create your application object:
return app
.. _`application factory`: http://flask.pocoo.org/docs/0.10/patterns/appfactories/

Examples
--------

.. code-block:: python
from flask import Flask, request
from flask_slacker import Slacker
app = Flask(__name__)
slacker = Slacker(app, token='my-token')
@app.route("/send_notification", methods=["POST"])
def send_notification():
channel = request.data.get("channel", "#random")
username = request.data.get("username")
message = request.data.get("message")
slacker.chat.post_message(channel, message, username=username)
if __name__ == "__name__":
app.run(host="0.0.0.0", port=5000)
13 changes: 7 additions & 6 deletions flask_slacker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@


class Slacker(object):
def __init__(self, app=None):
def __init__(self, app=None, token=None):
"""Initialize the Slacker interface.
:param app: Flask application
"""
if app is not None:
self.init_app(app)
self.init_app(app, token=token)

def init_app(self, app, config=None, session=None):
def init_app(self, app, config=None, session=None, token=None):
"""
Initialize the app in Flask.
"""
Expand All @@ -33,10 +33,11 @@ def init_app(self, app, config=None, session=None):
if config is None:
config = app.config

if 'SLACKER_TOKEN' not in config:
raise Exception('Missing SLACKER_TOKEN in your config.')
if token is None and 'SLACKER_TOKEN' not in config:
raise Exception('SLACKER_TOKEN not found in your config '
'nor `token` was informed.')

token = config['SLACKER_TOKEN']
token = config['SLACKER_TOKEN'] or token
timeout = config['SLACKER_TIMEOUT']
http_proxy = config['SLACKER_HTTP_PROXY']
https_proxy = config['SLACKER_HTTPS_PROXY']
Expand Down

0 comments on commit a97873b

Please sign in to comment.