Sending message to specific channel using dispatcher.utter in actions.py

How can I send message to specific channels lets say,

I have to send

“Hello, this is alfred” from socket.io channel

and

“Hello this is John” from facebook channel,

how can i do it so, from actions.py using dispatcher.utter

Solution 1

In the domain:

responses:
  utter_greet:
  - text: "Hello, this is Alfred"
    channel: "slack"
  - text: "Hello, this is John"
    channel: "facebook"
  - text: "Hello, this is Chris"

Reference: Channel-Specific Response Variations


Solution 2

In the actions:

class ActionGreet(Action):
    def name(self):
        return "action_greet"

    async def run(self, dispatcher, tracker, domain):
        channel = tracker.get_latest_input_channel().lower()
        name = "Chris"
        
        if channel == "slack":
            name = "Alfred"
        elif channel == "facebook":
            name = "John"

        dispatcher.utter_message(text = f"Hello, this is {name}")

Reference: Tracker.get_latest_input_channel


Note

Make sure the value of the channel key matches the value returned by the name() method of your input channel. If you are using a built-in channel, this value will also match the channel name used in your credentials.yml file.

2 Likes

thank you so much! can u tell me how we can block/disable input when we wait for bots reply in rasa-webchat

1 Like

Glad to be of help :slight_smile:

I don’t know about WebChat, please create a new topic for that