英文:
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('lexv2-runtime')
# Send the user's input to the bot
response = client.recognize_text(
botId='<botId>',
botAliasId='<botAliasId>',
localeId='<localeId>',
sessionId='<sessionId>',
text='event['input']
)
# Return the bot's response to the caller
return {
'output': response['messages']
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论