Created
April 22, 2024 19:17
-
-
Save jjanczyszyn/8c110c04aff7cbd41a5b722d87695f02 to your computer and use it in GitHub Desktop.
dummy wavecli service exposing slack commands endpoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import requests | |
from flask import Flask, request, jsonify | |
app: Flask = Flask(__name__) | |
SLACK_SIGNING_SECRET: str = "your_slack_signing_secret_here" | |
def send_slack_message(channel: str, text: str, thread_ts: str = None) -> dict: | |
url: str = "https://slack.com/api/chat.postMessage" | |
headers: dict = { | |
"Authorization": f"Bearer {SLACK_SIGNING_SECRET}", | |
"Content-Type": "application/json" | |
} | |
data: dict = { | |
"channel": channel, | |
"text": text, | |
"thread_ts": thread_ts | |
} | |
response: requests.Response = requests.post(url, headers=headers, json=data) | |
return response.json() | |
def rollback(message: str) -> str: | |
channel_id: str = request.form.get('channel_id') | |
thread_ts: str = request.form.get('message_ts') | |
send_slack_message(channel_id, f"Rollback initiated: {message}...", thread_ts) | |
time.sleep(2) | |
send_slack_message(channel_id, "Rollback in progress...", thread_ts) | |
time.sleep(3) | |
send_slack_message(channel_id, "Rollback completed!", thread_ts) | |
return "Rollback completed" | |
def deploy(message: str) -> str: | |
channel_id: str = request.form.get('channel_id') | |
thread_ts: str = request.form.get('message_ts') | |
send_slack_message(channel_id, f"Deploy initiated: {message}...", thread_ts) | |
time.sleep(2) | |
send_slack_message(channel_id, "Deploy in progress...", thread_ts) | |
time.sleep(3) | |
send_slack_message(channel_id, "Deploy completed!", thread_ts) | |
return "Deploy completed" | |
@app.route('/slack/command', methods=['POST']) | |
def slack_command() -> str: | |
if request.headers.get('X-Slack-Signature') != SLACK_SIGNING_SECRET: | |
return jsonify({"error": "Invalid request"}), 401 | |
command: str = request.form.get('command') | |
message_ts: str = request.form.get('message_ts') | |
text: str = request.form.get('text') | |
if command == '/rollback': | |
response_text: str = rollback(text) | |
elif command == '/deploy': | |
response_text: str = deploy(text) | |
else: | |
response_text: str = "Unknown command" | |
return jsonify({"response_type": "in_channel", "text": response_text}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment