Lambda函数无法连接到Lex机器人。

huangapple go评论67阅读模式
英文:

Lambda function can't connect to Lex bot

问题

I want to invoke my Lex bot from my lambda function, by passing an input (utterance) to my bot.

Here is my Python code:

import boto3
import json

def lambda_handler(event, context):
    print(event)
    # Create a new Amazon Lex runtime client
    client = boto3.client('lex-runtime')

    # Send the user's input to the bot
    response = client.post_text(
        botName='BotName',
        botAlias='BotAlias',
        userId='UserID',
        inputText=event['input']
    )

    # Return the bot's response to the caller
    return {
        'output': response['message']
    }

I get the following error:

"errorMessage": "Could not connect to the endpoint URL: "https://runtime.lex.af-south-1.amazonaws.com/bot/BotName/alias/BotAlias/user/UserID/text"",
"errorType": "EndpointConnectionError",

My lambda function has the following permissions:
AmazonLexFullAccess
AmazonLexRunBotsOnly

My Lex bot has the basic Amazon Lex permissions.

英文:

I want to invoke my Lex bot from my lambda function, by passing an input (utterance) to my bot.

Here is my Python code:

import boto3
import json

def lambda_handler(event, context):
    print(event)
    # Create a new Amazon Lex runtime client
    client = boto3.client('lex-runtime')

    # Send the user's input to the bot
    response = client.post_text(
        botName='BotName',
        botAlias='BotAlias',
        userId='UserID',
        inputText=event['input']
    )

    # Return the bot's response to the caller
    return {
        'output': response['message']
    }

I get the following error:

"errorMessage": "Could not connect to the endpoint URL: "https://runtime.lex.af-south-1.amazonaws.com/bot/BotName/alias/BotAlias/user/UserID/text"",
"errorType": "EndpointConnectionError",

My lambda function has the following permissions:
AmazonLexFullAccess
AmazonLexRunBotsOnly

My Lex bot has the basic Amazon Lex permissions.

答案1

得分: 1

检查 boto3 客户端初始化中的 botName、botAlias 或 region,或者可能是 IAM 权限。很明显这是 EndpointConnectionError。因此,请检查并查看您的 URL 中是否存在任何缺失的字符串值。

英文:

check botName, botAlias, or region in the boto3 client initialization or possible IAM permissions. It is clear that it is EndpointConnectionError. So check and see if there is any missing string values in your URL as well.

答案2

得分: 1

以下是翻译好的部分:

"所以事实证明,我之前使用了 'lex-runtime' 而不是 'lexv2-runtime'。参数也需要更改。

修正后的代码如下:

import boto3
import json

def lambda_handler(event, context):
    # 创建一个新的 Amazon Lex 运行时客户端
    client = boto3.client('lexv2-runtime')
    
    # 将用户的输入发送给机器人
    response = client.recognize_text(
        botId='<botId>',
        botAliasId='<botAliasId>',
        localeId='<localeId>',
        sessionId='<sessionId>',
        text=event['input']
    )
    
    # 将机器人的响应返回给调用者
    return {
        'output': response['messages']
    }

希望这有帮助!

英文:

So it turns out that I was using 'lex-runtime' instead of 'lexv2-runtime'. The parameters also have to change.

The corrected code is as follows:

import boto3
import json

def lambda_handler(event, context):
    # Create a new Amazon Lex runtime client
    client = boto3.client(&#39;lexv2-runtime&#39;)
    
    # Send the user&#39;s input to the bot
    response = client.recognize_text(
        botId=&#39;&lt;botId&gt;&#39;,
        botAliasId=&#39;&lt;botAliasId&gt;&#39;,
        localeId=&#39;&lt;localeId&gt;&#39;,
        sessionId=&#39;&lt;sessionId&gt;&#39;,
        text=&#39;event[&#39;input&#39;]
    )
    
    # Return the bot&#39;s response to the caller
    return {
        &#39;output&#39;: response[&#39;messages&#39;]
    }

huangapple
  • 本文由 发表于 2023年5月8日 00:26:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定