# Trigger intent from custom action

**URL:** <https://forum.rasa.com/t/trigger-intent-from-custom-action/51727>\
**Category:** Rasa Open Source\
**Created:** [March 4, 2022, 6:41am UTC](https://forum.rasa.com/t/trigger-intent-from-custom-action/51727 "2022-03-04T06:41:50Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Polaris000](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/polaris000/32/18422_2.png) [@Polaris000](https://forum.rasa.com/u/Polaris000)\
**Post date:** [March 4, 2022, 6:41am UTC](https://forum.rasa.com/t/trigger-intent-from-custom-action/51727/1 "2022-03-04T06:41:50Z")

</div>

I’m trying to trigger a user intent from a custom action through the `UserUttered` event. I create this event in my custom action and in the debug logs, it appears to be present in the tracker as a valid state (its the last state in the listed states).

```auto
[state 16] user intent: my_intent | previous action name: my_custom_action | slots: {'slot1': (1.0, 0.0, 0.0), 'slot2': (1.0, 1.0), 'slot3': (1.0, 0.0), 'slot4': (1.0,),...)}

```

I also noticed that `get_intent_of_latest_message` returns None after this custom action is executed.

My issue is that the bot doesn’t seem to predict the next action based on this triggered intent, that’s now present on the tracker.

My requirement is to basically trigger an intent through a custom action, and ensure that the bot continues the conversation based on this injected intent, as if the user themselves had entered it.

My code:

```auto
                ....
                data = \
                {
                    "intent": {
                        "name": "my_intent"
                    }
                }
              return [UserUttered(text="/my_intent", parse_data=data)

```

Right now, I’m resorting to setting a `FollowupAction` event to make sure the bot proceeds on the right path.

---

<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:** [March 5, 2022, 11:59am UTC](https://forum.rasa.com/t/trigger-intent-from-custom-action/51727/2 "2022-03-05T11:59:04Z")

</div>

> [@Polaris000](#):
>
> Right now, I’m resorting to setting a `FollowupAction` event to make sure the bot proceeds on the right path.

This is the recommended approach, why not use that directly?

---

<div class="post-metadata">

**Author:** ![Polaris000](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/polaris000/32/18422_2.png) [@Polaris000](https://forum.rasa.com/u/Polaris000)\
**Post date:** [March 6, 2022, 5:03am UTC](https://forum.rasa.com/t/trigger-intent-from-custom-action/51727/3 "2022-03-06T05:03:48Z")

</div>

Hi @ChrisRahme . Thanks for the prompt response, as usual! My ultimate goal is to be able to seemlessly redirect the user to another part of the conversation.

something like this:

```auto
# flow 1 going on
intent: ...
action: action_flow_1_end

# must take the user to flow 2 now after action_flow_1_end

```

#### Original approach

Currently, I make the user click on a button (basically get an intent out of them) and that helps take the user to another part of the conversation.

```auto
# flow 1 going on
intent: ...
action: action_flow_1_end

# must take the user to flow 2 now after action_flow_1_end
intent: trigger_flow_2
action: action_flow_2_start
intent:
...

```

#### What i’m currently trying

But I’m now looking at ways to avoid asking the user for this, and redirect them directly. something like this:

```auto
# flow 1 going on
intent: ...
action: action_flow_1_end

# must take the user to flow 2 now after action_flow_1_end

# add intent trigger_flow_2 to the tracker
# bot then predicts action_flow_2_start
action: action_flow_2_start

```

My original question was regarding how to accomplish this. When I said I was resorting to `FollowupAction`, it was because the bot wasn’t using the injected intent to make the next action prediction, and was pretty much always predicting `action_listen`.

#### Regarding your suggestion

My only worry with your suggestion (directly use FollowupAction) like this:

```auto
# flow 1 going on
intent: ...
action: action_flow_1_end

# must take the user to flow 2 now after action_flow_1_end

# trigger action_flow_2_start directly
action: action_flow_2_start

```

is that it may hurt policy performance, since all my stories require that there be the intent `trigger_flow_2` before `action_flow_2_start`.

#### TLDR

1. Would directly triggering the next action (not bothering with the intent in the middle at all) affect performance?
2. If I’m still looking for a way to manually inject an intent so that the bot considers it for the next prediction, is there a way to do it?

---

<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:** [March 6, 2022, 8:31am UTC](https://forum.rasa.com/t/trigger-intent-from-custom-action/51727/4 "2022-03-06T08:31:07Z")

</div>

Thanks for the detailed information!

* * *

> [@Polaris000](#):
>
> Would directly triggering the next action (not bothering with the intent in the middle at all) affect performance?

Using `FollowupAction` should not affect performance 🙂

Once you use it, just talk to your bot a bit using Rasa X and export the story to the `stories.yml` file - just to show your bot that this is normal, even though it has no choice but to execute it.

> [@Polaris000](#):
>
> it may hurt policy performance, since all my stories require that there be the intent `trigger_flow_2` before `action_flow_2_start`.

This is why I’m suggesting to add some new stories that were built after using `FollowupAction`, but again, this won’t hurt your bot’s performance since it has no choice to begin with.

* * *

> [@Polaris000](#):
>
> If I’m still looking for a way to manually inject an intent so that the bot considers it for the next prediction, is there a way to do it?

I think `UserUttered` does not execute actions, I also failed to use it that way and had to use `FollowupAction` instead (which was preferable in my case anyway).

But if you really need `UserUttered`, maybe you can find a way to execute it from your frontend application as if the user sent the message (but without displaying it of course).

---

<div class="post-metadata">

**Author:** ![Polaris000](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/polaris000/32/18422_2.png) [@Polaris000](https://forum.rasa.com/u/Polaris000)\
**Post date:** [March 6, 2022, 11:30am UTC](https://forum.rasa.com/t/trigger-intent-from-custom-action/51727/5 "2022-03-06T11:30:21Z")

</div>

Thanks a lot for the help. I’ll look into using `FollowupAction` directly, or make frontend trigger it.

Last question: If there isn’t a way (with `UserUttered` or through an API, or otherwise) to inject an intent on to the tracker that the bot uses to predict the next action (excluding work-arounds like you suggested), shouldn’t this be a feature Rasa should provide out of the box?

---

<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:** [March 6, 2022, 3:30pm UTC](https://forum.rasa.com/t/trigger-intent-from-custom-action/51727/6 "2022-03-06T15:30:39Z")

</div>

There is an API to do this, although I have never used it myself.

See [Inject an intent into a conversation](https://rasa.com/docs/rasa/pages/http-api#operation/triggerConversationIntent): `http://{server_ip}:5005/conversations/{conversation_id}/trigger_intent`

---

<div class="post-metadata">

**Author:** ![Polaris000](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/polaris000/32/18422_2.png) [@Polaris000](https://forum.rasa.com/u/Polaris000)\
**Post date:** [March 6, 2022, 7:01pm UTC](https://forum.rasa.com/t/trigger-intent-from-custom-action/51727/7 "2022-03-06T19:01:28Z")

</div>

Thanks… though I had seen this approach before the UserUttered event one, the event one seemed to be easier to implement. Will try this too.

---

<div class="post-metadata">

**Author:** ![Huascar321](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/huascar321/32/10058_2.png) [@Huascar321](https://forum.rasa.com/u/Huascar321)\
**Post date:** [April 15, 2022, 8:47pm UTC](https://forum.rasa.com/t/trigger-intent-from-custom-action/51727/8 "2022-04-15T20:47:22Z")

</div>

Hi I know it’s too late to reply to this post, but I think I figured out the reason why you couldn’t trigger an intent using UserUttered. Your problem was that you forgot to add “confidence” property in your data json. This code should work.

```auto
 ....
                data = \
                {
                    "intent": {
                        "name": "my_intent",
                        "confidence": 1.0,
                    }
                }
              return [UserUttered(text="/my_intent", parse_data=data)

```

Maybe this answer could help anyone who might have this same problem in the future

---

<div class="post-metadata">

**Author:** ![Polaris000](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/polaris000/32/18422_2.png) [@Polaris000](https://forum.rasa.com/u/Polaris000)\
**Post date:** [April 16, 2022, 3:52am UTC](https://forum.rasa.com/t/trigger-intent-from-custom-action/51727/9 "2022-04-16T03:52:43Z")

</div>

Thanks @Huascar321 . Will give it a go.

---

<div class="post-metadata">

**Author:** ![Polaris000](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/polaris000/32/18422_2.png) [@Polaris000](https://forum.rasa.com/u/Polaris000)\
**Post date:** [April 19, 2022, 11:19am UTC](https://forum.rasa.com/t/trigger-intent-from-custom-action/51727/10 "2022-04-19T11:19:49Z")

</div>

@Huascar321 I can’t get this to work… I’m using rasa 2.6 and tried something like this:

```auto
my_action: custom action that asks a yes no question

def run:
      ...
      
       data = \
            {
                "intent": {
                    "name": "/affirm",
                    "confidence": 1.0,
                }
            }

      dispatcher.utter_messagemessage("yes no question")
      return [UserUttered(text="/affirm", parse_data=data)]

```

the bot still predicts action\_listen after asking the yes no question…

---

<div class="post-metadata">

**Author:** ![itsjhonny](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/itsjhonny/32/20201_2.png) [@itsjhonny](https://forum.rasa.com/u/itsjhonny)\
**Post date:** [April 19, 2022, 1:07pm UTC](https://forum.rasa.com/t/trigger-intent-from-custom-action/51727/11 "2022-04-19T13:07:43Z")

</div>

Hi!

Can you make a little test?

put it in your rules.yml folder

```auto
- rule: Say affirm
  steps:
  - intent: affirm
  - action: utter_affirm #action that the bot will do after identifying affirm intent

```

and it in your config file

```auto
policies:
- ... # Other policies
- name: RulePolicy

```

I think need change here too

from

```auto
dispatcher.utter_messagemessage("yes no question")

```

to

```auto
dispatcher.utter_message(text="yes no question")

```

Reference:

- [Rules](https://rasa.com/docs/rasa/rules/)

---

<div class="post-metadata">

**Author:** ![Polaris000](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/polaris000/32/18422_2.png) [@Polaris000](https://forum.rasa.com/u/Polaris000)\
**Post date:** [April 19, 2022, 1:44pm UTC](https://forum.rasa.com/t/trigger-intent-from-custom-action/51727/12 "2022-04-19T13:44:05Z")

</div>

I have this flow (my\_action followed by /affirm) defined in multiple stories and I don’t want the restrictive nature of rules… I don’t think the predictive power of the bot is the issue here, it seems to just ignore the updated intent for some reason…

Could you share your code where this works?

---

<div class="post-metadata">

**Author:** ![sysang](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/sysang/32/16719_2.png) [@sysang](https://forum.rasa.com/u/sysang)\
**Post date:** [June 9, 2022, 11:03am UTC](https://forum.rasa.com/t/trigger-intent-from-custom-action/51727/13 "2022-06-09T11:03:20Z")

</div>

Consider to execute “action\_listion” to make the injected UserUttered effected.

```auto
    data = {
          "intent": {
              "name": "pseudo_user_intent",
              "confidence": 1.0,
            }
         }

    return [
        ActionExecuted("action_listen"),
        UserUttered(text="/pseudo_user_intent", parse_data=data),
      ]

```
