]> git.cworth.org Git - turbot/blobdiff - turbot/rot.py
Share the rot() implementation between both Lambda and Flask implementations
[turbot] / turbot / rot.py
index e991941d313b9f66bd4e8b81825e0d315590fe9b..1040903f56bc696c50a636d9210d9b6ea2e3cd09 100644 (file)
@@ -1,9 +1,5 @@
-from flask import Blueprint, request, make_response
-from turbot.slack import slack_is_valid_request, slack_send_reply
 import re
 
-rot_route = Blueprint('rot_route', __name__)
-
 def rot_string(str, n=13):
     """Return a rotated version of a string
 
@@ -20,30 +16,23 @@ def rot_string(str, n=13):
             result += letter
     return result
 
-@rot_route.route('/rot', methods = ['POST'])
-def rot():
-    """Implements the /rot route for the /rot slash command in Slack
-
-    This implements the /rot command of our Slack bot. The format of this
-    command is as follows:
+def rot(args):
+    """Return one or more rotated versions of a sring
 
-        /rot [count|*] String to be rotated
+    The arg string should be as follows:
 
-    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.
+        [count|*] String to be rotated
 
-    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."""
+    That is, the first word of the string is an optional number (or
+    the character '*'). If this is a number it 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.
 
-    if not slack_is_valid_request(request):
-        return make_response("invalid request", 403)
+    The result of the rotation is returned as a string (with Slack
+    formatting)."""
 
-    query = request.form.get('text')
-    match = re.match(r'^([0-9]+|\*) (.*)$', query)
+    match = re.match(r'^([0-9]+|\*) (.*)$', args)
     if (match):
         try:
             count = int(match.group(1))
@@ -52,7 +41,7 @@ def rot():
         text = match.group(2)
     else:
         count = None
-        text = query
+        text = args
 
     text = text.upper()
 
@@ -66,6 +55,4 @@ def rot():
 
     reply += "```"
 
-    slack_send_reply(request, reply)
-
-    return ""
+    return reply