Code Monkey home page Code Monkey logo

flask-slackbot's Introduction

Build Status Coverage Status PyPI Version Downloads

flask_slackbot

flask_slackbot is a Flask extension which helps you deal with slack's outgoing webhook.

Installation

$ pip install flask_slackbot

Usage

# coding=utf-8
from flask import Flask

from flask_slackbot import SlackBot


app = Flask(__name__)
app.config['SLACK_TOKEN'] = 'Your token here'
# if you need to use slacker you should give a slack chat token
app.config['SLACK_CHAT_TOKEN'] = 'Your slack chat token'
app.config['SLACK_CALLBACK'] = '/slack_callback'
app.debug = True
slackbot = SlackBot(app)

'''
The parameter of the callback function is a dict returns from the slack's outgoing api.
Here is the detail:
kwargs
{
    'token': token,
    'team_id': team_id,
    'team_domain': team_domain,
    'channel_id': channel_id,
    'channel_name': channel_name,
    'timestamp': timestamp,
    'user_id': user_id,
    'user_name': user_name,
    'text': text,
    'trigger_word': trigger_word
}'''
def fn1(kwargs):
    '''
    This function shows response the slack post directly without an extra post.
    In this case, you need to return a dict.'''
    return {'text': '!' + kwargs['text']} # Note the '!' character here is an user defined flag to tell the slack, this message is sent from the bot.


def fn4(kwargs):
    '''
    This function looks like upper one. But a little different, this will only response to the sender.
    In this case, you need to return a dict with an extra key private setted as True.
    And if you need this function, you should given the slack chat token in config.'''
    return {'text': '!' + kwargs['text'], 'private': True} # Note the '!' character here is an user defined flag to tell the slack, this message is sent from the bot.


def fn2(kwargs):
    '''
    This function shows response the slack post indirectly with an extra post.
    In this case, you need to return None.
    Now the slack will ignore the response from this request, and if you need do some complex task you can use the built-in slacker.
    For more information, see https://github.com/os/slacker'''
    slackbot.slack.chat.post_message('#general', 'hello from slacker handler')
    return None


def fn3(text):
    '''
    This function is a filter, which makes our bot ignore the text sent from itself.'''
    return text.startswith('!')

slackbot.set_handler(fn1)
slackbot.filter_outgoing(fn3)


if __name__ == "__main__":
    app.run()

Trap

If you have not set a trigger word, and your callback server return some text to slack, there would be a callback hell. You know like ping pong, and then turn into an infinite loop.

flask-slackbot's People

Contributors

halfcrazy avatar dongweiming avatar mozillazg avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.