Flask-Slacker

Adds support to your Flask application using Slacker.


Keywords
flask, slack
License
MIT
Install
pip install Flask-Slacker==0.0.2

Documentation

Flask-Slacker

Test Status Test Coverage Status

A Flask extension for Slacker.

Installation

pip install Flask-Slacker

Getting started

To quickly start using Flask-Slacker, simply create a Slacker instance:

from flask import Flask
from flask_slacker import Slacker

app = Flask(__name__)
slacker = Slacker(app, token='my-token')

Alternatively, if you're using the application factory pattern:

from flask_slacker import Slacker
slacker = Slacker(token='my-token')

and then later call init_app where you create your application object:

from flask import Flask
from flask_slacker import Slacker

def create_app():
    app = Flask(__name__)

    slacker = Slacker(token='my-super-secret-token')
    slacker.init_app(app)

    # more here..
    return app

You can also set SLACKER_TOKEN as an environment variable so you don't have to pass the token argument to the Slacker class:

from flask import Flask
from flask_slacker import Slacker

def create_app():
    app = Flask(__name__)

    slacker = Slacker()
    slacker.init_app(app)

    return app

Examples

from http import HTTPStatus
from flask import Flask, request, jsonify
from flask_slacker import Slacker

app = Flask(__name__)
slacker = Slacker(app, token="my-super-secret-token")
# slacker = Slacker()  # reading from environment

@app.route("/send_notification", methods=["POST"])
def send_notification():
    data = request.get_json()
    channel = data.get("channel", "#random")
    username = data.get("username")
    message = data.get("message")

    slacker.chat.post_message(
        channel, message,
        username=username
    )
    response = {"message": f"Slack message sent to #{channel}!"}
    return jsonify(response), HTTPStatus.CREATED


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

And here is the response using httpie:

$ http POST http://localhost:5000/send_notification message=testing username=matheus channel=random
HTTP/1.0 201 CREATED
Content-Length: 28
Content-Type: application/json
Date: Mon, 29 Oct 2018 17:05:38 GMT
Server: Werkzeug/0.14.1 Python/3.7.0

{
    "message": "Slack message sent to #random"
}