Skip to content

Commit

Permalink
telegram buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
spatade committed Oct 10, 2023
1 parent 94edf23 commit e76dbdb
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
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
7 changes: 6 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,10 @@
],
"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"}]]}

}
18 changes: 18 additions & 0 deletions tests/unit_test/utility_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2747,3 +2747,21 @@ 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")
from kairon.chat.converters.channels.telegram import TelegramResponseConverter
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")
from kairon.chat.converters.channels.telegram import TelegramResponseConverter
telegram = TelegramResponseConverter("button", "telegram")
response = telegram.button_transformer(input_json)
expected_output = json_data.get("telegram_button_op_multi")
assert expected_output == response

0 comments on commit e76dbdb

Please sign in to comment.