def create_app(config=None): app = Flask(__name__) app.config.from_object(settings) if isinstance(config, dict): app.config.update(config) elif config: app.config.from_pyfile(os.path.realpath(config)) redis_store.init_app(app) cache.init_app(app) app.plugin_modules = plugin_modules slackbot = SlackBot(app) _callback = partial(callback, app=app) slackbot.set_handler(_callback) slackbot.filter_outgoing(_filter) return app
def create_app(config=None): app = Flask(__name__) app.config.from_object(settings) if isinstance(config, dict): app.config.update(config) elif config: app.config.from_pyfile(os.path.realpath(config)) redis_store.init_app(app) cache.init_app(app) app.cache = cache app.plugin_modules = plugin_modules slackbot = SlackBot(app) slackbot.set_handler(callback) slackbot.filter_outgoing(_filter) return app
from flask_slackbot import SlackBot app = Flask(__name__) app.config['SLACK_TOKEN'] = 'Your token here' app.config['SLACK_CALLBACK'] = '/slack_callback' app.debug = True slackbot = SlackBot(app) def fn1(kwargs): return {'text': '!' + kwargs['text']} def fn2(kwargs): slackbot.slack.chat.post_message('#general', 'hello from slacker handler') return None def fn3(text): return text.startswith('!') slackbot.set_handler(fn1) slackbot.filter_outgoing(fn3) if __name__ == "__main__": app.run()
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 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(host='0.0.0.0', port=80)
app = create_app() def callback(kwargs): s = kwargs['text'] if isinstance(s, unicode): s = s.encode('utf-8') private = True if 'private' in s or '私聊' in s else False data = {'message': s.replace('私聊', '', 1)} bot = None for plugin_module in plugin_modules: if plugin_module.test(data, bot): rv = plugin_module.handle(data, bot, kv=None, app=app) return {'text': '!' + rv, 'private': private} return {'text': '!呵呵'} def _filter(line): return line.startswith('!') slackbot = SlackBot(app) slackbot.set_handler(callback) slackbot.filter_outgoing(_filter) if __name__ == '__main__': app.run()
redis_store.init_app(app) return app app = create_app() def callback(kwargs): s = kwargs['text'] if isinstance(s, unicode): s = s.encode('utf-8') data = {'message': s} bot = None for plugin_module in plugin_modules: if plugin_module.test(data, bot): rv = plugin_module.handle(data, bot, kv=None, app=app) return {'text': '!' + rv} return {'text': '!呵呵'} def _filter(line): return line.startswith('!') slackbot = SlackBot(app) slackbot.set_handler(callback) slackbot.filter_outgoing(_filter) if __name__ == '__main__': app.run()