views.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:WineHelper 作者: scorelabio 项目源码 文件源码
def hears(request):
    """
    This route listens for incoming events from Slack and uses the event
    handler helper function to route events to our Bot.
    """

    #Wit makes our responses timeout, so we ignore Slack retries
    if "HTTP_X_SLACK_RETRY_NUM" in request.META:
        return HttpResponse("OK", 200)

    slack_event = json.loads(request.body)

    # ============= Slack URL Verification ============ #
    # In order to verify the url of our endpoint, Slack will send a challenge
    # token in a request and check for this token in the response our endpoint
    # sends back.
    #       For more info: https://api.slack.com/events/url_verification
    if "challenge" in slack_event:
        return HttpResponse(slack_event["challenge"], 200)
        #removed  {"content_type":"application/json"} from flask response

    # ============ Slack Token Verification =========== #
    # We can verify the request is coming from Slack by checking that the
    # verification token in the request matches our app's settings
    if pyBot.verification != slack_event.get("token"):
        print "Invalid Slack verification token: %s \npyBot has: \
                   %s\n\n" % (slack_event["token"], pyBot.verification)
        # By adding "X-Slack-No-Retry" : 1 to our response headers, we turn off
        # Slack's automatic retries during development.
        return HttpResponse(message, 403)

    # ====== Process Incoming Events from Slack ======= #
    # If the incoming request is an Event we've subcribed to
    if "event" in slack_event:
        event_type = slack_event["event"]["type"]
        # Then handle the event by event_type and have your bot respond
        return _event_handler(event_type, slack_event)

    # If our bot hears things that are not events we've subscribed to,
    # send a quirky but helpful error response
    return HttpResponse("[NO EVENT IN SLACK REQUEST] These are not the droids\
                         you're looking for.", 404)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号