Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何为Lex bot运行时的PostText API调用获取授权用户_Python 3.x_Rest_Api_Amazon Web Services_Amazon Lex - Fatal编程技术网

Python 3.x 如何为Lex bot运行时的PostText API调用获取授权用户

Python 3.x 如何为Lex bot运行时的PostText API调用获取授权用户,python-3.x,rest,api,amazon-web-services,amazon-lex,Python 3.x,Rest,Api,Amazon Web Services,Amazon Lex,抱歉发了这么长的邮件。我正试图用lambda函数调用带有PostText运行时API的Lex bot。但是,当我测试这个调用时,它会返回用户ID没有被授权使用它。这是我收到的错误消息: Response: { "errorMessage": "An error occurred (AccessDeniedException) when calling the PostText operation: User: arn:aws:sts::981709171824:assumed-rol

抱歉发了这么长的邮件。我正试图用lambda函数调用带有PostText运行时API的Lex bot。但是,当我测试这个调用时,它会返回用户ID没有被授权使用它。这是我收到的错误消息:

    Response:
{
  "errorMessage": "An error occurred (AccessDeniedException) when calling the PostText operation: User: arn:aws:sts::981709171824:assumed-role/lambda_basic_execution/OrchestratorAPIApp is not authorized to perform: lex:PostText on resource: arn:aws:lex:us-east-1:981709171824:bot:SupportBot_BookCab:SupportBot_BookCab",
  "errorType": "ClientError",
  "stackTrace": [
    [
      "/var/task/lambda_function.py",
      18,
      "lambda_handler",
      "inputText= userInput"
    ],
    [
      "/var/runtime/botocore/client.py",
      314,
      "_api_call",
      "return self._make_api_call(operation_name, kwargs)"
    ],
    [
      "/var/runtime/botocore/client.py",
      612,
      "_make_api_call",
      "raise error_class(parsed_response, operation_name)"
    ]
  ]
}

Request ID:
"677f1820-6ed2-11e8-b891-33ab1951c65f"

Function Logs:
START RequestId: 677f1820-6ed2-11e8-b891-33ab1951c65f Version: $LATEST
An error occurred (AccessDeniedException) when calling the PostText operation: User: arn:aws:sts::981709171824:assumed-role/lambda_basic_execution/OrchestratorAPIApp is not authorized to perform: lex:PostText on resource: arn:aws:lex:us-east-1:981709171824:bot:SupportBot_BookCab:SupportBot_BookCab: ClientError
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 18, in lambda_handler
    inputText= userInput
  File "/var/runtime/botocore/client.py", line 314, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/var/runtime/botocore/client.py", line 612, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (AccessDeniedException) when calling the PostText operation: User: arn:aws:sts::981709171824:assumed-role/lambda_basic_execution/OrchestratorAPIApp is not authorized to perform: lex:PostText on resource: arn:aws:lex:us-east-1:981709171824:bot:SupportBot_BookCab:SupportBot_BookCab

END RequestId: 677f1820-6ed2-11e8-b891-33ab1951c65f
REPORT RequestId: 677f1820-6ed2-11e8-b891-33ab1951c65f  Duration: 325.25 ms Billed Duration: 400 ms     Memory Size: 128 MB Max Memory Used: 31 MB  
这是我调用API的代码:

    import boto3

def lambda_handler(event, context):
    responderName = event["DestinationBot"]
    userId = event["RecipientID"]
    userInput = event["message"]["text"]

    client = boto3.client('lex-runtime')

    response = client.post_text(
        botName=responderName,
        botAlias=responderName,
        userId=userId,
        sessionAttributes={
        },
        requestAttributes={
        },
        inputText= userInput
    )
这是我的示例测试输入:

{
  "DestinationBot": "SupportBot_BookCab",
  "RecipientID": "12345",
  "message": {
      "text": "book me a cab"
  }
}

PostText
userID
是您在用户和Lex之间保持来回对话的方式。它可以是您可以在用户的传入请求中识别用户的任何内容,这些内容对用户来说是一致的和唯一的,至少对于该会话是如此

用户ID
客户端应用程序用户的ID。AmazonLex使用它来识别用户与您的机器人的对话。在运行时,每个请求必须包含userID字段。

长度约束:最小长度为2。最大长度为100。
图案:[0-9a-zA-Z.U7;:-]+

因此,如果用户正在使用Facebook messenger,他们将拥有一个与他们的消息一起传递的Facebook ID,您可以将该ID用作他们的
用户ID

如果用户正在使用Twilio SMS,他们的信息中会传递一个电话号码,您可以将其用作其
用户ID

您的代码当前正在获取
事件[“RecipientID”]
,并将其用作
用户ID
。但是来自传入消息的
接收者ID
是您自己,即传入消息的接收者

你的错误告诉你

。。。用户:arn:aws:sts::xxxxxxxxx:假定角色/lambda\u基本执行/OrchestratorAPIApp

因此,
userID
=
事件[“RecipientID”]
=
'arn:aws:sts::XXXXXXXX:acked role/lambda\u basic\u execution/OrchestratorAPIApp'

您肯定不希望使用收件人ID

相反,您希望发件人的ID是
用户ID
。比如:

userId=event[“SenderID”]

这可能不是您使用的确切代码,它只是一个示例。您应该能够查看传入的请求,并在其中找到一些东西作为适当的用户ID,正如我在上面用Facebook和Twilio解释的那样