nlprasa-corerasanlua

Rasa calling external API throws NONE


I built a small chat bot using rasa. I want my bot to tell a joke by calling an external api but i'm getting None as the response.

I'm attaching the API call method here.

class ApiAction(Action):
    def name(self):
        return "action_get_jokes"

    def run(self, dispatcher, tracker, domain):
        r = requests.get('https://api.chucknorris.io/jokes/random')
        response = r.text
        json_data= json.loads(response)
        for k,v in json_data.items():
            if k == 'value':
                return [SlotSet("jokes_response",v)]
            else:
                return [SlotSet("jokes_response","404 not found")]

In my domain.yml i have slot for joke response

slots:
  jokes_response:
    type: unfeaturized
    auto_fill: false
utter_jokes:
  - text: "Here you go : {jokes_response} "
  - text: "There you go: {jokes_response} "

under actions i tried using both main and directly specifying '- action_get_jokes' but none of them worked.

actions:
   - action_get_jokes
   - __main__.ApiAction

Solution

  • I didn't use slots, but i tried your use case and succeed in a different way. and also i dont think you need to give ApiAction in domain file under actions section.

    from typing import Any, Text, Dict, List
    from rasa_sdk import Action, Tracker
    from rasa_sdk.executor import CollectingDispatcher    
    import requests
    import json
    class ApiAction(Action):
    
    def name(self) -> Text:
        return "action_get_jokes"
    
    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
        r = requests.get('https://api.chucknorris.io/jokes/random')
        response = r.text
        json_data= json.loads(response)
    
        reply = 'Here you go '
        if (json_data["value"]):
            reply = reply + json_data["value"]
        else:
            reply = reply + "404 not found"
    
        dispatcher.utter_message(reply)
    
        return []