Update app.py
Browse files
app.py
CHANGED
@@ -63,56 +63,30 @@ async def on_message(message):
|
|
63 |
await post_to_slack(message.author, message.content, message.channel.name, message.jump_url, slack_mention, trigger)
|
64 |
break
|
65 |
|
66 |
-
# ask-for-help
|
67 |
-
# discord.ForumChannel.last_message_id -> The last thread ID that was created on this forum.
|
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
|
75 |
-
try:
|
76 |
-
# title
|
77 |
-
response = slack_client.chat_postMessage(
|
78 |
-
channel=SLACK_CHANNEL_ID,
|
79 |
-
text=f"New post in #ask-for-help by {message.author}: *{message.channel.name}*\n{message.jump_url}"
|
80 |
-
)
|
81 |
-
# reply in thread
|
82 |
-
thread_ts = response['ts']
|
83 |
-
slack_client.chat_postMessage(
|
84 |
-
channel=SLACK_CHANNEL_ID,
|
85 |
-
text=content,
|
86 |
-
thread_ts=thread_ts
|
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(1259415803879751700)
|
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 |
-
|
103 |
slack_client.chat_postMessage(
|
104 |
channel=SLACK_CHANNEL_ID_TEST,
|
105 |
text=content,
|
106 |
thread_ts=thread_ts
|
107 |
)
|
108 |
else:
|
109 |
-
|
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 |
-
|
116 |
slack_client.chat_postMessage(
|
117 |
channel=SLACK_CHANNEL_ID_TEST,
|
118 |
text=content,
|
@@ -121,15 +95,24 @@ async def on_message(message):
|
|
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 |
-
|
126 |
try:
|
127 |
response = slack_client.conversations_history(
|
128 |
channel=SLACK_CHANNEL_ID_TEST,
|
129 |
-
limit=
|
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']
|
@@ -137,7 +120,7 @@ def find_slack_thread_ts(thread_id):
|
|
137 |
except SlackApiError as e:
|
138 |
print(f"Error fetching Slack messages: {e.response['error']}")
|
139 |
|
140 |
-
return None
|
141 |
|
142 |
|
143 |
|
|
|
63 |
await post_to_slack(message.author, message.content, message.channel.name, message.jump_url, slack_mention, trigger)
|
64 |
break
|
65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
ask_for_help = await bot.fetch_channel(1259415803879751700)
|
67 |
if ask_for_help.last_message_id == message.channel.id:
|
68 |
content = message.content
|
69 |
thread_id = message.channel.id
|
70 |
|
71 |
try:
|
72 |
+
|
73 |
thread_ts = find_slack_thread_ts(thread_id)
|
74 |
|
75 |
if thread_ts:
|
76 |
+
|
77 |
slack_client.chat_postMessage(
|
78 |
channel=SLACK_CHANNEL_ID_TEST,
|
79 |
text=content,
|
80 |
thread_ts=thread_ts
|
81 |
)
|
82 |
else:
|
83 |
+
|
84 |
response = slack_client.chat_postMessage(
|
85 |
channel=SLACK_CHANNEL_ID_TEST,
|
86 |
text=f"New post in #ask-for-help by {message.author}: *{message.channel.name}*\n{message.jump_url}\n\nDiscord Thread ID: {thread_id}"
|
87 |
)
|
88 |
thread_ts = response['ts']
|
89 |
+
|
90 |
slack_client.chat_postMessage(
|
91 |
channel=SLACK_CHANNEL_ID_TEST,
|
92 |
text=content,
|
|
|
95 |
except SlackApiError as e:
|
96 |
print(f"Error posting to Slack: {e.response['error']}")
|
97 |
|
98 |
+
|
99 |
def find_slack_thread_ts(thread_id):
|
100 |
+
"""Search for a Slack thread with the given Discord thread ID and return the thread_ts."""
|
101 |
try:
|
102 |
response = slack_client.conversations_history(
|
103 |
channel=SLACK_CHANNEL_ID_TEST,
|
104 |
+
limit=200
|
105 |
)
|
106 |
messages = response['messages']
|
107 |
|
108 |
+
while response['has_more']:
|
109 |
+
response = slack_client.conversations_history(
|
110 |
+
channel=SLACK_CHANNEL_ID_TEST,
|
111 |
+
limit=200,
|
112 |
+
cursor=response['response_metadata']['next_cursor']
|
113 |
+
)
|
114 |
+
messages.extend(response['messages'])
|
115 |
+
|
116 |
for message in messages:
|
117 |
if f"Discord Thread ID: {thread_id}" in message['text']:
|
118 |
return message['ts']
|
|
|
120 |
except SlackApiError as e:
|
121 |
print(f"Error fetching Slack messages: {e.response['error']}")
|
122 |
|
123 |
+
return None
|
124 |
|
125 |
|
126 |
|