2024-06-03 18:24:56 -05:00
|
|
|
import os
|
|
|
|
from slack_bolt import App
|
|
|
|
from slack_bolt.adapter.socket_mode import SocketModeHandler
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
import firepup650 as fp
|
|
|
|
|
|
|
|
input = fp.replitInput
|
|
|
|
|
2024-06-04 17:18:48 -05:00
|
|
|
fp.replitCursor = (
|
|
|
|
fp.bcolors.REPLIT + ">>>" + fp.bcolors.RESET
|
|
|
|
) # Totally not hijacking one of my functions to use ;P
|
2024-06-03 18:24:56 -05:00
|
|
|
|
|
|
|
load_dotenv()
|
|
|
|
|
2024-06-04 16:08:13 -05:00
|
|
|
for requiredVar in ["SLACK_BOT_TOKEN", "SLACK_APP_TOKEN"]:
|
|
|
|
if not os.environ.get(requiredVar):
|
2024-06-04 17:18:48 -05:00
|
|
|
raise ValueError(
|
|
|
|
f'Missing required environment variable "{requiredVar}". Please create a .env file in the same directory as this script and define it.'
|
|
|
|
)
|
2024-06-03 18:24:56 -05:00
|
|
|
|
2024-06-04 17:15:21 -05:00
|
|
|
print("Establishing a connection to slack...")
|
2024-06-03 18:24:56 -05:00
|
|
|
app = App(token=os.environ.get("SLACK_BOT_TOKEN"))
|
|
|
|
client = app.client
|
|
|
|
|
2024-06-04 17:15:21 -05:00
|
|
|
print("Building a list of users, please stand by.")
|
|
|
|
users_list = []
|
|
|
|
cursor = "N/A"
|
|
|
|
pages = 0
|
|
|
|
while cursor:
|
|
|
|
data = ""
|
|
|
|
if cursor != "N/A":
|
|
|
|
data = client.users_list(cursor=cursor, limit=1000)
|
|
|
|
else:
|
|
|
|
data = client.users_list(limit=1000)
|
|
|
|
cursor = data["response_metadata"]["next_cursor"]
|
|
|
|
users_list.extend(data["members"])
|
|
|
|
pages += 1
|
|
|
|
print(f"Pages of users loaded: {pages}")
|
|
|
|
print("All pages loaded, generating user mappings now.")
|
|
|
|
del pages
|
|
|
|
user_mappings = {}
|
|
|
|
for user in users_list:
|
2024-06-04 17:18:48 -05:00
|
|
|
user_mappings[f"<@{user['id']}>"] = (
|
|
|
|
f"<@{user['profile']['display_name']}>"
|
|
|
|
if user["profile"]["display_name"]
|
|
|
|
else f"<@{user['id']}>"
|
|
|
|
)
|
2024-06-04 17:15:21 -05:00
|
|
|
|
|
|
|
print("User mappings loaded. User count:", len(user_mappings))
|
|
|
|
|
2024-06-03 18:24:56 -05:00
|
|
|
if __name__ == "__main__":
|
2024-06-04 16:37:19 -05:00
|
|
|
print("^D at any time to terminate program")
|
2024-06-03 18:24:56 -05:00
|
|
|
while 1:
|
2024-06-04 16:37:19 -05:00
|
|
|
chan = input("Channel ID")
|
2024-06-03 18:24:56 -05:00
|
|
|
try:
|
|
|
|
print("^C to change channel")
|
|
|
|
while 1:
|
2024-06-04 16:37:19 -05:00
|
|
|
thread = input("Reply to a thread? (y|N)").lower().startswith("y")
|
2024-06-03 18:24:56 -05:00
|
|
|
ts = None
|
|
|
|
if thread:
|
2024-06-04 17:18:48 -05:00
|
|
|
hasID = (
|
|
|
|
input("Do you have the TS ID? (y|N))").lower().startswith("y")
|
|
|
|
)
|
2024-06-03 18:24:56 -05:00
|
|
|
if not hasID:
|
|
|
|
try:
|
2024-06-04 17:18:48 -05:00
|
|
|
print(
|
|
|
|
"Getting the last 50 messages for threading options..."
|
|
|
|
)
|
2024-06-03 18:24:56 -05:00
|
|
|
res = client.conversations_history(
|
2024-06-04 16:37:19 -05:00
|
|
|
channel=chan, inclusive=True, limit=50
|
2024-06-03 18:24:56 -05:00
|
|
|
)
|
2024-06-04 16:37:19 -05:00
|
|
|
messages = res["messages"]
|
|
|
|
texts = {}
|
2024-06-04 17:15:21 -05:00
|
|
|
print("Building messages, this might take a little bit...")
|
2024-06-04 16:37:19 -05:00
|
|
|
for i in range(len(messages)):
|
2024-06-04 17:15:21 -05:00
|
|
|
label = f'{messages[i]["text"]} ({messages[i]["ts"]})'
|
|
|
|
for user in user_mappings:
|
|
|
|
label = label.replace(user, user_mappings[user])
|
|
|
|
texts[label] = i
|
2024-06-04 17:18:48 -05:00
|
|
|
found = messages[
|
|
|
|
fp.menu(
|
|
|
|
texts,
|
|
|
|
"Please select the message to reply to as a thread",
|
|
|
|
)
|
|
|
|
]
|
2024-06-03 18:24:56 -05:00
|
|
|
ts = found["ts"]
|
|
|
|
except Exception as E:
|
|
|
|
print(f"Exception: {E}")
|
2024-06-04 16:37:19 -05:00
|
|
|
break
|
2024-06-03 18:24:56 -05:00
|
|
|
else:
|
|
|
|
ts = input("TS ID")
|
2024-06-04 17:18:48 -05:00
|
|
|
print(
|
|
|
|
"^C to change/exit thread (^C twice if you want to change channel)"
|
|
|
|
)
|
2024-06-04 16:37:19 -05:00
|
|
|
try:
|
2024-06-04 17:18:48 -05:00
|
|
|
while 1:
|
2024-06-05 00:06:46 -05:00
|
|
|
msg = input(
|
|
|
|
"[THREAD] Message (Raw text, not blocks)"
|
|
|
|
).replace("\\n", "\n")
|
2024-06-04 17:18:48 -05:00
|
|
|
try:
|
2024-06-04 17:23:56 -05:00
|
|
|
client.chat_postMessage(
|
|
|
|
channel=chan, text=msg, thread_ts=ts
|
2024-06-03 18:24:56 -05:00
|
|
|
)
|
2024-06-04 17:23:56 -05:00
|
|
|
print("Message sent (to the thread)!")
|
2024-06-04 17:18:48 -05:00
|
|
|
except Exception as E:
|
|
|
|
print(f"Exception: {E}")
|
2024-06-04 16:37:19 -05:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
print()
|
|
|
|
if ts:
|
|
|
|
continue
|
2024-06-05 00:06:46 -05:00
|
|
|
msg = input("[CHANNEL] Message (Raw text, not blocks)").replace(
|
|
|
|
"\\n", "\n"
|
|
|
|
)
|
2024-06-03 18:24:56 -05:00
|
|
|
try:
|
2024-06-04 17:23:56 -05:00
|
|
|
client.chat_postMessage(channel=chan, text=msg)
|
|
|
|
print("Message sent (to the channel)!")
|
2024-06-03 18:24:56 -05:00
|
|
|
except Exception as E:
|
|
|
|
print(f"Exception: {E}")
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print()
|