reformatted dispatcher to have only one methods
@@ -23,6 +23,32 @@ class CollectingDispatcher:
self.messages = []
+ def utter_message(
+ self,
+ image: Optional[Text] = None,
+ json_message: Dict[Text, Any] = None,
+ template: Optional[Text] = None,
+ attachment: Optional[Text] = None,
+ text: Optional[Text] = None,
+ buttons: Optional[List[Dict[Text, Any]]] = None,
+ *elements: Dict[Text, Any],
+ **kwargs: Any,
+ ) -> None:
+ """"Send a text to the output channel"""
+
+ message = {
+ "text": text,
+ "buttons": buttons,
+ "elements": elements,
+ "custom": json_message,
+ "template": template,
+ "image": image,
+ "attachment": attachment,
+ }
+ message.update(kwargs)
+
+ self.messages.append(message)
+
# deprecated
def utter_custom_message(self, *elements: Dict[Text, Any], **kwargs: Any) -> None:
warnings.warn(
@@ -36,36 +62,19 @@ class CollectingDispatcher:
def utter_elements(self, *elements: Dict[Text, Any], **kwargs: Any) -> None:
"""Sends a message with custom elements to the output channel."""
- message = {"text": None, "elements": elements}
- message.update(kwargs)
-
- self.messages.append(message)
-
- def utter_message(self, text: Text, **kwargs: Any) -> None:
- """"Send a text to the output channel"""
-
- message = {"text": text}
- message.update(kwargs)
-
- self.messages.append(message)
+ self.utter_message(*elements)
def utter_button_message(
self, text: Text, buttons: List[Dict[Text, Any]], **kwargs: Any
) -> None:
"""Sends a message with buttons to the output channel."""
- message = {"text": text, "buttons": buttons}
- message.update(kwargs)
-
- self.messages.append(message)
+ self.utter_message(text=text, buttons=buttons)
def utter_attachment(self, attachment: Text, **kwargs: Any) -> None:
"""Send a message to the client with attachments."""
- message = {"text": None, "attachment": attachment}
- message.update(kwargs)
-
- self.messages.append(message)
+ self.utter_message(attachment=attachment)
# noinspection PyUnusedLocal
def utter_button_template(
@@ -78,10 +87,7 @@ class CollectingDispatcher:
) -> None:
"""Sends a message template with buttons to the output channel."""
- message = {"template": template, "buttons": buttons}
- message.update(kwargs)
-
- self.messages.append(message)
+ self.utter_message(template=template, buttons=buttons)
# noinspection PyUnusedLocal
def utter_template(
@@ -89,26 +95,17 @@ class CollectingDispatcher:
) -> None:
""""Send a message to the client based on a template."""
- message = {"template": template}
- message.update(kwargs)
-
- self.messages.append(message)
+ self.utter_message(template=template)
def utter_custom_json(self, json_message: Dict[Text, Any], **kwargs: Any) -> None:
"""Sends custom json to the output channel."""
- json_message = {"custom": json_message}
- json_message.update(kwargs)
-
- self.messages.append(json_message)
+ self.utter_message(json_message=json_message)
def utter_image_url(self, image: Text, **kwargs: Any) -> None:
"""Sends url of image attachment to the output channel."""
- message = {"image": image}
- message.update(kwargs)
-
- self.messages.append(message)
+ self.utter_message(image=image)
class ActionExecutor:
