Amazon Lex Integration
Integrate Sticky Calls API with Amazon Lex for caller memory in voice bots.
Setup with Lambda Function
Time: 10 minutes Difficulty: Easy
Step 1: Create Lambda Function
import json
import boto3
import requests
STICKY_CALLS_API_KEY = 'YOUR_API_KEY'
STICKY_CALLS_URL = 'https://api.stickycalls.com'
def lambda_handler(event, context):
intent_name = event['sessionState']['intent']['name']
session_attributes = event['sessionState'].get('sessionAttributes', {})
# Get caller phone from session
caller_phone = session_attributes.get('caller_phone', '')
if intent_name == 'IdentifyCaller':
return identify_caller(event, caller_phone)
elif intent_name == 'SaveContext':
return save_context(event, caller_phone)
def identify_caller(event, caller_phone):
# Call Sticky Calls API
response = requests.post(
f'{STICKY_CALLS_URL}/v1/calls/start',
headers={
'Authorization': f'Bearer {STICKY_CALLS_API_KEY}',
'Content-Type': 'application/json'
},
json={
'call_id': event['sessionId'],
'identity_hints': {
'ani': caller_phone,
'external_ids': {
'lex_session_id': event['sessionId']
}
}
}
)
data = response.json()
identity = data.get('identity', {})
# Store in session attributes
session_attributes = {
'customer_ref': data.get('customer_ref', ''),
'identity_confidence': str(identity.get('confidence', 0.0)),
'identity_level': identity.get('level', 'none'),
'variables': json.dumps(data.get('variables', {})),
'open_intents': json.dumps(data.get('open_intents', []))
}
# Build response message
if identity.get('confidence', 0) >= 0.7:
message = "Welcome back! I can see your previous interaction history."
else:
message = "Welcome! How can I help you today?"
return {
'sessionState': {
'sessionAttributes': session_attributes,
'dialogAction': {
'type': 'Close'
},
'intent': {
'name': 'IdentifyCaller',
'state': 'Fulfilled'
}
},
'messages': [
{
'contentType': 'PlainText',
'content': message
}
]
}
def save_context(event, caller_phone):
slots = event['sessionState']['intent']['slots']
session_attributes = event['sessionState'].get('sessionAttributes', {})
context = slots.get('ConversationSummary', {}).get('value', {}).get('interpretedValue', '')
# Save to Sticky Calls
requests.post(
f'{STICKY_CALLS_URL}/v1/calls/end',
headers={
'Authorization': f'Bearer {STICKY_CALLS_API_KEY}',
'Content-Type': 'application/json'
},
json={
'call_id': event['sessionId'],
'customer_ref': session_attributes.get('customer_ref', ''),
'intent': event['sessionState']['intent']['name'],
'intent_status': 'closed',
'variables': {
'summary': context,
'session_id': event['sessionId']
}
}
)
return {
'sessionState': {
'dialogAction': {
'type': 'Close'
},
'intent': {
'name': 'SaveContext',
'state': 'Fulfilled'
}
}
}
Step 2: Create Lex Intents
IdentifyCaller Intent
Invocation:
- At conversation start
Fulfillment:
- Lambda function:
StickyCallsIntegration
Response:
- Personalized greeting based on match
SaveContext Intent
Sample Utterances:
- "I'm done"
- "That's all"
- "Goodbye"
Slots:
ConversationSummary(required)
Fulfillment:
- Lambda function:
StickyCallsIntegration
Step 3: Configure Lex Bot
- Add Lambda Permission:
aws lambda add-permission \
--function-name StickyCallsIntegration \
--statement-id lex-invoke \
--action lambda:InvokeFunction \
--principal lex.amazonaws.com
- Link to Bot:
- Lex Console → Your Bot → Aliases → Prod
- Lambda function:
StickyCallsIntegration
Flow Example
User calls bot
↓
Lex invokes: IdentifyCaller intent
↓
Lambda calls: Sticky Calls API
↓
IF identity.confidence >= 0.7:
Response: "Welcome back! I can see your interaction history."
ELSE:
Response: "Welcome! How can I help?"
↓
... conversation ...
↓
User says: "That's all"
↓
Lex invokes: SaveContext intent
↓
Lambda saves context to Sticky Calls
↓
End conversation
Testing
- Test in Lex Console
- Make first call (no match)
- Complete conversation
- Make second call (should match)
- Verify personalized greeting
Ready to add memory to Amazon Lex? Sign up →