|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +A routing layer for the onboarding bot tutorial built using |
| 4 | +[Slack's Events API](https://api.slack.com/events-api) in Python |
| 5 | +""" |
| 6 | +import json |
| 7 | +import bot |
| 8 | +from flask import Flask, request, make_response, render_template |
| 9 | + |
| 10 | +pyBot = bot.Bot() |
| 11 | +slack = pyBot.client |
| 12 | + |
| 13 | +app = Flask(__name__) |
| 14 | + |
| 15 | + |
| 16 | +def _event_handler(event_type, slack_event): |
| 17 | + """ |
| 18 | + A helper function that routes events from Slack to our Bot |
| 19 | + by event type and subtype. |
| 20 | +
|
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + event_type : str |
| 24 | + type of event recieved from Slack |
| 25 | + slack_event : dict |
| 26 | + JSON response from a Slack reaction event |
| 27 | +
|
| 28 | + Returns |
| 29 | + ---------- |
| 30 | + obj |
| 31 | + Response object with 200 - ok or 500 - No Event Handler error |
| 32 | +
|
| 33 | + """ |
| 34 | + team_id = slack_event["team_id"] |
| 35 | + # ================ Team Join Events =============== # |
| 36 | + # When the user first joins a team, the type of event will be team_join |
| 37 | + if event_type == "team_join": |
| 38 | + user_id = slack_event["event"]["user"]["id"] |
| 39 | + # Send the onboarding message |
| 40 | + pyBot.onboarding_message(team_id, user_id) |
| 41 | + return make_response("Welcome Message Sent", 200,) |
| 42 | + |
| 43 | + # ============== Share Message Events ============= # |
| 44 | + # If the user has shared the onboarding message, the event type will be |
| 45 | + # message. We'll also need to check that this is a message that has been |
| 46 | + # shared by looking into the attachments for "is_shared". |
| 47 | + elif event_type == "message" and slack_event["event"].get("attachments"): |
| 48 | + user_id = slack_event["event"].get("user") |
| 49 | + if slack_event["event"]["attachments"][0].get("is_share"): |
| 50 | + # Update the onboarding message and check off "Share this Message" |
| 51 | + pyBot.update_share(team_id, user_id) |
| 52 | + return make_response("Welcome message updates with shared message", |
| 53 | + 200,) |
| 54 | + |
| 55 | + # ============= Reaction Added Events ============= # |
| 56 | + # If the user has added an emoji reaction to the onboarding message |
| 57 | + elif event_type == "reaction_added": |
| 58 | + user_id = slack_event["event"]["user"] |
| 59 | + # Update the onboarding message |
| 60 | + pyBot.update_emoji(team_id, user_id) |
| 61 | + return make_response("Welcome message updates with reactji", 200,) |
| 62 | + |
| 63 | + # =============== Pin Added Events ================ # |
| 64 | + # If the user has added an emoji reaction to the onboarding message |
| 65 | + elif event_type == "pin_added": |
| 66 | + user_id = slack_event["event"]["user"] |
| 67 | + # Update the onboarding message |
| 68 | + pyBot.update_pin(team_id, user_id) |
| 69 | + return make_response("Welcome message updates with pin", 200,) |
| 70 | + |
| 71 | + # ============= Event Type Not Found! ============= # |
| 72 | + # If the event_type does not have a handler |
| 73 | + message = "You have not added an event handler for the %s" % event_type |
| 74 | + # Return a helpful error message |
| 75 | + return make_response(message, 200, {"X-Slack-No-Retry": 1}) |
| 76 | + |
| 77 | + |
| 78 | +@app.route("/install", methods=["GET"]) |
| 79 | +def pre_install(): |
| 80 | + """This route renders the installation page with 'Add to Slack' button.""" |
| 81 | + # Since we've set the client ID and scope on our Bot object, we can change |
| 82 | + # them more easily while we're developing our app. |
| 83 | + client_id = pyBot.oauth["client_id"] |
| 84 | + scope = pyBot.oauth["scope"] |
| 85 | + # Our template is using the Jinja templating language to dynamically pass |
| 86 | + # our client id and scope |
| 87 | + return render_template("install.html", client_id=client_id, scope=scope) |
| 88 | + |
| 89 | + |
| 90 | +@app.route("/thanks", methods=["GET", "POST"]) |
| 91 | +def thanks(): |
| 92 | + """ |
| 93 | + This route is called by Slack after the user installs our app. It will |
| 94 | + exchange the temporary authorization code Slack sends for an OAuth token |
| 95 | + which we'll save on the bot object to use later. |
| 96 | + To let the user know what's happened it will also render a thank you page. |
| 97 | + """ |
| 98 | + # Let's grab that temporary authorization code Slack's sent us from |
| 99 | + # the request's parameters. |
| 100 | + code_arg = request.args.get('code') |
| 101 | + # The bot's auth method to handles exchanging the code for an OAuth token |
| 102 | + pyBot.auth(code_arg) |
| 103 | + return render_template("thanks.html") |
| 104 | + |
| 105 | + |
| 106 | +@app.route("/listening", methods=["GET", "POST"]) |
| 107 | +def hears(): |
| 108 | + """ |
| 109 | + This route listens for incoming events from Slack and uses the event |
| 110 | + handler helper function to route events to our Bot. |
| 111 | + """ |
| 112 | + slack_event = json.loads(request.data) |
| 113 | + |
| 114 | + # ============= Slack URL Verification ============ # |
| 115 | + # In order to verify the url of our endpoint, Slack will send a challenge |
| 116 | + # token in a request and check for this token in the response our endpoint |
| 117 | + # sends back. |
| 118 | + # For more info: https://api.slack.com/events/url_verification |
| 119 | + if "challenge" in slack_event: |
| 120 | + return make_response(slack_event["challenge"], 200, {"content_type": |
| 121 | + "application/json" |
| 122 | + }) |
| 123 | + |
| 124 | + # ============ Slack Token Verification =========== # |
| 125 | + # We can verify the request is coming from Slack by checking that the |
| 126 | + # verification token in the request matches our app's settings |
| 127 | + if pyBot.verification != slack_event.get("token"): |
| 128 | + message = "Invalid Slack verification token: %s \npyBot has: \ |
| 129 | + %s\n\n" % (slack_event["token"], pyBot.verification) |
| 130 | + # By adding "X-Slack-No-Retry" : 1 to our response headers, we turn off |
| 131 | + # Slack's automatic retries during development. |
| 132 | + make_response(message, 403, {"X-Slack-No-Retry": 1}) |
| 133 | + |
| 134 | + # ====== Process Incoming Events from Slack ======= # |
| 135 | + # If the incoming request is an Event we've subcribed to |
| 136 | + if "event" in slack_event: |
| 137 | + event_type = slack_event["event"]["type"] |
| 138 | + # Then handle the event by event_type and have your bot respond |
| 139 | + return _event_handler(event_type, slack_event) |
| 140 | + # If our bot hears things that are not events we've subscribed to, |
| 141 | + # send a quirky but helpful error response |
| 142 | + return make_response("[NO EVENT IN SLACK REQUEST] These are not the droids\ |
| 143 | + you're looking for.", 404, {"X-Slack-No-Retry": 1}) |
| 144 | + |
| 145 | + |
| 146 | +if __name__ == '__main__': |
| 147 | + app.run(debug=True) |
0 commit comments