matrix-rasa

A rasa bot for matrix based on the data from matrix.org
git clone git://archive.git.mtrnord.blog/MTRNord/matrix-rasa.git
Log | Files | Refs | LICENSE

actions.py (2014B)


      1 from rasa_sdk.knowledge_base.storage import InMemoryKnowledgeBase
      2 from rasa_sdk.knowledge_base.actions import ActionQueryKnowledgeBase
      3 from rasa_sdk.executor import CollectingDispatcher
      4 from typing import Text
      5 
      6 
      7 class ActionMyKB(ActionQueryKnowledgeBase):
      8     def __init__(self):
      9         # load knowledge base with data from the given file
     10         knowledge_base = InMemoryKnowledgeBase("knowledge_base_data.json")
     11 
     12         # overwrite the representation function of the hotel object
     13         # by default the representation function is just the name of the object
     14         knowledge_base.set_representation_function_of_object(
     15             "matrix_room", lambda obj: "https://matrix.to/#/" + obj["matrix_room"]
     16         )
     17         knowledge_base.set_key_attribute_of_object("client", "name")
     18         knowledge_base.set_key_attribute_of_object("provider", "name")
     19 
     20         super().__init__(knowledge_base)
     21 
     22     def utter_attribute_value(
     23         self,
     24         dispatcher: CollectingDispatcher,
     25         object_name: Text,
     26         attribute_name: Text,
     27         attribute_value: Text,
     28     ):
     29         """
     30         Utters a response that informs the user about the attribute value of the
     31         attribute of interest.
     32 
     33         Args:
     34             dispatcher: the dispatcher
     35             object_name: the name of the object
     36             attribute_name: the name of the attribute
     37             attribute_value: the value of the attribute
     38         """
     39         # TODO: Make this work with the true/false lists of features and make it pretty
     40         if attribute_value:
     41             dispatcher.utter_message(
     42                 text=(
     43                     f"'{object_name}' has the value '{attribute_value}' "
     44                     f"for attribute '{attribute_name}'."
     45                 )
     46             )
     47         else:
     48             dispatcher.utter_message(
     49                 text=(
     50                     f"Did not find a valid value for attribute '{attribute_name}' "
     51                     f"for object '{object_name}'."
     52                 )
     53             )