custom-slack-steps/main.py

72 lines
2.3 KiB
Python
Raw Permalink Normal View History

2025-01-31 17:32:25 +00:00
import os, sys, logging
from slack_bolt import App, Complete, Fail
from slack_bolt.adapter.socket_mode import SocketModeHandler
from dotenv import load_dotenv
from logs import log
from traceback import format_exc
from funcs import toBool
2025-01-31 17:32:25 +00:00
load_dotenv()
for requiredVar in ["SLACK_BOT_TOKEN", "SLACK_SIGNING_SECRET", "PORT"]:
if not os.environ.get(requiredVar):
raise ValueError(
f'Missing required environment variable "{requiredVar}". Please create a .env file in the same directory as this script and define the missing variable.'
)
debug = toBool(os.environ.get("DEBUG"))
if debug:
logging.basicConfig(level=logging.DEBUG)
log("Establishing a connection to slack...")
2025-01-31 17:32:25 +00:00
app = App(
token=os.environ.get("SLACK_BOT_TOKEN"),
signing_secret=os.environ.get("SLACK_SIGNING_SECRET"),
)
client = app.client
log("Connected to slack")
2025-01-31 17:32:25 +00:00
@app.function("convert_user_to_channel")
def useridToChannel(
inputs: dict, fail: Fail, complete: Complete, logger: logging.Logger
):
try:
ids = [inputs["user_id"], inputs["workflow_id"]]
channel_id = client.conversations_open(users=ids)["channel"]["id"]
if debug:
log(f"{ids} -> {channel_id}", "DEBUG")
complete({"channel_id": channel_id})
2025-01-31 17:32:25 +00:00
except:
log(format_exc(), "ERROR")
fail(
2025-01-31 17:54:43 +00:00
"An error occured app-side trying to process this workflow step. Please contact <@U06JLP2R8JV> about this issue."
)
2025-01-31 17:32:25 +00:00
@app.function("get_message_content")
def getMessageContent(
inputs: dict, fail: Fail, complete: Complete, logger: logging.Logger
):
try:
result = client.conversations_history(
channel=inputs["channel_id"],
inclusive=True,
oldest=inputs["message_ts"],
limit=1,
)
message = result["messages"][0]["text"]
if debug:
log(f"{inputs['channel_id']} + {inputs['message_ts']} ->\n" + message)
2025-01-31 17:32:25 +00:00
complete({"message_content": message})
except:
log(format_exc(), "ERROR")
fail(
2025-01-31 17:54:43 +00:00
"An error occured app-side trying to process this workflow step. Please contact <@U06JLP2R8JV> about this issue."
2025-01-31 17:32:25 +00:00
)
if __name__ == "__main__":
log(f"Starting bot on port {os.environ.get('PORT')}")
2025-01-31 17:32:25 +00:00
app.start(port=int(os.environ.get("PORT")))