Trigger intent from inside a custom action

Rasa Core version: 0.10.4

Python version: 3.6.5

Operating system (windows, osx, …): Ubuntu 18.04.1 LTS

Issue: I need to trigger an intent from inside a custom action.

I’m checking if the user has introduced some terms in his/her query. To do that, I’m searching in a JSON file. If I find any of the terms in the query, then I want to run an action, if not, then I need to run a different one.

Content of domain file:

intents:
  - defineTerm
  - affirm
  - default

templates:
  utter_confirm:
    - "Do you want to see a definition for \"%s\"?"
  utter_default:
    - "Terms not found"

actions:
  - utter_confirm
  - utter_default
  - actions.MainAction 
  - actions.RunAction 

Content of actions.py file:

class MainAction(Action):
    def name(self):
        return "take_action"

    def run(self, dispatcher, tracker, domain):
        terms = my_utils.get_terms(tracker.latest_message.text) # list of valid terms
        if not terms:
                return[UserUttered(text="/default", intent={"name": "default", "confidence": 1.0})]
        else:
                message = {"text": domain.templates['utter_confirm'][0]["text"] % ', '.join(terms)}
                dispatcher.utter_response(message)
        return []


class RunAction(Action):
    def name(self):
        return "run_action"

    def run(self, dispatcher, tracker, domain):
        # do some things
        return []

Content of stories.md file:

## story1
* defineTerm
        - take_action
* affirm
        - run_action

## story2a
* defineTerm
        - take_action
* default
        - utter_default

## story2b
* defineTerm
        - take_action

I’ve tried writing either “story2a” or “story2b”, but it doesn’t work properly. It would be good too if I could set it to the end of a story, i.e. make that the bot see the next text introduced by the user as the beginning of a story. Any suggestion?

1 Like

What do you mean by trigger an intent? Could you post an example conversation of what you’re trying to achieve?

Fixed! I had to leave only the story2a in my the stories.md file. I don´t know why it was not working before.

I meant that when the code goes into the return[UserUttered(text="/default", intent={"name": "default", "confidence": 1.0})] line (MainAction class), the story had to follow the story2a path but it was running the story1 instead, i.e. it was waiting an affirm intent (as expected in story1) instead of print the utter_default (as expected in story2a).

An example of what I had:

  • User: What is X? # Term X is not in my JSON file
  • Bot: Terms not found # it has gone into the if not terms clause in the MainAction class as expected
  • User: What is Y?
  • Bot: # it ran the RunAction class code instead going to the MainAction class

But now it is fixed. I don´t know why it wasn´t working before but it´s working now. Example of what I have now:

  • User: What is X? # Term X is not in my JSON file
  • Bot: Terms not found # it has gone into the if not terms clause in the MainAction class as expected
  • User: What is Y? # Term Y is not in my JSON file either
  • Bot: Terms not found # it has gone into the if not terms clause in the MainAction class as expected
  • User: What is Z? # Term Z is in my JSON file
  • Bot: Do you want to see a definition for “Z”?
  • User: Yes
  • Bot: # it runs the RunAction class as expected now

ty!

Now, it isn´t working again :sob: Any idea why?

I’ve fixed it forcing the bot to restart the story replacing the line: return[UserUttered(text="/default", intent={"name": "default", "confidence": 1.0})] by: return [Restarted()]

It´s not very elegant, but it works though.

1 Like

Hi @akelad , still not sure how do you achieve this in the new rasa

have you looked at the mapping policy?

do you found the solution

Hi @KheireddineAzzez ,

I think you can solve it by using rule policy.

You can write your stories in rule to trigger the action.

I want to use it to close form validation if the user fills in something related to a story

Hi @KheireddineAzzez,

Could you please try with form action to trigger the intent? You can map intent with slot defined or declared for form.

For example,

Sample_form: Slot_name: - Intent: intent_name