add funcs.py, small formatting changes, debug only logs
This commit is contained in:
parent
3619106e29
commit
924bf210f2
3 changed files with 17 additions and 4 deletions
4
funcs.py
Normal file
4
funcs.py
Normal 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)
|
3
logs.py
3
logs.py
|
@ -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
14
main.py
|
@ -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")))
|
||||
|
|
Loading…
Reference in a new issue