From 2c0f2530a7d730e00d4f59029037df3cb4fc70fc Mon Sep 17 00:00:00 2001 From: TechieDamien Date: Fri, 12 Nov 2021 12:28:05 +0000 Subject: [PATCH] Added basic callback support --- __init__.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/__init__.py b/__init__.py index 4be73a3..b608aae 100644 --- a/__init__.py +++ b/__init__.py @@ -1,6 +1,7 @@ from opsdroid.skill import Skill from opsdroid.matchers import match_rasanlu, match_parse import random +import importlib class Respond(Skill): # Constructor gets configs for the skill and applies the decorator @@ -23,6 +24,11 @@ class Respond(Skill): # from Rasa. self.fallback_response_options = self.config.get("fallback_response_options", ["Sorry, Rasa did not extract sufficient entities to fulfil your request"]) + # An optional callback used for more advanced responses. Such + # functions should simply return the final response string and + # will be given the message and extracted entities (if enabled). + self.callback_file = self.config.get("callback_file", None) + self.callback_name = self.config.get("callback_name", None) # Extract the entities from the rasanlu response @@ -69,7 +75,11 @@ class Respond(Skill): @match_parse("hgftgyhjknbvhgftyuihjkvhgftyuijkbjhgtyyuijhkjghfdtrytyihujkbvncgfxdtrytuyiuhkbjvbhcfdytryuhjkbvhcfdyrtuyiuhkbhj") async def respond(self, message): chosen_response = random.choice(self.response_options) + entities = None # In case we want callbacks without entities if self.use_entities: entities = self._get_entities(message) chosen_response = self._substitute_entities(chosen_response, entities) - await message.respond(random.choice(self.response_options)) + if self.callback_file != None and self.callback_name != None: + callback_module = importlib.import_module(self.callback_file) + chosen_response = eval("callback_module." + self.callback_name + "(message, entities)") + await message.respond(chosen_response)