add funcs.py, small formatting changes, debug only logs

This commit is contained in:
Firepup Sixfifty 2025-02-05 08:31:40 -06:00
parent 3619106e29
commit 924bf210f2
Signed by: Firepup650
SSH key fingerprint: SHA256:cb8sEJwc0kQJ6/nMUhscWRe35itf0NFMdSKl3v4qt48
3 changed files with 17 additions and 4 deletions

4
funcs.py Normal file
View file

@ -0,0 +1,4 @@
def toBool(thing: any) -> bool:
if type(thing) == str:
return thing.lower() in ["yes", "true", "on", "one", "1", "y", "t"]
return bool(thing)

View file

@ -9,7 +9,7 @@ def log(
level: str = "LOG",
origin: str = None, # pyright: ignore[reportArgumentType]
time: Union[dt, str] = "now",
) -> bytes:
) -> None:
message = message.strip()
if level in ["EXIT", "CRASH", "FATAL", "ERROR"]:
stream = stderr
@ -43,4 +43,3 @@ def log(
file=stream,
flush=True,
)
return (log[5:] + "\r\n").encode("utf8")

14
main.py
View file

@ -4,6 +4,7 @@ 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
load_dotenv()
@ -13,12 +14,17 @@ for requiredVar in ["SLACK_BOT_TOKEN", "SLACK_SIGNING_SECRET", "PORT"]:
f'Missing required environment variable "{requiredVar}". Please create a .env file in the same directory as this script and define the missing variable.'
)
print("[INFO] Establishing a connection to slack...", flush=True)
debug = toBool(os.environ.get("DEBUG"))
if debug:
logging.basicConfig(level=logging.DEBUG)
log("Establishing a connection to slack...")
app = App(
token=os.environ.get("SLACK_BOT_TOKEN"),
signing_secret=os.environ.get("SLACK_SIGNING_SECRET"),
)
client = app.client
log("Connected to slack")
@app.function("convert_user_to_channel")
@ -28,6 +34,8 @@ def useridToChannel(
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})
except:
log(format_exc(), "ERROR")
@ -47,8 +55,9 @@ def getMessageContent(
oldest=inputs["message_ts"],
limit=1,
)
message = result["messages"][0]["text"]
if debug:
log(f"{inputs['channel_id']} + {inputs['message_ts']} ->\n" + message)
complete({"message_content": message})
except:
log(format_exc(), "ERROR")
@ -58,4 +67,5 @@ def getMessageContent(
if __name__ == "__main__":
log(f"Starting bot on port {os.environ.get('PORT')}")
app.start(port=int(os.environ.get("PORT")))