Skip to content

Commit

Permalink
fix for email chat formatting for formatText (#1717)
Browse files Browse the repository at this point in the history
* fix for email chat formatting for formatText

* fix for email chat formatting for formatText

---------

Co-authored-by: spandan_mondal <[email protected]>
  • Loading branch information
hasinaxp and spandan_mondal authored Jan 8, 2025
1 parent 8f82fa2 commit 485967f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
11 changes: 10 additions & 1 deletion kairon/shared/actions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,16 @@ def __format_custom_bot_reply(data):
if data.get('type') == "image":
reply = f'<span><img src="{data.get("src")}" alt="{data.get("alt")}" width="150" height="150"/><br/><p>{data.get("alt")}</p></span>'
elif data.get('type') == "paragraph":
reply = f'<p>{reply}</p>'
reply = ""
for child in data['children']:
text = child.get('text', '')
if child.get("bold"):
text = f"<b>{text}</b>"
if child.get("italic"):
text = f"<i>{text}</i>"
if child.get("strikethrough"):
text = f"<s>{text}</s>"
reply += text
elif data.get('type') == "link":
reply = f'<a target="_blank" href="{data.get("href")}">{reply}</a>'
elif data.get('type') == "video":
Expand Down
40 changes: 40 additions & 0 deletions tests/unit_test/action/action_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4060,3 +4060,43 @@ def test_prepare_bot_responses_messages_pop(self):
'feedback, analyzing customer data, and much more.'},
{'role': 'user', 'content': 'I am interested in Kairon and want to know what features it offers'}
]



def test_format_custom_bot_reply_indirect():
# Test text
data = {"text": "This is a test message."}
result = ActionUtility._ActionUtility__format_custom_bot_reply(data)
assert result == "This is a test message."

# Test image
data = {"type": "image", "src": "image_url", "alt": "image_alt", "children": []}
result = ActionUtility._ActionUtility__format_custom_bot_reply(data)
expected = '<span><img src="image_url" alt="image_alt" width="150" height="150"/><br/><p>image_alt</p></span>'
assert result == expected

# Test paragraph with formatting
data = {
"type": "paragraph",
"children": [
{"text": "Bold text", "bold": True},
{"text": "Italic text ", "italic": True},
{"text": "Strike text ", "strikethrough": True},
{"text": " and normal text."}
]
}
result = ActionUtility._ActionUtility__format_custom_bot_reply(data)
expected = "<b>Bold text</b><i>Italic text </i><s>Strike text </s> and normal text."
assert result == expected

# Test link
data = {"type": "link", "href": "http://example.com", "children": [{"text": "Example"}]}
result = ActionUtility._ActionUtility__format_custom_bot_reply(data)
expected = '<a target="_blank" href="http://example.com">Example</a>'
assert result == expected

# Test video
data = {"type": "video", "url": "http://example.com/video", "children": []}
result = ActionUtility._ActionUtility__format_custom_bot_reply(data)
expected = '<a target="_blank" href="http://example.com/video">http://example.com/video</a>'
assert result == expected

0 comments on commit 485967f

Please sign in to comment.