Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix splitting of multiline messages containing buttons #11131

Merged
merged 12 commits into from
Jun 29, 2022
1 change: 1 addition & 0 deletions changelog/11131.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Responses with two newlines will now be split correctly to separate messages if buttons are present, so that buttons will be sent with the last message.
16 changes: 13 additions & 3 deletions rasa/core/channels/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,21 @@ async def send_text_with_buttons(
buttons: List[Dict[Text, Any]],
**kwargs: Any,
) -> None:
await self._persist_message(
self._message(recipient_id, text=text, buttons=buttons)
)
"""Sends text message with buttons."""
message_parts: List[Text] = text.strip().split("\n\n")
last_message_idx: int = len(message_parts) - 1

for idx, message_part in enumerate(message_parts):
await self._persist_message(
self._message(
recipient_id,
text=message_part,
buttons=buttons if idx == last_message_idx else None,
)
)

async def send_custom_json(
self, recipient_id: Text, json_message: Dict[Text, Any], **kwargs: Any
) -> None:
"""Sends custom json."""
await self._persist_message(self._message(recipient_id, custom=json_message))
36 changes: 30 additions & 6 deletions tests/core/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ async def test_send_response(default_channel, default_tracker):
"text": "This message should come first: \n\n"
"This is message two \nThis as well\n\n"
}
multiline_text_message_with_buttons = {
**multiline_text_message,
"buttons": [
{"title": "some title 1", "payload": "/some_payload1"},
{"title": "some title 2", "payload": "/some_payload2"},
],
}
image_only_message = {"image": "https://i.imgur.com/nGF1K8f.jpg"}
text_and_image_message = {
"text": "look at this",
Expand All @@ -56,14 +63,17 @@ async def test_send_response(default_channel, default_tracker):
await default_channel.send_response(
default_tracker.sender_id, multiline_text_message
)
await default_channel.send_response(
default_tracker.sender_id, multiline_text_message_with_buttons
)
await default_channel.send_response(default_tracker.sender_id, image_only_message)
await default_channel.send_response(
default_tracker.sender_id, text_and_image_message
)
await default_channel.send_response(default_tracker.sender_id, custom_json_message)
collected = default_channel.messages

assert len(collected) == 8
assert len(collected) == 10

# text only message
assert collected[0] == {"recipient_id": "my-sender", "text": "hey"}
Expand All @@ -78,20 +88,34 @@ async def test_send_response(default_channel, default_tracker):
"text": "This is message two \nThis as well",
}

# image only message
# multiline text message with buttons, should split on '\n\n'
assert collected[3] == {
"recipient_id": "my-sender",
"image": "https://i.imgur.com/nGF1K8f.jpg",
"text": "This message should come first: ",
}
assert collected[4] == {
"recipient_id": "my-sender",
"text": "This is message two \nThis as well",
"buttons": [
{"title": "some title 1", "payload": "/some_payload1"},
{"title": "some title 2", "payload": "/some_payload2"},
],
}

# text & image combined - will result in two messages
assert collected[4] == {"recipient_id": "my-sender", "text": "look at this"}
# image only message
assert collected[5] == {
"recipient_id": "my-sender",
"image": "https://i.imgur.com/T5xVo.jpg",
"image": "https://i.imgur.com/nGF1K8f.jpg",
}

# text & image combined - will result in two messages
assert collected[6] == {"recipient_id": "my-sender", "text": "look at this"}
assert collected[7] == {
"recipient_id": "my-sender",
"image": "https://i.imgur.com/T5xVo.jpg",
}
assert collected[8] == {"recipient_id": "my-sender", "text": "look at this"}
assert collected[9] == {
"recipient_id": "my-sender",
"custom": {"some_random_arg": "value", "another_arg": "value2"},
}
Expand Down