Added basic callback support

pull/1/head
TechieDamien 2021-11-12 12:28:05 +00:00
parent 98a4f5349d
commit 2c0f2530a7
1 changed files with 11 additions and 1 deletions

View File

@ -1,6 +1,7 @@
from opsdroid.skill import Skill from opsdroid.skill import Skill
from opsdroid.matchers import match_rasanlu, match_parse from opsdroid.matchers import match_rasanlu, match_parse
import random import random
import importlib
class Respond(Skill): class Respond(Skill):
# Constructor gets configs for the skill and applies the decorator # Constructor gets configs for the skill and applies the decorator
@ -23,6 +24,11 @@ class Respond(Skill):
# from Rasa. # from Rasa.
self.fallback_response_options = self.config.get("fallback_response_options", self.fallback_response_options = self.config.get("fallback_response_options",
["Sorry, Rasa did not extract sufficient entities to fulfil your request"]) ["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 # Extract the entities from the rasanlu response
@ -69,7 +75,11 @@ class Respond(Skill):
@match_parse("hgftgyhjknbvhgftyuihjkvhgftyuijkbjhgtyyuijhkjghfdtrytyihujkbvncgfxdtrytuyiuhkbjvbhcfdytryuhjkbvhcfdyrtuyiuhkbhj") @match_parse("hgftgyhjknbvhgftyuihjkvhgftyuijkbjhgtyyuijhkjghfdtrytyihujkbvncgfxdtrytuyiuhkbjvbhcfdytryuhjkbvhcfdyrtuyiuhkbhj")
async def respond(self, message): async def respond(self, message):
chosen_response = random.choice(self.response_options) chosen_response = random.choice(self.response_options)
entities = None # In case we want callbacks without entities
if self.use_entities: if self.use_entities:
entities = self._get_entities(message) entities = self._get_entities(message)
chosen_response = self._substitute_entities(chosen_response, entities) 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)