Skip to content

Commit

Permalink
Split long messages in SlackChatApp
Browse files Browse the repository at this point in the history
  • Loading branch information
StanGirard committed Apr 22, 2024
1 parent 92193a0 commit a77f29e
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,22 +364,21 @@ def ask_question(self, body, brain_id, thread_ts, question=None):
)
question_response["assistant"] += f"\n\n*Sources:*\n{source_text}"

blocks = [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": question_response["assistant"],
},
}
# Split the message if it's too long
messages = [
question_response["assistant"][i : i + 3000]
for i in range(0, len(question_response["assistant"]), 3000)
]

self.app.client.chat_postMessage(
channel=body["event"]["channel"],
text=question_response["assistant"],
blocks=blocks,
thread_ts=thread_ts,
)
for message in messages:
self.app.client.chat_postMessage(
channel=body["event"]["channel"],
text=message,
blocks=[
{"type": "section", "text": {"type": "mrkdwn", "text": message}}
],
thread_ts=thread_ts,
)
else:
self.app.client.chat_postMessage(
channel=body["event"]["channel"],
Expand Down Expand Up @@ -451,22 +450,24 @@ def handle_iteractive_request(self, payload):
)
question_response["assistant"] += f"\n\n*Sources:*\n{source_text}"

blocks = [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": question_response["assistant"],
},
}
# Split the message if it's too long
messages = [
question_response["assistant"][i : i + 3000]
for i in range(0, len(question_response["assistant"]), 3000)
]

self.app.client.chat_postMessage(
channel=payload["channel"]["id"],
text=question_response["assistant"],
blocks=blocks,
thread_ts=thread_ts,
)
for message in messages:
self.app.client.chat_postMessage(
channel=payload["channel"]["id"],
text=message,
blocks=[
{
"type": "section",
"text": {"type": "mrkdwn", "text": message},
}
],
thread_ts=thread_ts,
)
self.set_brain_id(thread_ts, question_response.get("brain_id"))
else:
self.app.client.chat_postMessage(
Expand Down

0 comments on commit a77f29e

Please sign in to comment.