How to get value from user input and pass it into custom actions?

Hi, I tried to get some values from user input and passed it to my custom action with below code:

This is my custom action

class ActionTrendingByHashTag(Action):

    def name(self) -> Text:
        return "action_trending_by_hashtag"

    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

        hashtag = next(tracker.get_latest_entity_values("hashtag"),None)
        result = get_tiktok_trending_by_hashtag(hashtag)
        for video in result:
            dispatcher.utter_message(text=video['title'], image=video['cover'])

        return []

And here is intent:

- intent: confirm_hashtag
    examples: |
      - [cosplay](hashtag) nha
      - Hashtag là [cosplay abc](hashtag)
      - Từ khóa là [cosplay](hashtag)
      - hashtag anh muốn tìm là [cosplay](hashtag)
      - Từ khóa cần tìm kiếm là [cosplay](hashtag)
      - Anh muốn tìm về từ khóa [cosplay](hashtag)

In domain.yml, I declared slots and entities:

entities:
  - hashtag

slots:
  hashtag:
    type: text
    influence_conversation: true
    mappings:
      - type: from_entity
        entity: hashtag

And I defined a story:

- story: ask trending by hashtag
    steps:
      - intent: greet
      - action: utter_greet
      - intent: ask_for_tiktok_trending_by_hashtag
      - action: utter_confirm
      - intent: confirm_hashtag
        entities:
          - hashtag: singsong
      - slot_was_set:
          - hashtag: hashtag
      - action: action_trending_by_hashtag

But when I try to push value, action get None. Is there any wrong in my code? Thanks for your time!

@baodv1001

first check that are you able to get the user input whilst using the print statement
print(hashtag)
then next like 
get_tiktok_trending_by_hashtag({hashtag})
check
print(result)

Ref: f-strings in Python - GeeksforGeeks

You will get the clear idea. Good luck!

Thanks for your reply, when I print(hashtag), it give me None. When I type ‘cosplay’ same with examples, entity can get this value but the other word hashtag is None. Any idea about it?

Are you able to see user input whilst using print statements?

When I ran rasa shell --debug, and I got this result:

I except the value for entity hashtag is “sontung” but when I print(hashtag), the value is always null. When user input is here: The entities is not empty. I can’t understand why this happen.

Bump

@baodv1001 you not tag me and I did not saw your message sorry. What is the current status of your error?

@nik202 My bad! I try to put some example values in nlu.yml like this:

And in custom actions, when user typed which value the same with example, I got it. But with other, the value of entity is still None

@nik202 My partner tried with new intent: image And he made all files like me, the result he got is: image

I don’t know why he success but not me.

Should I create an lookup example for the entity? But the entity I want to get value is unpredictable such as hashtag, … because it has very many different values.

@baodv1001 should also work in your case too.

It hard for me to comment without seeing the code, if you need to talk to me please schedule the appointment if you are ok with it.

@nik202 I don’t change my code, now it the same with above. I don’t know where is my mistake to fix that :frowning: Do you need more code, tell me, I will show it. Thanks so much!

Bump

@baodv1001 I already shared the solution’s, suggestion and even proposed to schedule a meeting but you have not shown any interest so sorry I can not help much on this. Good Luck!

@nik202 Thanks for you support. I realize that if I want to get some value like email, phone number … I should use entity and regex to extract that entity. But with dynamic value like name, hashtag, … should use form to extract value. I will try this idea soon. Thanks again!