0

I'm creating a connector for Rasa X (rasa 1.0) to work with Google Assistant as a front-end. Before 1.0 came out, this tutorial: https://medium.com/rasa-blog/going-beyond-hey-google-building-a-rasa-powered-google-assistant-5ff916409a25 worked really well. However, when I tried to run Rasa X on the same structure there were incompatibilities between my old Flask-based connector and the Rasa agent starting the project.

In the new version, rasa.core.agent.handle_channels([input_channel], http_port=xxxx) uses Sanic instead, which seems to be incompatible with my old method.

I have tried to convert the old Flask connector to a Sanic(never used it before) one and I used Postman to check the health route, which works. I also receive the payload from Assistant. However, when I forward this to the Rasa agent, I get nothing in return.

  • This is the new Sanic connector:
class GoogleAssistant(InputChannel):

    @classmethod
    def name(cls):
        return "google_assistant"

    def blueprint(self, on_new_message):
        # this is a Sanic Blueprint
        google_webhook = sBlueprint("google_webhook")

        @google_webhook.route("/", methods=['GET'])
        def health(request):
            return response.json({"status": "ok"})

        @google_webhook.route("/webhook", methods=["POST"])
        def receive(request):
            #payload = json.loads(request)
            payload = request.json
            sender_id = payload["user"]['userId']
            intent = payload['inputs'][0]['intent']             
            text = payload['inputs'][0]['rawInputs'][0]['query']

            try:
                if intent == "actions.intent.MAIN":
                    message = "<speak>Hello! <break time=\"1\"/> Welcome to the Rasa-powered Google Assistant skill. You can start by saying hi."
                else:
                    # Here seems to be the issue. responses is always empty
                    out = CollectingOutputChannel()
                    on_new_message(UserMessage(text,out,sender_id))
                    responses = [m["text"] for m in out.messages]
                    message = responses[0]
            except LookupError as e:
                message = "RASA_NO_REPLY"
                print(e)

            r = json.dumps("some-response-json")
            return HTTPResponse(body=r, content_type="application/json")

        return google_webhook
  • And this is the script starting the project:
action_endpoint = EndpointConfig(url="http://localhost:5055/webhook")
nlu_interpreter = RasaNLUInterpreter('path-to-model')
agent = Agent.load('path-to-model-2', interpreter = nlu_interpreter, action_endpoint=action_endpoint)

input_channel = GoogleAssistant()
agent.handle_channels([input_channel], http_port=5010)

I expect the output to be the text selected by the Rasa agent as a reply "This is the reply", but I get nothing (the list is empty).

EDIT

I defined def receive(request): as async def receive(request): and changed on_new_message(UserMessage(text,out,sender_id)) to await on_new_message(UserMessage(text,out,sender_id)). Furthermore, the script starting the project is now:

loop = asyncio.get_event_loop()  

action_endpoint = EndpointConfig(url="http://localhost:5055/webhook")
nlu_interpreter = RasaNLUInterpreter('path')
agent = Agent.load('path2', interpreter = nlu_interpreter, action_endpoint=action_endpoint)
input_channel = GoogleAssistant()

loop.run_until_complete(agent.handle_channels([input_channel], http_port=5010))
loop.close()

Unfortunately it didn't change anything, still not getting any reply from Rasa on the output channel.

davidism
  • 121,510
  • 29
  • 395
  • 339
John Szatmari
  • 375
  • 4
  • 11

1 Answers1

0

Due to the introduction of the async Sanic server, on_new_message has to be awaited. Try changing your function definition to

async def receive(request):

and the else to

out = CollectingOutputChannel()
await on_new_message(UserMessage(query, output_channel, user_id))
m = [m['text'] for m in out.messages]
message = responses[0]