【问题标题】:Alexa Skill To Retrieve an ImageAlexa 检索图像的技能
【发布时间】:2021-11-08 18:20:01
【问题描述】:

现在我的技能有两个槽

  • (1) 详细信息编号
  • (2) 页码。

技能正确获取这些槽位并成功将它们重复给用户。

但是,我想要从具有这些插槽作为文件名的 S3 存储桶中提取和映像的技能(即 detailNumber-pageNumber.png)。如何实现?

class LaunchRequestHandler(AbstractRequestHandler):
"""Handler for Skill Launch."""
def can_handle(self, handler_input):
    # type: (HandlerInput) -> bool
    return ask_utils.is_request_type("LaunchRequest")(handler_input)

def handle(self, handler_input):
    # type: (HandlerInput) -> Response
    speak_output = "Welcome, to fabrication. What detail would you like to view?"
    reprompt_text = "Open detail 5 on LSF-900."

    return (
        handler_input.response_builder
            .speak(speak_output)
            .ask(reprompt_text)
            .response
    )

class OpenDetailIntentHandler(AbstractRequestHandler):
"""Handler for Open Detail Intent."""
def can_handle(self, handler_input):
    # type: (HandlerInput) -> bool
    return ask_utils.is_intent_name("OpenDetailIntent")(handler_input)

def handle(self, handler_input):
    # type: (HandlerInput) -> Response
    slots = handler_input.request_envelope.request.intent.slots
    detailNumber = slots["detailNumber"].value
    pageNumber = slots["pageNumber"].value
    speak_output = 'Thanks, I will show you detail {detailNumber} on page LSF {pageNumber}.'.format(detailNumber=detailNumber, pageNumber=pageNumber)

    return (
        handler_input.response_builder
            .speak(speak_output)
            # .ask("add a reprompt if you want to keep the session open for the user to respond")
            .response
    )

【问题讨论】:

    标签: python alexa alexa-slot


    【解决方案1】:

    如果您使用 s3 Alexa Hosted

    1. 确保您的文件在 s3 上
    2. Create a presigned URL

    示例 >

    from ask_sdk_model import ui
    from utils import create_presigned_url
    
    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        image_url = create_presigned_url("Media/" + detailNumber + "-" + pageNumber +" .png")
        
    

    utils.py(在开发者控制台的临时模板中可用)

    import logging
    import os
    import boto3
    from botocore.exceptions import ClientError
    
    
    def create_presigned_url(object_name):
        """Generate a presigned URL to share an S3 object with a capped expiration of 60 seconds
    
        :param object_name: string
        :return: Presigned URL as string. If error, returns None.
        """
        s3_client = boto3.client('s3',
                                 region_name=os.environ.get('S3_PERSISTENCE_REGION'),
                                 config=boto3.session.Config(signature_version='s3v4',s3={'addressing_style': 'path'}))
        try:
            bucket_name = os.environ.get('S3_PERSISTENCE_BUCKET')
            response = s3_client.generate_presigned_url('get_object',
                                                        Params={'Bucket': bucket_name,
                                                                'Key': object_name},
                                                        ExpiresIn=60*1)
        except ClientError as e:
            logging.error(e)
            return None
    
        # The response contains the presigned URL
        return response
    

    确保使用 Media 作为 Alexa 托管技能的默认父文件夹。

    如果您不使用 s3 Alexa Hosted

    如果如上所述 s3 不是公开的,那么您可以直接提供 s3 的路径,或者创建具有适当权限的签名 URL。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-21
      相关资源
      最近更新 更多