Sociopath
Sociopath

Reputation: 13401

How to pass extra parameters in in handle_text in rasa?

I am creating a chatbot using RASA and Flask

I have a strange requirement where for the same question I have to configure 2 different responses based on request.

In request there is a key customer it can be 1 or 0.

Say I have a question:

what is refund policy?

If the question is asked by customer(where key value is 1)

it will return You will get refund in 3 days

else it will return You will get refund in 7 days

So I am stuck how to pass this customer value in handle_text which is generating response for my Question.

Is there a way to do this in Rasa?

My Code:

from rasa_core.interpreter import RasaNLUInterpreter
from rasa_core.utils importEndpointConfig
from flask import FLASK, request
import json

nlu_interpreter = RasaNLUInterpreter(NLU_MODEL)
action_endpoint = EndpointConfig(url=ACTION_ENDPOINT) 
agent = Agent.load(DIALOG_MODEL, interpreter=nlu_interpreter, action_endpoint=action_endpoint)

@app.route("/chatbot", methods=["POST"])
def bot():
    data = json.loads(request.data)
    msg = data["msg"]  # the question by user
    is_customer = data["customer"]

    response = agent.handle_text(msg, sender_id=data["user_id"])

    return json.dumps(response[0]["text"])

Upvotes: 1

Views: 1780

Answers (1)

lahsuk
lahsuk

Reputation: 1274

You need to configure that action in the action files and run a separate action server to handle that for you.

For example, inside the run method of your custom method, you could get the slot value of customer and then depending on that slot value, you can return required message.

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

      customer = tracker.get_slot('customer')
      if customer:
          dispatcher.utter_message("You will get refund in 3 days.")
      else:
          dispatcher.utter_message("You will get refund in 7 days.")

Doc link: https://rasa.com/docs/rasa/core/actions/#custom-actions

Hope that helps.

Upvotes: 1

Related Questions