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

telegram buttons #1049

Closed
Closed
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/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ def link_transformer(self, message):
except Exception as ex:
raise Exception(f" Error in TelegramResponseConverter::link_transformer {str(ex)}")

def button_transformer(self, message):
try:
jsoniterator = ElementTransformerOps.json_generator(message)
reply_markup = {}
inline_keyboard = []
reply_markup.update({"inline_keyboard": inline_keyboard})
inline_keyboard_array = []
for item in jsoniterator:
if item.get("type") == ElementTypes.BUTTON.value:
title = ElementTransformerOps.json_generator(item.get("children"))
for titletext in title:
button_text = titletext.get("text")
btn_body = {}
btn_body.update({"text": button_text})
btn_body.update({"callback_data": item.get("value")})
inline_keyboard_array.append(btn_body)
inline_keyboard.append(inline_keyboard_array)
return reply_markup
except Exception as ex:
raise Exception(f"Exception in TelegramResponseConverter::button_transfomer: {str(ex)}")

async def messageConverter(self, message):
try:
if self.message_type == ElementTypes.IMAGE.value:
Expand All @@ -42,5 +63,7 @@ async def messageConverter(self, message):
return self.link_transformer(message)
elif self.message_type == ElementTypes.VIDEO.value:
return super().video_transformer(message)
elif self.message_type == ElementTypes.BUTTON.value:
return self.button_transformer(message)
except Exception as ex:
raise Exception(f"Error in TelegramResponseConverter::messageConverter {str(ex)}")
5 changes: 5 additions & 0 deletions kairon/chat/handlers/channels/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
from kairon.chat.agent_processor import AgentProcessor
from kairon import Utility
from kairon.chat.converters.channels.response_factory import ConverterFactory
from kairon.chat.converters.channels.responseconverter import ElementTransformerOps
import json

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -159,6 +161,9 @@ async def send_custom_json(
del response["text"]
api_call = getattr(self, send_functions[("text",)])
api_call(recipient_id, *response_list, **response)
elif ops_type in ["button"]:
body_default = ElementTransformerOps.getChannelConfig(ChannelTypes.TELEGRAM.value, "body_message")
self.send_message(recipient_id, text=body_default, reply_markup=json.dumps(response))
else:
self.send_message(recipient_id, str(json_message))
except Exception as ap:
Expand Down
1 change: 1 addition & 0 deletions metadata/message_template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ telegram:
"disable_notification":false,
"reply_to_message_id":0}'
video: '{"text":"<data>"}'
body_message: 'Please select from quick buttons:'

msteams:
body_message: "Please select from quick buttons:"
Expand Down
9 changes: 8 additions & 1 deletion tests/testing_data/channel_data/channel_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -457,5 +457,12 @@
],
"button": "Submit Option"
}
}
},
"telegram_button_op_one": {"inline_keyboard": [[{"text": "One", "callback_data": "single button is clicked"}]]},
"telegram_button_op_multi": {"inline_keyboard": [[{"text": "Veg", "callback_data": "Vegetables only"},
{"text": "Non-veg", "callback_data": "fish food served"},
{"text": "Desert", "callback_data": "Only Desert food"}]]},

"button_one_exception": [{"type":"button","value":"single button is clicked","id":"5"},
{"type":"paragraph","children":[{"text":""}]}]
}
33 changes: 33 additions & 0 deletions tests/unit_test/utility_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from kairon.shared.models import TemplateType
from kairon.shared.utils import Utility, MailUtility
from kairon.shared.verification.email import QuickEmailVerification
from kairon.chat.converters.channels.telegram import TelegramResponseConverter


class TestUtility:
Expand Down Expand Up @@ -2747,3 +2748,35 @@ def test_is_picklable_for_mongo_failure(self):
assert not Utility.is_picklable_for_mongo({"requests": requests})
assert not Utility.is_picklable_for_mongo({"utility": Utility})
assert not Utility.is_picklable_for_mongo({"is_picklable_for_mongo": Utility.is_picklable_for_mongo})

def test_button_transformer_telegram_single_button(self):
json_data = json.load(open("tests/testing_data/channel_data/channel_data.json"))
input_json = json_data.get("button_one")
telegram = TelegramResponseConverter("button", "telegram")
response = telegram.button_transformer(input_json)
expected_output = json_data.get("telegram_button_op_one")
assert expected_output == response

def test_button_transformer_telegram_multi_buttons(self):
json_data = json.load(open("tests/testing_data/channel_data/channel_data.json"))
input_json = json_data.get("button_three")
telegram = TelegramResponseConverter("button", "telegram")
response = telegram.button_transformer(input_json)
expected_output = json_data.get("telegram_button_op_multi")
assert expected_output == response

@pytest.mark.asyncio
async def test_button_transformer_telegram_messageConverter(self):
json_data = json.load(open("tests/testing_data/channel_data/channel_data.json"))
input_json = json_data.get("button_three")
telegram = ConverterFactory.getConcreteInstance("button", "telegram")
response = await telegram.messageConverter(input_json)
expected_output = json_data.get("telegram_button_op_multi")
assert expected_output == response

def test_button_transformer_telegram_exception(self):
json_data = json.load(open("tests/testing_data/channel_data/channel_data.json"))
input_json = json_data.get("button_one_exception")
telegram = TelegramResponseConverter("button", "telegram")
with pytest.raises(Exception):
telegram.button_transformer(input_json)
Loading