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

Format text converter channels #1709

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions kairon/chat/converters/channels/messenger.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from kairon.chat.converters.channels.responseconverter import ElementTransformerOps
from kairon.shared.constants import ElementTypes

Expand Down Expand Up @@ -34,6 +36,25 @@ def link_transformer(self, message):
except Exception as ex:
raise Exception(f" Error in MessengerResponseConverter::link_transformer {str(ex)}")

def paragraph_transformer(self, message):
try:
message_template = ElementTransformerOps.getChannelConfig(self.channel, self.message_type)
paragraph_template = json.loads(message_template)
jsoniterator = ElementTransformerOps.json_generator(message)
final_text = ""
for item in jsoniterator:
if item.get("type") == "paragraph":
children = ElementTransformerOps.json_generator(item.get("children", []))
for child in children:
text = child.get("text", "")
final_text += text
final_text += "\n"

paragraph_template["text"] = final_text
return paragraph_template
except Exception as ex:
raise Exception(f"Error in MessengerResponseConverter::paragraph_transformer {str(ex)}")

def button_transformer(self, message):
try:
button_json_temp = {}
Expand Down Expand Up @@ -89,5 +110,7 @@ async def messageConverter(self, message):
return self.button_transformer(message)
elif self.message_type == ElementTypes.QUICK_REPLY.value:
return self.quick_reply_transformer(message)
elif self.message_type == ElementTypes.FORMAT_TEXT.value:
return self.paragraph_transformer(message)
except Exception as ex:
raise Exception(f"Error in MessengerResponseConverter::messageConverter {str(ex)}")
32 changes: 32 additions & 0 deletions kairon/chat/converters/channels/telegram.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from kairon.chat.converters.channels.responseconverter import ElementTransformerOps
from kairon.shared.constants import ElementTypes

Expand Down Expand Up @@ -34,6 +36,34 @@ def link_transformer(self, message):
except Exception as ex:
raise Exception(f" Error in TelegramResponseConverter::link_transformer {str(ex)}")

def paragraph_transformer(self, message):
try:
message_template = ElementTransformerOps.getChannelConfig(self.channel, self.message_type)
paragraph_template = json.loads(message_template)
jsoniterator = ElementTransformerOps.json_generator(message)
final_text = ""
for item in jsoniterator:
if item.get("type") == "paragraph":
children = ElementTransformerOps.json_generator(item.get("children", []))
for child in children:
text = child.get("text", "")
leading_spaces = len(text) - len(text.lstrip())
trailing_spaces = len(text) - len(text.rstrip())
text = text.strip()
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>"
final_text += f"{' ' * leading_spaces}{text}{' ' * trailing_spaces}"
final_text += "\n"

paragraph_template["text"] = final_text
return paragraph_template
except Exception as ex:
raise Exception(f"Error in TelegramResponseConverter::paragraph_transformer {str(ex)}")

def button_transformer(self, message):
try:
jsoniterator = ElementTransformerOps.json_generator(message)
Expand Down Expand Up @@ -65,5 +95,7 @@ async def messageConverter(self, message):
return super().video_transformer(message)
elif self.message_type == ElementTypes.BUTTON.value:
return self.button_transformer(message)
elif self.message_type == ElementTypes.FORMAT_TEXT.value:
return self.paragraph_transformer(message)
except Exception as ex:
raise Exception(f"Error in TelegramResponseConverter::messageConverter {str(ex)}")
32 changes: 31 additions & 1 deletion kairon/chat/converters/channels/whatsapp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

from kairon.chat.converters.channels.responseconverter import ElementTransformerOps
import ujson as json

from kairon.shared.constants import ElementTypes


Expand Down Expand Up @@ -34,6 +34,34 @@ def link_transformer(self, message):
except Exception as ex:
raise Exception(f"Error in WhatsappResponseConverter::link_transformer {str(ex)}")

def paragraph_transformer(self, message):
try:
message_template = ElementTransformerOps.getChannelConfig(self.channel, self.message_type)
paragraph_template = json.loads(message_template)
jsoniterator = ElementTransformerOps.json_generator(message)
final_text = ""
for item in jsoniterator:
if item.get("type") == "paragraph":
children = ElementTransformerOps.json_generator(item.get("children", []))
for child in children:
text = child.get("text", "")
leading_spaces = len(text) - len(text.lstrip())
trailing_spaces = len(text) - len(text.rstrip())
text = text.strip()
if child.get("bold"):
text = f"*{text}*"
if child.get("italic"):
text = f"_{text}_"
if child.get("strikethrough"):
text = f"~{text}~"
final_text += f"{' ' * leading_spaces}{text}{' ' * trailing_spaces}"
final_text += "\n"

paragraph_template["body"] = final_text
return paragraph_template
except Exception as ex:
raise Exception(f"Error in WhatsappResponseConverter::paragraph_transformer {str(ex)}")

def button_transformer(self, message):
try:
message_template = ElementTransformerOps.getChannelConfig(self.channel, self.message_type)
Expand Down Expand Up @@ -121,5 +149,7 @@ async def messageConverter(self, message):
return self.button_transformer(message)
elif self.message_type == ElementTypes.DROPDOWN.value:
return self.dropdown_transformer(message)
elif self.message_type == ElementTypes.FORMAT_TEXT.value:
return self.paragraph_transformer(message)
except Exception as ex:
raise Exception(f"Error in WhatsappResponseConverter::messageConverter {str(ex)}")
2 changes: 1 addition & 1 deletion kairon/chat/handlers/channels/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ async def send_custom_json(
del response["photo"]
api_call = getattr(self, send_functions[("photo",)])
api_call(recipient_id, *response_list, **response)
elif ops_type in ["link", "video"]:
elif ops_type in ["link", "video", "formatText"]:
response_list.append(response.get("text"))
del response["text"]
api_call = getattr(self, send_functions[("text",)])
Expand Down
2 changes: 1 addition & 1 deletion kairon/chat/handlers/channels/whatsapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ async def send_custom_json(
message = json_message.get("data")
messagetype = json_message.get("type")
content_type = {"link": "text", "video": "video", "image": "image", "button": "interactive",
"dropdown": "interactive", "audio": "audio"}
"dropdown": "interactive", "audio": "audio", "formatText": "text"}
if messagetype is not None and messagetype in type_list:
messaging_type = content_type.get(messagetype)
from kairon.chat.converters.channels.response_factory import ConverterFactory
Expand Down
1 change: 1 addition & 0 deletions kairon/shared/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class ElementTypes(str, Enum):
BUTTON = "button"
DROPDOWN = "dropdown"
QUICK_REPLY = "quick_reply"
FORMAT_TEXT = "formatText"


class WhatsappBSPTypes(str, Enum):
Expand Down
14 changes: 13 additions & 1 deletion metadata/message_template.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type_list: ["image","link","video","button","quick_reply","dropdown","audio"]
type_list: ["image","link","video","button","quick_reply","dropdown","audio", "formatText"]
slack:
image: '{
"blocks": [
Expand Down Expand Up @@ -88,6 +88,7 @@ messenger:
video: '{"text":"<data>"}'
button: '{"text":"<data>"}'
body_message: "Please select from quick buttons:"
formatText: '{"text":"<data>"}'

whatsapp:
image: '{
Expand All @@ -112,6 +113,10 @@ whatsapp:
"action":"<action>"
}'
body_message: "Please select from quick buttons:"
formatText: '{
"preview_url": true,
"body":"<data>"
}'

dropdown: '{
"type": "list",
Expand All @@ -128,6 +133,13 @@ telegram:
"reply_to_message_id":0}'
video: '{"text":"<data>"}'
body_message: 'Please select from quick buttons:'
formatText: '{
"text":"<data>",
"parse_mode":"HTML",
"disable_web_page_preview":false,
"disable_notification":false,
"reply_to_message_id":0
}'

msteams:
body_message: "Please select from quick buttons:"
Expand Down
Loading
Loading