Amazon Lex/CloudFormation提供了API和控制台来获取服务的反馈。使用AWS SDK或AWS命令行界面可以访问这些API。下面是使用AWS SDK for Python(Boto3)获取Amazon Lex服务的正向/负向反馈的示例代码:
import boto3
client = boto3.client('lex-runtime')
response = client.post_text(
botName='myLexBot',
botAlias='myBotAlias',
userId='1234',
inputText='Hello'
)
message = response['message']
dialog_state = response['dialogState']
if dialog_state == 'ElicitSlot':
slot_to_elicit = response['slotToElicit']
print(f"Please provide {slot_to_elicit}")
elif dialog_state == 'ConfirmIntent':
intent_name = response['intentName']
print(f"Confirm {intent_name}")
elif dialog_state == 'ReadyForFulfillment':
intent_name = response['intentName']
fulfillment_state = response['fulfillmentState']
print(f"Intent {intent_name} ready for fulfillment with state {fulfillment_state}")
else:
print("Got response with status " + dialog_state + ": " + message)
上述代码会向Amazon Lex的Bot发送文本输入“Hello”,然后解析返回的Json响应并提取“dialogState”属性。如果“dialogState”是“ElicitSlot”,则Bot需要请求提供更多的槽值;如果“dialogState”是“ConfirmIntent”,则Bot需要确认意图;如果“dialogState”是“ReadyForFulfillment”,则Bot已经完全理解请求并准备履行请求。否则,Bot将返回解答或其他状态。
同样地,您可以使用AWS SDK for Python(Boto3)获取Amazon CloudFormation服务的反馈。下面是一个示例代码:
import boto3
client = boto3.client('cloudformation')
response = client.describe_stacks(
StackName='myStackName'
)
status = response['Stacks'][0]['StackStatus']
if status == 'CREATE_COMPLETE':
print("Stack created successfully")
elif status == 'ROLLBACK_COMPLETE':
print("Stack creation rolled back")
else:
print("Stack creation is in progress...")
上述代码会获取Amazon CloudFormation服务中名为“myStackName”的堆栈的信息。它提取堆栈状态(“StackStatus”属性),并针对其值打印不同的消息。如果堆栈状态为