-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalexa_connector.py
105 lines (90 loc) · 4.18 KB
/
alexa_connector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import logging
import json
from sanic import Blueprint, response
from sanic.request import Request
from sanic.response import HTTPResponse
#from typing import Text, Optional, List, Dict, Any
from typing import Text, Dict, Any, Optional, Callable, Awaitable, NoReturn
import rasa.utils.endpoints
from rasa.core.channels.channel import UserMessage, OutputChannel
from rasa.core.channels.channel import InputChannel
from rasa.core.channels.channel import CollectingOutputChannel
logger = logging.getLogger(__name__)
class AlexaConnector(InputChannel):
"""A custom http input channel for Alexa.
You can find more information on custom connectors in the
Rasa docs: https://rasa.com/docs/rasa/user-guide/connectors/custom-connectors/
"""
@classmethod
#def name(cls):
def name(cls) -> Text:
return "alexa_assistant"
# Sanic blueprint for handling input. The on_new_message
# function pass the received message to Rasa Core
# after you have parsed it
def blueprint(self, on_new_message):
alexa_webhook = Blueprint("alexa_webhook", __name__)
# required route: use to check if connector is live
@alexa_webhook.route("/", methods=["GET"])
async def health(request):
return response.json({"status": "ok"})
# required route: defines
@alexa_webhook.route("/webhook", methods=["POST"])
async def receive(request):
# get the json request sent by Alexa
payload = request.json
# check to see if the user is trying to launch the skill
intenttype = payload["request"]["type"]
# if the user is starting the skill, let them know it worked & what to do next
if intenttype == "LaunchRequest":
message = "Hey, willkommen!. Starte damit mir 'Hallo' zu sagen."
session = "false"
else:
# get the Alexa-detected intent
intent = payload["request"]["intent"]["name"]
# makes sure the user isn't trying to end the skill
if intent == "AMAZON.StopIntent":
session = "true"
message = "Bis zum nächsten Mal!"
else:
# get the user-provided text from the slot named "text"
text = payload["request"]["intent"]["slots"]["text"]["value"]
# initialize output channel
out = CollectingOutputChannel()
# send the user message to Rasa & wait for the
# response to be sent back
await on_new_message(UserMessage(text, out))
# extract the text from Rasa's response
responses = [m["text"] for m in out.messages]
#message = responses[0]
#give more than the first response
message=""
for res in responses:
message=message+str(res)+" \n"
#message = str(responses[0])+" "+str(responses[1])
session = "false"
# Send the response generated by Rasa back to Alexa to
# pass on to the user. For more information, refer to the
# Alexa Skills Kit Request and Response JSON Reference:
# https://developer.amazon.com/en-US/docs/alexa/custom-skills/request-and-response-json-reference.html
r = {
"version": "1.0",
"sessionAttributes": {"status": "test"},
"response": {
"outputSpeech": {
"type": "PlainText",
"text": message,
"playBehavior": "REPLACE_ENQUEUED",
},
"reprompt": {
"outputSpeech": {
"type": "PlainText",
"text": message,
"playBehavior": "REPLACE_ENQUEUED",
}
},
"shouldEndSession": session,
},
}
return response.json(r)
return alexa_webhook