Spaces:
Paused
Paused
testing threads
Browse files
app.py
CHANGED
|
@@ -9,6 +9,7 @@ from slack_sdk.errors import SlackApiError
|
|
| 9 |
DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
|
| 10 |
SLACK_BOT_TOKEN = os.getenv('BOT_USER_OAUTH_TOKEN_HF')
|
| 11 |
SLACK_CHANNEL_ID = os.getenv('SLACK_CHANNEL_ID_HF')
|
|
|
|
| 12 |
|
| 13 |
TRIGGERS = {
|
| 14 |
"discord bot": "<@U051DB2754M>", # Adam
|
|
@@ -67,7 +68,7 @@ async def on_message(message):
|
|
| 67 |
# when message is posted
|
| 68 |
# fetch ask-for-help channel
|
| 69 |
# if ask-for-help.last_message_id == message.id of on_message -> success
|
| 70 |
-
|
| 71 |
ask_for_help = await bot.fetch_channel(1019883044724822016)
|
| 72 |
if ask_for_help.last_message_id == message.channel.id:
|
| 73 |
content = message.content
|
|
@@ -86,6 +87,63 @@ async def on_message(message):
|
|
| 86 |
)
|
| 87 |
except SlackApiError as e:
|
| 88 |
print(f"Error posting to Slack: {e.response['error']}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
async def post_to_slack(author, content, channel, url, slack_mention, trigger):
|
|
@@ -97,6 +155,7 @@ async def post_to_slack(author, content, channel, url, slack_mention, trigger):
|
|
| 97 |
except SlackApiError as e:
|
| 98 |
print(f"Error posting to Slack: {e.response['error']}")
|
| 99 |
|
|
|
|
| 100 |
# runs discord bot in thread = helps avoid blocking calls
|
| 101 |
def run_bot():
|
| 102 |
bot.run(DISCORD_TOKEN)
|
|
|
|
| 9 |
DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
|
| 10 |
SLACK_BOT_TOKEN = os.getenv('BOT_USER_OAUTH_TOKEN_HF')
|
| 11 |
SLACK_CHANNEL_ID = os.getenv('SLACK_CHANNEL_ID_HF')
|
| 12 |
+
SLACK_CHANNEL_ID_TEST = 'C07B4KNU5BQ'
|
| 13 |
|
| 14 |
TRIGGERS = {
|
| 15 |
"discord bot": "<@U051DB2754M>", # Adam
|
|
|
|
| 68 |
# when message is posted
|
| 69 |
# fetch ask-for-help channel
|
| 70 |
# if ask-for-help.last_message_id == message.id of on_message -> success
|
| 71 |
+
#--------------------------------------------------------------------------------------------------------------------
|
| 72 |
ask_for_help = await bot.fetch_channel(1019883044724822016)
|
| 73 |
if ask_for_help.last_message_id == message.channel.id:
|
| 74 |
content = message.content
|
|
|
|
| 87 |
)
|
| 88 |
except SlackApiError as e:
|
| 89 |
print(f"Error posting to Slack: {e.response['error']}")
|
| 90 |
+
#--------------------------------------------------------------------------------------------------------------------
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
ask_for_help = await bot.fetch_channel(1019883044724822016)
|
| 94 |
+
if ask_for_help.last_message_id == message.channel.id:
|
| 95 |
+
content = message.content
|
| 96 |
+
thread_id = message.channel.id
|
| 97 |
+
|
| 98 |
+
try:
|
| 99 |
+
thread_ts = find_slack_thread_ts(thread_id)
|
| 100 |
+
|
| 101 |
+
if thread_ts:
|
| 102 |
+
# if exists, post in existing thread (don't create new one)
|
| 103 |
+
slack_client.chat_postMessage(
|
| 104 |
+
channel=SLACK_CHANNEL_ID_TEST,
|
| 105 |
+
text=content,
|
| 106 |
+
thread_ts=thread_ts
|
| 107 |
+
)
|
| 108 |
+
else:
|
| 109 |
+
# if not, create new thread
|
| 110 |
+
response = slack_client.chat_postMessage(
|
| 111 |
+
channel=SLACK_CHANNEL_ID_TEST,
|
| 112 |
+
text=f"New post in #ask-for-help by {message.author}: *{message.channel.name}*\n{message.jump_url}\n\nDiscord Thread ID: {thread_id}"
|
| 113 |
+
)
|
| 114 |
+
thread_ts = response['ts']
|
| 115 |
+
# first reply
|
| 116 |
+
slack_client.chat_postMessage(
|
| 117 |
+
channel=SLACK_CHANNEL_ID_TEST,
|
| 118 |
+
text=content,
|
| 119 |
+
thread_ts=thread_ts
|
| 120 |
+
)
|
| 121 |
+
except SlackApiError as e:
|
| 122 |
+
print(f"Error posting to Slack: {e.response['error']}")
|
| 123 |
+
|
| 124 |
+
def find_slack_thread_ts(thread_id):
|
| 125 |
+
# search for a Slack thread based on Discord thread_id"""
|
| 126 |
+
try:
|
| 127 |
+
response = slack_client.conversations_history(
|
| 128 |
+
channel=SLACK_CHANNEL_ID_TEST,
|
| 129 |
+
limit=1000 # adjustable
|
| 130 |
+
)
|
| 131 |
+
messages = response['messages']
|
| 132 |
+
|
| 133 |
+
for message in messages:
|
| 134 |
+
if f"Discord Thread ID: {thread_id}" in message['text']:
|
| 135 |
+
return message['ts']
|
| 136 |
+
|
| 137 |
+
except SlackApiError as e:
|
| 138 |
+
print(f"Error fetching Slack messages: {e.response['error']}")
|
| 139 |
+
|
| 140 |
+
return None
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
#----------------------------------------------------------------------------------------------
|
| 145 |
+
|
| 146 |
+
|
| 147 |
|
| 148 |
|
| 149 |
async def post_to_slack(author, content, channel, url, slack_mention, trigger):
|
|
|
|
| 155 |
except SlackApiError as e:
|
| 156 |
print(f"Error posting to Slack: {e.response['error']}")
|
| 157 |
|
| 158 |
+
|
| 159 |
# runs discord bot in thread = helps avoid blocking calls
|
| 160 |
def run_bot():
|
| 161 |
bot.run(DISCORD_TOKEN)
|