使用 python 调用 alexa 的定时器 api 不工作
Calling alexa's timer api with python not working
我尝试了以下代码通过 api 调用 alexa 的计时器:
timer = {
"duration": "PT15S",
"timerLabel": "Change name",
"creationBehavior": {
"displayExperience": {
"visibility": "VISIBLE"
}
},
"triggeringBehavior": {
"operation": {
"type": "ANNOUNCE",
"textToAnnounce": [
{
"locale": "en-US",
"text": "Break time"
}
]
},
"notificationConfig": {
"playAudible": True
}
}
}
class TimerHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
return ask_utils.is_intent_name("TimerHandler")(handler_input)
def handle(self, handler_input):
request_envelope = handler_input.request_envelope
permissions = request_envelope.context.system.user
if not (permissions):
return (
handler_input.response_builder
.add_directive({
type: "Connections.SendRequest",
name: "AskFor",
payload: {
"@type": "AskForPermissionsConsentRequest",
"@version": "1",
"permissionScope": "alexa::alerts:timers:skill:readwrite"
},
token: "<string>"
})
.speak("This skill needs permission to access your timer")
.ask()
.response
)
else:
return requests.post("https://api.amazonalexa.com/v1/alerts/timers", timer),
requests.status_code
但是,我收到以下错误。
Error received:
[ERROR] AttributeError: 'Response' object has no attribute 'deserialized_types'
Traceback (most recent call last):
File "/var/task/ask_sdk_core/skill_builder.py", line 112, in wrapper
return skill.serializer.serialize(response_envelope) # type:ignore
File "/var/task/ask_sdk_core/serialize.py", line 125, in serialize
return {key: self.serialize(val) for key, val in iteritems(obj_dict)}
File "/var/task/ask_sdk_core/serialize.py", line 125, in <dictcomp>
return {key: self.serialize(val) for key, val in iteritems(obj_dict)}
File "/var/task/ask_sdk_core/serialize.py", line 114, in serialize
k: k for k in obj.deserialized_types.keys()
有人能帮帮我吗:(这对我来说真的很重要,因为我不知道是什么问题,也不知道是什么错误。
您的 handle
方法 return 结果类型错误。
它必须 return ask_sdk_model.response.Response
类型的实例,并且如果条件 if not permissions
为真,它会正确执行。但如果它是假的,它 returns requests.models.Response
实例。尽管这些 类 具有相同的名称,但它们来自不同的框架并且不兼容。
也许您需要处理此 POST 请求的响应,然后 return 需要的结果作为 ask_sdk_model.response.Response
实例,如下所示:
return handler_input.response_builder.response
我尝试了以下代码通过 api 调用 alexa 的计时器:
timer = {
"duration": "PT15S",
"timerLabel": "Change name",
"creationBehavior": {
"displayExperience": {
"visibility": "VISIBLE"
}
},
"triggeringBehavior": {
"operation": {
"type": "ANNOUNCE",
"textToAnnounce": [
{
"locale": "en-US",
"text": "Break time"
}
]
},
"notificationConfig": {
"playAudible": True
}
}
}
class TimerHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
return ask_utils.is_intent_name("TimerHandler")(handler_input)
def handle(self, handler_input):
request_envelope = handler_input.request_envelope
permissions = request_envelope.context.system.user
if not (permissions):
return (
handler_input.response_builder
.add_directive({
type: "Connections.SendRequest",
name: "AskFor",
payload: {
"@type": "AskForPermissionsConsentRequest",
"@version": "1",
"permissionScope": "alexa::alerts:timers:skill:readwrite"
},
token: "<string>"
})
.speak("This skill needs permission to access your timer")
.ask()
.response
)
else:
return requests.post("https://api.amazonalexa.com/v1/alerts/timers", timer),
requests.status_code
但是,我收到以下错误。
Error received:
[ERROR] AttributeError: 'Response' object has no attribute 'deserialized_types'
Traceback (most recent call last):
File "/var/task/ask_sdk_core/skill_builder.py", line 112, in wrapper
return skill.serializer.serialize(response_envelope) # type:ignore
File "/var/task/ask_sdk_core/serialize.py", line 125, in serialize
return {key: self.serialize(val) for key, val in iteritems(obj_dict)}
File "/var/task/ask_sdk_core/serialize.py", line 125, in <dictcomp>
return {key: self.serialize(val) for key, val in iteritems(obj_dict)}
File "/var/task/ask_sdk_core/serialize.py", line 114, in serialize
k: k for k in obj.deserialized_types.keys()
有人能帮帮我吗:(这对我来说真的很重要,因为我不知道是什么问题,也不知道是什么错误。
您的 handle
方法 return 结果类型错误。
它必须 return ask_sdk_model.response.Response
类型的实例,并且如果条件 if not permissions
为真,它会正确执行。但如果它是假的,它 returns requests.models.Response
实例。尽管这些 类 具有相同的名称,但它们来自不同的框架并且不兼容。
也许您需要处理此 POST 请求的响应,然后 return 需要的结果作为 ask_sdk_model.response.Response
实例,如下所示:
return handler_input.response_builder.response