From: Carl Worth Date: Tue, 13 Oct 2020 23:07:01 +0000 (-0700) Subject: Fold all of turbot/slack.py up into turbot_flask/turbot.py X-Git-Url: https://git.cworth.org/git?p=turbot;a=commitdiff_plain;h=aa2bae20d2fae5878b1358d62b86d8f807abc835 Fold all of turbot/slack.py up into turbot_flask/turbot.py Just making the Flask bot self-contained at this point, (since we're basically about to abandon it in its development for the time being while we focus on the Lambda-based bot that has access to DynamoDB). --- diff --git a/turbot/slack.py b/turbot/slack.py deleted file mode 100644 index 8dc5ccc..0000000 --- a/turbot/slack.py +++ /dev/null @@ -1,60 +0,0 @@ -from flask import current_app -from slack import WebClient -from slack.signature import SignatureVerifier -import os -import requests - -slack_signing_secret = os.environ['SLACK_SIGNING_SECRET'] -slack_bot_token = os.environ['SLACK_BOT_TOKEN'] - -signature_verifier = SignatureVerifier(slack_signing_secret) -slack_client = WebClient(slack_bot_token) - -def slack_is_valid_request(request): - """Returns true if request actually came from Slack. - - By means of checking the requests signature together with the slack - signing key. - - Note: If flask is in debug mode, this function will always return true.""" - - if current_app.debug: - return True - - data = request.get_data() - headers = request.headers - - return signature_verifier.is_valid_request(data, headers) - -def slack_send_reply(request, text): - """Send a Slack message as a reply to a specified request. - - If the request is associated with a direct message, the reply is - made by using the "response_url" from the request. Otherwise, the - reply will be sent to the channel associated with the request. - - Note: If flask is in debug mode, this function will just print the - text to stdout.""" - - app = current_app - channel_name = request.form.get('channel_name') - response_url = request.form.get('response_url') - channel = request.form.get('channel_id') - - if (app.debug): - print("Sending message to channel '{}': {}".format(channel, text)) - return - - if (channel_name == "directmessage"): - resp = requests.post(response_url, - json = {"text": text}, - headers = {"Content-type": "application/json"}) - if (resp.status_code != 200): - app.logger.error("Error posting request to Slack: " + resp.text) - else: - slack_send_message(channel, text) - -def slack_send_message(channel, text): - """Send a Slack message to a specified channel.""" - - slack_client.chat_postMessage(channel=channel, text=text) diff --git a/turbot_flask/turbot.py b/turbot_flask/turbot.py index 2eaa268..ea0607c 100644 --- a/turbot_flask/turbot.py +++ b/turbot_flask/turbot.py @@ -1,12 +1,15 @@ #!/usr/bin/env python3 +from flask import current_app +from slack import WebClient +from slack.signature import SignatureVerifier from flask import Flask, request, make_response from slackeventsapi import SlackEventAdapter -from turbot.slack import slack_is_valid_request, slack_send_reply + import os +import requests import threading from turbot.rot import rot -from turbot.slack import slack_send_message from turbot.sheets import sheets_create app = Flask(__name__) @@ -14,6 +17,60 @@ app = Flask(__name__) slack_signing_secret = os.environ['SLACK_SIGNING_SECRET'] slack_events = SlackEventAdapter(slack_signing_secret, "/events", app) +slack_bot_token = os.environ['SLACK_BOT_TOKEN'] + +signature_verifier = SignatureVerifier(slack_signing_secret) +slack_client = WebClient(slack_bot_token) + +def slack_is_valid_request(request): + """Returns true if request actually came from Slack. + + By means of checking the requests signature together with the slack + signing key. + + Note: If flask is in debug mode, this function will always return true.""" + + if current_app.debug: + return True + + data = request.get_data() + headers = request.headers + + return signature_verifier.is_valid_request(data, headers) + +def slack_send_reply(request, text): + """Send a Slack message as a reply to a specified request. + + If the request is associated with a direct message, the reply is + made by using the "response_url" from the request. Otherwise, the + reply will be sent to the channel associated with the request. + + Note: If flask is in debug mode, this function will just print the + text to stdout.""" + + app = current_app + channel_name = request.form.get('channel_name') + response_url = request.form.get('response_url') + channel = request.form.get('channel_id') + + if (app.debug): + print("Sending message to channel '{}': {}".format(channel, text)) + return + + if (channel_name == "directmessage"): + resp = requests.post(response_url, + json = {"text": text}, + headers = {"Content-type": "application/json"}) + if (resp.status_code != 200): + app.logger.error("Error posting request to Slack: " + resp.text) + else: + slack_send_message(channel, text) + +def slack_send_message(channel, text): + """Send a Slack message to a specified channel.""" + + slack_client.chat_postMessage(channel=channel, text=text) + @app.route('/rot', methods = ['POST']) def rot_route(): """Implements the /rot slash command for Slack replying in Slack