Created
April 6, 2020 11:03
-
-
Save nivleshc/d66df70dabab8d7e11f65d38160eb6c4 to your computer and use it in GitHub Desktop.
lifeinsurance-bot-lambda-code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from botocore.vendored import requests | |
from dateutil.relativedelta import relativedelta | |
from datetime import datetime | |
def lambda_handler(event, context): | |
print("event:"+str(event)) | |
print("context:"+str(context)) | |
customer_state = event['currentIntent']['slots']['State'] | |
customer_firstname = event['currentIntent']['slots']['FirstName'] | |
customer_lastname = event['currentIntent']['slots']['LastName'] | |
customer_dob_str = event['currentIntent']['slots']['DOB'] | |
customer_coverlevel = event['currentIntent']['slots']['CoverLevel'] | |
customer_smoker = event['currentIntent']['slots']['Smoker'] | |
customer_gender = event['currentIntent']['slots']['Gender'] | |
print(customer_state) | |
print(customer_firstname) | |
print(customer_lastname) | |
print(customer_dob_str) | |
print(customer_coverlevel) | |
print(customer_smoker) | |
print(customer_gender) | |
date_now = datetime.now() | |
date_now_year = date_now.year | |
customer_dob_year = int(customer_dob_str) | |
customer_age = date_now_year - customer_dob_year | |
print("Customer YOB:" + customer_dob_str) | |
print("Customer age:" + str(customer_age)) | |
if customer_gender == "Female": | |
sex = 0 | |
else: | |
sex = 1 | |
if customer_smoker == "NO": | |
smoker = 0 | |
else: | |
smoker = 1 | |
url = "url-for-ml-model-api” | |
data = {"age": customer_age, “state”: customer_state, "sex": sex, "smoker": smoker} | |
r = requests.post(url,json=data) | |
premium = r.json()['claim_pred'] | |
print("premium: " + str(premium)) | |
message = customer_firstname + " from what you have told me, your monthly premiums will be approximately $" + str(round(premium/12)) | |
return { | |
"sessionAttributes": {}, | |
"dialogAction": { | |
"type": "Close", | |
"fulfillmentState": "Fulfilled", | |
"message": { | |
"contentType": "PlainText", | |
"content": message | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment