0

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
Sai Prasanth
  • 73
  • 1
  • 13
  • You just need to specify `action_get_jokes`. The class name is wrong. Also, can you verify if the value is being fetched in the `ApiAction` class in the first place? – msamogh Oct 08 '19 at 12:26
  • I tried with action_get_jokes it returns NONE. Values are not being fetched. i feel the class name is not being triggered. Can you help me with that ? – Sai Prasanth Oct 09 '19 at 12:19

1 Answers1

1

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 []
Rajitha Fernando
  • 1,655
  • 15
  • 14