How to fix Google Search API Integration?

Hi all,

My google search method is working correctly if not integrated in rasa action.py but when I run it as a regular python file. When integrated in rasa it only prints me the else output “Couldn’t find any results. Try again.”. I defined the function as “async”. Is there anything else I need to do?

Many thanks!

This is my script:

class GoogleSearchAction(Action):

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

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

        msg = tracker.latest_message["text"]
        print(f'Latest msg: {msg}')

                # replace API key
        params = {"q": msg,
                  "hl": "en",
                  "gl": "us",
                  "google_domain": "google.com",
                  "api_key": API_KEY
                  }


        search = GoogleSearch(params)
        results = search.get_dict()

        answer_box = None
        try: 
            if results["answer_box"]["type"] == "calculator_result" or results["answer_box"]["type"] == "currency_converter":
                answer_box = results["answer_box"]["result"]
            elif results["answer_box"]["type"] == "weather_result":
                answer_box = results["answer_box"]["temperature"] + " " + results["answer_box"]["unit"]
            elif results["answer_box"]["type"] == "finance_results":
                answer_box = results["answer_box"]["price"] + " " + results["answer_box"]["currency"]
            elif results["answer_box"]["type"] == "population_result":
                answer_box = results["answer_box"]["population"]
            elif results["answer_box"]["type"] == "dictionary_results":
                answer_box = results["answer_box"]["definitions"]
            elif results["answer_box"]["type"] == "organic_result":
                try:
                    answer_box = results["answer_box"]["list"] 
                except:
                    try:
                        answer_box = results["answer_box"]["contents"]["table"]
                    except:
                        try:
                            answer_box = results["answer_box"]["snippet"]
                        except:
                            answer_box = results["answer_box"]["answer"]

            elif results["answer_box"]["type"] == "translation_result":
                answer_box = results["answer_box"]["target"]["text"]
            elif results["answer_box"]["type"] == "directions":
                answer_box = results["answer_box"]["routes"]["summary"]
            else:
                answer_box = results["answer_box"]["answer"]
        except:
            answer_box = "Couldn't find any results. Try again. "

        print(answer_box)  
        dispatcher.utter_message(text=answer_box)