]> git.cworth.org Git - turbot/commitdiff
Share the rot() implementation between both Lambda and Flask implementations
authorCarl Worth <cworth@cworth.org>
Sun, 11 Oct 2020 21:12:42 +0000 (14:12 -0700)
committerCarl Worth <cworth@cworth.org>
Sun, 11 Oct 2020 21:29:30 +0000 (14:29 -0700)
This provides a model of how to do code sharing between the two
implementations we have, (with the expectation that the code in the
"turbot" package will be generic and that code in turbot_slack and
turbot_lambda will use that common code while doing whatever
additional marshalling is necessary for their particular application
environments).

Makefile
turbot/rot.py
turbot_flask/turbot.py
turbot_lambda/lambda_function.py

index 8aaad75091914c2f7dbf074a6a1076d7e8307991..640c8f0ca4316c74b771d3a6148f7054e6908e75 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -76,8 +76,8 @@ deploy-flask:
 deploy-lambda:
        rm -rf .deploy-lambda-source
        git clone . .deploy-lambda-source
-       rm -rf .deploy-lambda-source/.git
        (cd .deploy-lambda-source/turbot_lambda; zip ../turbot.zip lambda_function.py)
+       (cd .deploy-lambda-source; zip turbot.zip $$(git ls-files -- turbot))
        (cd .deploy-lambda-source; \
                aws lambda update-function-code \
                --profile halibut \
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
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):
index fe545b8eed418176f9f877877991144eeb8de825..53251d29b71baa5779a1be054bc7fa1797661daf 100644 (file)
@@ -1,60 +1,4 @@
-import re
-
-def rot_string(str, n=13):
-    """Return a rotated version of a string
-
-    Specifically, this functions returns a version of the input string
-    where each uppercase letter has been advanced 'n' positions in the
-    alphabet (wrapping around). Lowercase letters and any non-alphabetic
-    characters will be unchanged."""
-
-    result = ''
-    for letter in str:
-        if letter.isupper():
-            result += chr(ord("A") + (ord(letter) - ord("A") + n) % 26)
-        else:
-            result += letter
-    return result
-
-def rot(args):
-    """Implements logic for /rot slash command in Slack, returning a string
-
-    This implements the /rot command of our Slack bot. 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 returned as a string (with Slack
-    formatting)."""
-
-    match = re.match(r'^([0-9]+|\*) (.*)$', args)
-    if (match):
-        try:
-            count = int(match.group(1))
-        except ValueError:
-            count = None
-        text = match.group(2)
-    else:
-        count = None
-        text = args
-
-    text = text.upper()
-
-    reply = "```/rot {} {}\n".format(count if count else '*', text)
-
-    if count:
-        reply += rot_string(text, count)
-    else:
-        reply += "\n".join(["{:02d} ".format(count) + rot_string(text, count)
-                            for count in range(1, 26)])
-
-    reply += "```"
-
-    return reply
+from turbot.rot import rot
 
 def lambda_handler(event, context):
     """Top-level entry point for our lambda function.