]> git.cworth.org Git - turbot/blobdiff - turbot_flask/turbot.py
Share the rot() implementation between both Lambda and Flask implementations
[turbot] / turbot_flask / turbot.py
index df7d51e0195cce4c3ed097816db5aac4864b5d08..bcbc91e899ef7f7adc0d8b215988647fd1d9c78c 100644 (file)
@@ -1,19 +1,49 @@
 #!/usr/bin/env python3
 
-from flask import Flask
+from flask import Flask, request
 from slackeventsapi import SlackEventAdapter
+from turbot.slack import slack_is_valid_request, slack_send_reply
 import os
 import threading
-from turbot.rot import rot_route
+from turbot.rot import rot
 from turbot.slack import slack_send_message
 from turbot.sheets import sheets_create
 
 app = Flask(__name__)
-app.register_blueprint(rot_route)
 
 slack_signing_secret = os.environ['SLACK_SIGNING_SECRET']
 slack_events = SlackEventAdapter(slack_signing_secret, "/slack/events", app)
 
+@app.route('/rot', methods = ['POST'])
+def rot_route():
+    """Implements the /rot slash command for Slack replying in Slack
+
+    The format of this command is as follows:
+
+        /rot [count|*] String to be rotated
+
+    The optional count indicates an amount to rotate each character in the
+    string. If the count is '*' or is not present, then the string will
+    be rotated through all possible 25 values.
+
+    The result of the rotation is provided as a message in Slack. If the
+    slash command was issued in a direct message, the response is made by
+    using the "response_url" from the request. This allows the bot to reply
+    in a direct message that it is not a member of. Otherwise, if the slash
+    command was issued in a channel, the bot will reply in that channel."""
+
+    if not slack_is_valid_request(request):
+        return make_response("invalid request", 403)
+
+    text = request.form.get('text')
+
+    reply = rot(text)
+
+    slack_send_reply(request, reply)
+
+    return ""
+
+
 @slack_events.on("channel_created")
 def handle_channel_created(event_data):
     def later(channel):