处理松弛的传出webhook

flask_slackbot的Python项目详细描述


Build StatusCoverage StatusPyPI VersionDownloads

烧瓶松弛器

flask slackbot是一个flask扩展,它可以帮助您处理slack传出的webhook。

安装

$ pip install flask-slackbot

使用量

# coding=utf-8fromflaskimportFlaskfromflask_slackbotimportSlackBotapp=Flask(__name__)app.config['SLACK_TOKEN']='Your token here'# if you need to use slacker you should give a slack chat tokenapp.config['SLACK_CHAT_TOKEN']='Your slack chat token'app.config['SLACK_CALLBACK']='/slack_callback'app.debug=Trueslackbot=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
}'''deffn1(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.deffn4(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.deffn2(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')returnNonedeffn3(text):'''
    This function is a filter, which makes our bot ignore the text sent from itself.'''returntext.startswith('!')slackbot.set_handler(fn1)slackbot.filter_outgoing(fn3)if__name__=="__main__":app.run()

陷阱

如果你还没有设置一个触发器字,并且你的回调服务器返回一些文本到slack,那将是一个回调地狱。你知道就像乒乓球,然后变成一个无限循环。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何在Spring工具套件中找到Spring版本?   是否有API将vSphere JSON结果映射到适当的Java对象?   java在spring WebSocket中有请求作用域吗?(websocket scop==会话范围)   java我想从list1中删除list2元素并返回list1   java使用JPA/Hibernate为单个集合使用多个@Where   JSF2.0中混合Ajax和完整请求的java   java变量miles可能尚未初始化   java使用文件路径StringArray构建父子数组   java数据源在struts中不起作用   java从另一个类访问txtField值   具有相同主键和外键双向关系的java Hibernate实体OneToOne   java Android广播接收器:上下文问题   java我可以将实体指定为数据存储属性以实现类似joinlike的功能吗?   java如何排除hadoop核心依赖项   java是否可以确定用户是否实时单击了任何通知(不仅仅是来自我的应用程序)?   java在单独的类中访问私有变量?   java如何使用Spring@Value注入映射