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",
|
level: str = "LOG",
|
||||||
origin: str = None, # pyright: ignore[reportArgumentType]
|
origin: str = None, # pyright: ignore[reportArgumentType]
|
||||||
time: Union[dt, str] = "now",
|
time: Union[dt, str] = "now",
|
||||||
) -> bytes:
|
) -> None:
|
||||||
message = message.strip()
|
message = message.strip()
|
||||||
if level in ["EXIT", "CRASH", "FATAL", "ERROR"]:
|
if level in ["EXIT", "CRASH", "FATAL", "ERROR"]:
|
||||||
stream = stderr
|
stream = stderr
|
||||||
|
@ -43,4 +43,3 @@ def log(
|
||||||
file=stream,
|
file=stream,
|
||||||
flush=True,
|
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 dotenv import load_dotenv
|
||||||
from logs import log
|
from logs import log
|
||||||
from traceback import format_exc
|
from traceback import format_exc
|
||||||
|
from funcs import toBool
|
||||||
|
|
||||||
load_dotenv()
|
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.'
|
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(
|
app = App(
|
||||||
token=os.environ.get("SLACK_BOT_TOKEN"),
|
token=os.environ.get("SLACK_BOT_TOKEN"),
|
||||||
signing_secret=os.environ.get("SLACK_SIGNING_SECRET"),
|
signing_secret=os.environ.get("SLACK_SIGNING_SECRET"),
|
||||||
)
|
)
|
||||||
client = app.client
|
client = app.client
|
||||||
|
log("Connected to slack")
|
||||||
|
|
||||||
|
|
||||||
@app.function("convert_user_to_channel")
|
@app.function("convert_user_to_channel")
|
||||||
|
@ -28,6 +34,8 @@ def useridToChannel(
|
||||||
try:
|
try:
|
||||||
ids = [inputs["user_id"], inputs["workflow_id"]]
|
ids = [inputs["user_id"], inputs["workflow_id"]]
|
||||||
channel_id = client.conversations_open(users=ids)["channel"]["id"]
|
channel_id = client.conversations_open(users=ids)["channel"]["id"]
|
||||||
|
if debug:
|
||||||
|
log(f"{ids} -> {channel_id}", "DEBUG")
|
||||||
complete({"channel_id": channel_id})
|
complete({"channel_id": channel_id})
|
||||||
except:
|
except:
|
||||||
log(format_exc(), "ERROR")
|
log(format_exc(), "ERROR")
|
||||||
|
@ -47,8 +55,9 @@ def getMessageContent(
|
||||||
oldest=inputs["message_ts"],
|
oldest=inputs["message_ts"],
|
||||||
limit=1,
|
limit=1,
|
||||||
)
|
)
|
||||||
|
|
||||||
message = result["messages"][0]["text"]
|
message = result["messages"][0]["text"]
|
||||||
|
if debug:
|
||||||
|
log(f"{inputs['channel_id']} + {inputs['message_ts']} ->\n" + message)
|
||||||
complete({"message_content": message})
|
complete({"message_content": message})
|
||||||
except:
|
except:
|
||||||
log(format_exc(), "ERROR")
|
log(format_exc(), "ERROR")
|
||||||
|
@ -58,4 +67,5 @@ def getMessageContent(
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
log(f"Starting bot on port {os.environ.get('PORT')}")
|
||||||
app.start(port=int(os.environ.get("PORT")))
|
app.start(port=int(os.environ.get("PORT")))
|
||||||
|
|
Loading…
Reference in a new issue