[Python]AWS Lex 串接 Lambda Python3.7
最近剛好在寫一些給公司用的機器人.
用到了AWS Lex
串接Lambda
.
AWS Lex
的就直接跳過.
AWS Lex
訊息是由json
格式來傳遞的.
格式範例如下:
{
"currentIntent": {
"name": "intent-name",
"slots": {
"slot name": "value",
"slot name": "value"
},
"slotDetails": {
"slot name": {
"resolutions" : [
{ "value": "resolved value" },
{ "value": "resolved value" }
],
"originalValue": "original text"
},
"slot name": {
"resolutions" : [
{ "value": "resolved value" },
{ "value": "resolved value" }
],
"originalValue": "original text"
}
},
"confirmationStatus": "None, Confirmed, or Denied (intent confirmation, if configured)"
},
"bot": {
"name": "bot name",
"alias": "bot alias",
"version": "bot version"
},
"userId": "User ID specified in the POST request to Amazon Lex.",
"inputTranscript": "Text used to process the request",
"invocationSource": "FulfillmentCodeHook or DialogCodeHook",
"outputDialogMode": "Text or Voice, based on ContentType request header in runtime API request",
"messageVersion": "1.0",
"sessionAttributes": {
"key": "value",
"key": "value"
},
"requestAttributes": {
"key": "value",
"key": "value"
},
"recentIntentSummaryView": [
{
"intentName": "Name",
"checkpointLabel": Label,
"slots": {
"slot name": "value",
"slot name": "value"
},
"confirmationStatus": "None, Confirmed, or Denied (intent confirmation, if configured)",
"dialogActionType": "ElicitIntent, ElicitSlot, ConfirmIntent, Delegate, or Close",
"fulfillmentState": "Fulfilled or Failed",
"slotToElicit": "Next slot to elicit"
}
],
"sentimentResponse": {
"sentimentLabel": "sentiment",
"sentimentScore": "score"
}
}
雖然上面落落長, 但我們要的只需要currentIntent
底下的資訊.
所以在抓取slots
的部分Lambda
會用到的程式碼如下:
def yourLamda_handler(event, context):
variableA = event['currentIntent']['slots']['varA']
variableB = event['currentIntent']['slots']['varB']
...
...
...
return something
Lambda
就會接收到在AWS Lex
的中設定的slots varA & varB
.
然後在擷取出來做一些動作.