From: Carl Worth Date: Sat, 26 Sep 2020 05:22:32 +0000 (-0700) Subject: Add a route to implement a /rot slash command X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=11447eca6af60b8eea982293c0857674c84c1533;p=turbot Add a route to implement a /rot slash command This is to be used as follows: /rot [count] String to rotate The optional count indicates a number of positions to rotate every letter in the string. If it is not provided, all rotations will be printed. Note: The count can also be provided as an '*' character to explicitly request all possible rotations. --- diff --git a/requirements.in b/requirements.in index 1ec97a8..1969d31 100644 --- a/requirements.in +++ b/requirements.in @@ -3,3 +3,4 @@ flask_restful python-dotenv slackclient slackeventsapi +requests diff --git a/requirements.txt b/requirements.txt index 95004d3..62de8a6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -30,10 +30,14 @@ attrs==20.2.0 \ --hash=sha256:26b54ddbbb9ee1d34d5d3668dd37d6cf74990ab23c828c2888dccdceee395594 \ --hash=sha256:fce7fc47dfc976152e82d53ff92fa0407700c21acd20886a13777a0d20e655dc \ # via aiohttp +certifi==2020.6.20 \ + --hash=sha256:5930595817496dd21bb8dc35dad090f1c2cd0adfaf21204bf6732ca5d8ee34d3 \ + --hash=sha256:8fc0819f1f30ba15bdb34cceffb9ef04d99f420f68eb75d901e9560b8749fc41 \ + # via requests chardet==3.0.4 \ --hash=sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae \ --hash=sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691 \ - # via aiohttp + # via aiohttp, requests click==7.1.2 \ --hash=sha256:d2b5255c7c6349bc1bd1e59e08cd12acbbd63ce649f2588755783aa94dfb6b1a \ --hash=sha256:dacca89f4bfadd5de3d7489b7c8a566eee0d3676333fbb50030263894c38c0dc \ @@ -49,7 +53,7 @@ flask==1.1.2 \ idna==2.10 \ --hash=sha256:b307872f855b18632ce0c21c5e45be78c0ea7ae4c15c828c20788b26921eb3f6 \ --hash=sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0 \ - # via yarl + # via requests, yarl itsdangerous==1.1.0 \ --hash=sha256:321b033d07f2a4136d3ec762eac9f16a10ccd60f53c0c91af90217ace7ba1f19 \ --hash=sha256:b12271b2047cb23eeb98c8b5622e2e5c5e9abd9784a153e9d8ef9cb4dd09d749 \ @@ -124,6 +128,10 @@ pytz==2020.1 \ --hash=sha256:a494d53b6d39c3c6e44c3bec237336e14305e4f29bbf800b599253057fbb79ed \ --hash=sha256:c35965d010ce31b23eeb663ed3cc8c906275d6be1a34393a1d73a41febf4a048 \ # via flask-restful +requests==2.24.0 \ + --hash=sha256:b3559a131db72c33ee969480840fff4bb6dd111de7dd27c8ee1f820f4f00231b \ + --hash=sha256:fe75cc94a9443b9246fc7049224f75604b113c36acb93f87b80ed42c44cbb898 \ + # via -r requirements.in six==1.15.0 \ --hash=sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259 \ --hash=sha256:8b74bedcbbbaca38ff6d7491d76f2b06b3592611af620f8426e82dddb04a5ced \ @@ -136,6 +144,10 @@ slackeventsapi==2.2.1 \ --hash=sha256:1582964860b9e00b721ddfc7402defdd02bdf73f48d4cee7fb952a3b27db45fb \ --hash=sha256:6baa2bf12b5a9d312bf02836720f3ea3b730377449d67ea92ec63fc7c02c7c30 \ # via -r requirements.in +urllib3==1.25.10 \ + --hash=sha256:91056c15fa70756691db97756772bb1eb9678fa585d9184f24534b100dc60f4a \ + --hash=sha256:e7983572181f5e1522d9c98453462384ee92a0be7fac5f1413a1e35c56cc0461 \ + # via requests werkzeug==1.0.1 \ --hash=sha256:2de2a5db0baeae7b2d2664949077c2ac63fbd16d98da0ff71837f7d1dea3fd43 \ --hash=sha256:6c80b1e5ad3665290ea39320b91e1be1e0d5f60652b964a3070216de83d2e47c \ diff --git a/turbot.py b/turbot.py index 6c0d8b9..b8e3da2 100755 --- a/turbot.py +++ b/turbot.py @@ -1,10 +1,13 @@ #!/usr/bin/env python3 -from flask import Flask +from flask import Flask, request from slackeventsapi import SlackEventAdapter from slack import WebClient +from slack.errors import SlackApiError import os +import requests +import re app = Flask(__name__) @@ -14,6 +17,57 @@ slack_bot_token = os.environ['SLACK_BOT_TOKEN'] slack_events = SlackEventAdapter(slack_signing_secret, "/slack/events", app) slack_client = WebClient(slack_bot_token) +def rot_string(str, n=13): + result = '' + for letter in str: + if letter.isupper(): + result += chr(ord("A") + (ord(letter) - ord("A") + n) % 26) + else: + result += letter + return result + +@app.route('/rot', methods = ['POST']) +def rot(): + response_url = request.form.get('response_url') + channel_name = request.form.get('channel_name') + channel = request.form.get('channel_id') + query = request.form.get('text') + + match = re.match('^([0-9]+|\*) (.*)$', query) + if (match): + try: + count = int(match.group(1)) + except: + count = None + text = match.group(2) + else: + count = None + text = query + + 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 += "```" + + if (channel_name == "directmessage"): + resp = requests.post(response_url, + json = {"text": reply}, + headers = {"Content-type": "application/json"}) + if (resp.status_code != 200): + app.logger.error("Error posting request to Slack: " + resp.text) + else: + try: + slack_client.chat_postMessage(channel=channel, text=reply) + except SlackApiError as e: + app.logger.error("Slack API error: " + e.response["error"]) + return "" + @slack_events.on("error") def handle_error(error): app.logger.error("Error from Slack: " + str(error))