Implement getting message content
This commit is contained in:
parent
1dd6706b4a
commit
862d5be5ee
3 changed files with 110 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
__pycache__/**
|
||||||
|
**/__pycache__/**
|
||||||
|
.env
|
46
logs.py
Normal file
46
logs.py
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
from datetime import datetime as dt, timezone as tz
|
||||||
|
from sys import stdout, stderr
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
|
||||||
|
def log(
|
||||||
|
message: str,
|
||||||
|
level: str = "LOG",
|
||||||
|
origin: str = None, # pyright: ignore[reportArgumentType]
|
||||||
|
time: Union[dt, str] = "now",
|
||||||
|
) -> bytes:
|
||||||
|
message = message.strip()
|
||||||
|
if level in ["EXIT", "CRASH", "FATAL", "ERROR"]:
|
||||||
|
stream = stderr
|
||||||
|
else:
|
||||||
|
stream = stdout
|
||||||
|
if time == "now":
|
||||||
|
dtime = dt.now(tz.utc)
|
||||||
|
dtime.replace(tzinfo=tz.utc)
|
||||||
|
elif type(time) == str:
|
||||||
|
raise ValueError('Only "now" is an accepted string argument for time')
|
||||||
|
elif type(time) == dt:
|
||||||
|
dtime = time
|
||||||
|
else:
|
||||||
|
raise ValueError("time must either be a string or a dt object")
|
||||||
|
time = dtime.strftime("%d-%m-%Y %H:%M:%S")
|
||||||
|
log = ""
|
||||||
|
if not "\n" in message:
|
||||||
|
log = f"[{level}]{'['+origin+']' if origin else ''}[{time}] {message}"
|
||||||
|
print(
|
||||||
|
f"[{level}]{'['+origin+']' if origin else ''}[{time}] {message}",
|
||||||
|
file=stream,
|
||||||
|
flush=True,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
for line in message.split("\n"):
|
||||||
|
log = (
|
||||||
|
log + f"\r\n[{level}]{'['+origin+']' if origin else ''}[{time}] {line}"
|
||||||
|
)
|
||||||
|
print(
|
||||||
|
f"[{level}]{'['+origin+']' if origin else ''}[{time}] {line}",
|
||||||
|
file=stream,
|
||||||
|
flush=True,
|
||||||
|
)
|
||||||
|
return (log[5:] + "\r\n").encode("utf8")
|
61
main.py
61
main.py
|
@ -0,0 +1,61 @@
|
||||||
|
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
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
|
||||||
|
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.'
|
||||||
|
)
|
||||||
|
|
||||||
|
print("[INFO] Establishing a connection to slack...", flush=True)
|
||||||
|
app = App(
|
||||||
|
token=os.environ.get("SLACK_BOT_TOKEN"),
|
||||||
|
signing_secret=os.environ.get("SLACK_SIGNING_SECRET"),
|
||||||
|
)
|
||||||
|
client = app.client
|
||||||
|
|
||||||
|
|
||||||
|
@app.function("convert_user_to_channel")
|
||||||
|
def useridToChannel(
|
||||||
|
inputs: dict, fail: Fail, complete: Complete, logger: logging.Logger
|
||||||
|
):
|
||||||
|
user_id = inputs["user_id"]
|
||||||
|
try:
|
||||||
|
complete({"channel_id": user_id})
|
||||||
|
except:
|
||||||
|
log(format_exc(), "ERROR")
|
||||||
|
# fail("Am fail.")
|
||||||
|
# We can't run fail() here, unsure why.
|
||||||
|
|
||||||
|
|
||||||
|
@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"]
|
||||||
|
log(message, "DEBUG")
|
||||||
|
complete({"message_content": message})
|
||||||
|
except:
|
||||||
|
log(format_exc(), "ERROR")
|
||||||
|
fail(
|
||||||
|
"An error occured app-side trying to process this workflow step. Please contact <@U06JLP2R8JV> (Firepup650) about this issue."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.start(port=int(os.environ.get("PORT")))
|
Loading…
Reference in a new issue