# Custom Utterances

**URL:** <https://forum.rasa.com/t/custom-utterances/45841>\
**Category:** Rasa Open Source\
**Created:** [July 16, 2021, 10:52pm UTC](https://forum.rasa.com/t/custom-utterances/45841 "2021-07-16T22:52:21Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![dragon18456](https://avatars.discourse-cdn.com/v4/letter/d/53a042/32.png) [@dragon18456](https://forum.rasa.com/u/dragon18456)\
**Post date:** [July 16, 2021, 10:52pm UTC](https://forum.rasa.com/t/custom-utterances/45841/1 "2021-07-16T22:52:21Z")

</div>

Lets say, I have a list of responses that I want the bot to respond with and I want it to pick one randomly. However, there is something that I want to tell the user before the random response. For example:

1. I’ve added a cheeseburger to your order, will that be all?
2. I’ve added a cheeseburger to your order, anything else?
3. I’ve added a cheeseburger to your order, is there anything else you would like to add to your order today? etc.

I generate “I’ve added a cheeseburger to your order” in a custom action, how can I utter this and a random response in 1 utterance. The reason why it needs to be 1 utterance is because it has to interface with a tts/asr audio system, so I do not want multiple utterances by a machine at once.

---

<div class="post-metadata">

**Author:** ![nik202](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/nik202/32/16098_2.png) [@nik202](https://forum.rasa.com/u/nik202)\
**Post date:** [July 16, 2021, 11:56pm UTC](https://forum.rasa.com/t/custom-utterances/45841/2 "2021-07-16T23:56:11Z")

</div>

@dragon18456

In domain.yml

```
responses:
utter_random_response:
-text: will that be all?
-text: anything else?
-text: is there anything else you would like to add to your order today? etc.

```

In will pick random response every time.

In acttion.py

`dispatcher.utter_message(text="I’ve added a cheeseburger to your order;", "template=" utter_random_response")`

Text is the fixed response and utter\_random\_response will change every time.

You need to create the story or rule as per your scenario.

Good Luck and your post make me hungry 🙂

---

<div class="post-metadata">

**Author:** ![dragon18456](https://avatars.discourse-cdn.com/v4/letter/d/53a042/32.png) [@dragon18456](https://forum.rasa.com/u/dragon18456)\
**Post date:** [July 16, 2021, 11:58pm UTC](https://forum.rasa.com/t/custom-utterances/45841/3 "2021-07-16T23:58:52Z")

</div>

No, the part about “I’ve added cheeseburger to your order” is part of the generated stuff, I wanted the “will that be all?”, “anything else?” and “is there anything else etc.” to be in utter\_random\_response. So I can add that field “text” = “I’ve added a cheeseburger to your order” to the utter\_message and it just works?

---

<div class="post-metadata">

**Author:** ![nik202](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/nik202/32/16098_2.png) [@nik202](https://forum.rasa.com/u/nik202)\
**Post date:** [July 17, 2021, 12:00am UTC](https://forum.rasa.com/t/custom-utterances/45841/4 "2021-07-17T00:00:30Z")

</div>

@dragon18456 replace it and try. concept is same. what ever you want to add in random it will show random pick and text one will be fixed.

---

<div class="post-metadata">

**Author:** ![dragon18456](https://avatars.discourse-cdn.com/v4/letter/d/53a042/32.png) [@dragon18456](https://forum.rasa.com/u/dragon18456)\
**Post date:** [July 17, 2021, 12:03am UTC](https://forum.rasa.com/t/custom-utterances/45841/5 "2021-07-17T00:03:12Z")

</div>

Wait, but this causes 2 separate responses from the bot. I need these 2 to be 1 utterance instead of 2.

---

<div class="post-metadata">

**Author:** ![nik202](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/nik202/32/16098_2.png) [@nik202](https://forum.rasa.com/u/nik202)\
**Post date:** [July 17, 2021, 12:04am UTC](https://forum.rasa.com/t/custom-utterances/45841/6 "2021-07-17T00:04:27Z")

</div>

@dragon18456 I have update that’s what you want right? Its a voice does it matter?@dragon18456 Try, if not work then will provide you other possible solution.

---

<div class="post-metadata">

**Author:** ![dragon18456](https://avatars.discourse-cdn.com/v4/letter/d/53a042/32.png) [@dragon18456](https://forum.rasa.com/u/dragon18456)\
**Post date:** [July 17, 2021, 12:06am UTC](https://forum.rasa.com/t/custom-utterances/45841/7 "2021-07-17T00:06:07Z")

</div>

No, I need the bot to just respond with:

“I’ve added a cheeseburger to your order, will that be all”

Not

“I’ve added a cheeseburger to your order”

Newline

“Will that be all”

My system will be asr/audio based, so I can’t have 2 separate responses

---

<div class="post-metadata">

**Author:** ![ChrisRahme](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/chrisrahme/32/15572_2.png) [@ChrisRahme](https://forum.rasa.com/u/ChrisRahme)\
**Post date:** [July 17, 2021, 6:09am UTC](https://forum.rasa.com/t/custom-utterances/45841/8 "2021-07-17T06:09:52Z")

</div>

If you want to do it in the domain:

```auto
responses:
  utter_cheeseburger :
  - text: I’ve added a cheeseburger to your order, will that be all?
  - text: I’ve added a cheeseburger to your order, anything else?
  - text: I’ve added a cheeseburger to your order, is there anything else you would like to add to your order today?

```

Or, if you want to do it in an action:

```auto
import random

class ActionCheeseburger(Action):
    def name(self):
        return 'action_cheeseburger'
    
    def run(self, dispatcher, tracker, domain):
        base = "I’ve added a cheeseburger to your order, "
        questions = ["will that be all?", "anything else?", "is there anything else you would like to add to your order today?"]
        full_text = base + random.choice(questions)

        dispatcher.utter_message(text = full_text)
        return []

```

---

<div class="post-metadata">

**Author:** ![nik202](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/nik202/32/16098_2.png) [@nik202](https://forum.rasa.com/u/nik202)\
**Post date:** [July 17, 2021, 5:34pm UTC](https://forum.rasa.com/t/custom-utterances/45841/9 "2021-07-17T17:34:47Z")

</div>

@ChrisRahme Nice! can we do this with button also or if we need to select multiple buttons?

---

<div class="post-metadata">

**Author:** ![dragon18456](https://avatars.discourse-cdn.com/v4/letter/d/53a042/32.png) [@dragon18456](https://forum.rasa.com/u/dragon18456)\
**Post date:** [July 17, 2021, 7:13pm UTC](https://forum.rasa.com/t/custom-utterances/45841/10 "2021-07-17T19:13:42Z")

</div>

Thanks, I thought that this was how to do it, but it is a bit disappointing that Rasa doesn’t have this built in. I would have thought there would have been a way for Rasa to do this automatically, so that my random responses can be in the domain.yml rather than a python list

---

<div class="post-metadata">

**Author:** ![ChrisRahme](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/chrisrahme/32/15572_2.png) [@ChrisRahme](https://forum.rasa.com/u/ChrisRahme)\
**Post date:** [July 17, 2021, 8:40pm UTC](https://forum.rasa.com/t/custom-utterances/45841/11 "2021-07-17T20:40:16Z")

</div>

Yes you can!

I used variable buttons in my multilingual chatbot to output the buttons according to the language 🙂

---

<div class="post-metadata">

**Author:** ![nik202](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/nik202/32/16098_2.png) [@nik202](https://forum.rasa.com/u/nik202)\
**Post date:** [July 17, 2021, 8:41pm UTC](https://forum.rasa.com/t/custom-utterances/45841/12 "2021-07-17T20:41:10Z")

</div>

@ChrisRahme can you shared the snippet or some thread for the reference.

---

<div class="post-metadata">

**Author:** ![ChrisRahme](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/chrisrahme/32/15572_2.png) [@ChrisRahme](https://forum.rasa.com/u/ChrisRahme)\
**Post date:** [July 17, 2021, 8:41pm UTC](https://forum.rasa.com/t/custom-utterances/45841/13 "2021-07-17T20:41:40Z")

</div>

But you can do it in the domain as I’ve shown in the first example. You’ll have to repeat the same part though of course, but it’s all doable in the domain.

---

<div class="post-metadata">

**Author:** ![ChrisRahme](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/chrisrahme/32/15572_2.png) [@ChrisRahme](https://forum.rasa.com/u/ChrisRahme)\
**Post date:** [July 17, 2021, 8:49pm UTC](https://forum.rasa.com/t/custom-utterances/45841/14 "2021-07-17T20:49:55Z")

</div>

Sure @nik202! Here’s an example:

```auto
class ActionUtterServiceTypes(Action):
    def name(self):
        return 'action_utter_service_types'
    
    def run(self, dispatcher, tracker, domain):
        announce(self, tracker)
        text = get_text_from_lang(
            tracker,
            [['How can I help you today?', 'So I can get you to the right place, tell me what service you’d like help with.', 'How can I help?'],
            ['Comment puis-je vous aider?', 'Pour que je puisse vous guider, dites-moi pour quel service vous aimeriez obtenir de l’aide.', 'Comment puis-je aider?'],
            ['حتى أتمكن من إيصالك إلى المكان الصحيح ، أخبرني بالخدمة التي ترغب في المساعدة فيها.', 'كيف يمكنني أن أقدم المساعدة؟'],
            ['Այսպիսով, ես կարող եմ ձեզ ճիշտ տեղ հասցնել, ասեք ինձ, թե որ ծառայության հետ կցանկանայիք օգնել', 'Ինչպե՞ս կարող եմ օգնել:']]
        )
        
        buttons = get_buttons_from_lang(
            tracker,
            [['Wireless', 'Internet', 'DSL Internet', 'CableVision TV'],
            ['Sans Fil', 'Internet', 'Internet DSL', 'CableVision TV'],
            ['لاسلكي','إنترنت','DSL إنترنت','تلفزيون الكابل'],
            ['Անլար', 'Ինտերնետ', 'DSL ինտերնետ', 'CableVision TV']],
            [
                '/inform_service_type{"service_type": "wireless"}',
                '/inform_service_type{"service_type": "internet"}',
                '/inform_service_type{"service_type": "dsl"}',
                '/inform_service_type{"service_type": "cablevision"}'
            ])
        
        print('\nBOT:', text, buttons)
        dispatcher.utter_message(text = text, buttons = buttons)
        return []

```

Basically the `get_text_from_lang` and `get_buttons_from_lang` functions access the `language` slot and return the text or button depending on its value.

`get_text_from_lang` takes in a list of lists, each list represents a language, and each element inside it will be chosen randomly, like when writing multiple values for a response in the domain.

`get_buttons_from_lang` takes in a list of lists and a second list. The first list of lists are the button titles in the different languages, and the second list is a list of payloads. Meaning, in the example above, ‘Wireless’ and ‘Sans Fil’ buttons both have the payload `/inform_service_type{"service_type": "wireless"}`. ‘Wireless’ will appear if the bot is speaking English, and ‘Sans Fil’ will appear if it speaks French.

And this is the whole chatbot if you’d like to see more of the code: ChrisRahme/fyp-chatbot.

---

<div class="post-metadata">

**Author:** ![nik202](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/nik202/32/16098_2.png) [@nik202](https://forum.rasa.com/u/nik202)\
**Post date:** [July 17, 2021, 8:52pm UTC](https://forum.rasa.com/t/custom-utterances/45841/15 "2021-07-17T20:52:15Z")

</div>

@ChrisRahme Surely, I will try and recommend this to others on the forum if somebody required this idea.Many thanks. Regards Nik.
