AWS Lambda:KeyError

时间:2018-07-09 06:31:36

标签: amazon-web-services aws-lambda amazon-lex

我正在编写一个AWS Lambda函数,它显示了一个关键错误。我无法将插槽和currentIntent名称分配给其他变量。我正在创建一个类,该类将JSON参数初始化为该类的对象实例。我正在用Python编写代码。屏幕截图如下:

代码:

import boto3
import pymysql
import os
# import logging
#import json

# logger = logging.getLogger()
# logger.setLevel(logging.INFO)

RDS_HOST = 'safelex.cl9sev7auhbk.us-east-1.rds.amazonaws.com'
RDS_USER = 'root'
RDS_REGION = 'us-east-1'

class LexEvent:
    """handles incoming and outgoing params to Lex and validates slots"""

def __init__(self, event):
    self.event = event
    self.slots = event['currentIntent']['slots']
    self.intent = event['currentIntent']['name']
    self.input_text = event['inputTranscript']
    self.sess_attr = event['sessionAttributes']
    self.invocation = event['invocationSource']


### Helpers to control state of the conversation

def delegate(self, intent=None):
    return {
        'sessionAttributes': self.sess_attr,
        'dialogAction': {
            'type': 'Delegate',
            'slots': self.slots
        }
    }

def fulfill(self, msg="Your document is complete."):
    return {
        'sessionAttributes': self.sess_attr,
        'dialogAction': {
            'type': 'Close',
            'fulfillmentState': 'Fulfilled',
            "message": {
              "contentType": "PlainText",
              "content": msg
            }
        }
    }



def elicit_slot(self, err):
    return {
        'sessionAttributes': self.sess_attr,
        'dialogAction': {
            'type': 'ElicitSlot',
            'intentName': self.intent,
            'slots': self.slots,
            'slotToElicit': err['violatedSlot'],
            'message': {'contentType': 'PlainText', 'content': err['message'] }
        }
    }

def elicit_intent(self, msg="How can I help you?"):
    return {
         "sessionAttributes": self.sess_attr,
         "dialogAction": {
            "type": "ElicitIntent",
            "message": {
              "contentType": "PlainText",
              "content": msg
        }
    }
}

def connection(lex):
    conn = pymysql.connect(
        user=RDS_USER,
        password='safe1234',
        host=RDS_HOST,
        database='safe_lex',
        charset='utf8',
        #ssl_verify_cert=True,
        #ssl_ca='./rds-combined-ca-bundle.pem'
    )

    cursor = conn.cursor()
    cursor.execute("SELECT safes_score FROM sc_company_score WHERE DATE(created_at) = '2018-07-03'")
    rows = cursor.fetchall()
    for row in rows:
        return row[0]

def dispatch(lex):
if lex.intent == "SafeScore":
    score = lex.connection(lex)
    return lex.fulfill("Your SAFE score is %0.2f")


def lambda_handler(event, context=None):
    #event= json.loads(event)
    lex = LexEvent(event)
    #logger.info(('IN', event))
    #try:
    res = dispatch(lex)
    logger.info(('OUT', res))
    return res

错误截图:

enter image description here

0 个答案:

没有答案