'AWS Lex v2 bot response card giving error on twilio

Problem statement: I want to access Lex V2 chatbot with response card buttons on WhatsApp.

I created a Lex V2 bot, and created a channel for Twilio. Connected Twilio through WhatsApp, and able to use my bot fine for plaintext responses. But in my bot, I also return a response card with buttons for one of the slot values. This works fine on AWS console, but when I run it through Twilio, it gives error saying response body is empty, or too big. Sharing my lambda code below. Please help on how to make it work?

const AWS = require('aws-sdk');

exports.handler = async (event, context) => {
    //console.log("EVENT = ", event);
    const sessionState = event['sessionState'];
    const sessionAttributes = sessionState['sessionAttributes'];

    const location = get_slot(event, 'Location');
    const checkInDate = get_slot(event, "CheckInDate");
    const nights = get_slot(event, "Nights");
    const roomType = get_slot(event, "RoomType");
    
    const response = "Booking completed for location = " + location +
        ", checkInDate = " + checkInDate +
        ", nights = " + nights +
        ", roomType = " + roomType;

    console.log('slots = ', response);

    if(!roomType){
        const message = {
                'contentType': 'ImageResponseCard',
                "imageResponseCard": {
                    "title": "Room Type",
                    "subtitle": "Select your room preference",
                    //"imageUrl": "",
                    "buttons": [
                        {
                            "text": "King",
                            "value": "king"
                        },
                        {
                            "text": "Queen",
                            "value": "queen"
                        },
                        {
                            "text": "Deluxe",
                            "value": "deluxe"
                        }
                    ]
                }
            };
        /*const message =  {
            'contentType': 'PlainText',
            'content': 'select room king.q'
        };*/    
        return elicit_intent(event, sessionAttributes, message);
    }
    
   const message =  {
            'contentType': 'PlainText',
            'content': response
        };
    return close(event, sessionAttributes, message);
};

function close(intent_request, session_attributes, message) {
    intent_request['sessionState']['intent']['state'] = "Fulfilled";
    return {
        'sessionState': {
            'sessionAttributes': session_attributes,
            'dialogAction': {
                'type': 'Close'
            },
            'intent': intent_request['sessionState']['intent']
        },
        'messages': [message],
        'sessionId': intent_request['sessionId'],
        'requestAttributes': intent_request['requestAttributes'] 
    };
}

function elicit_intent(intent_request, session_attributes, message){
    intent_request['sessionState']['intent']['state'] = "InProgress";
    return {
        'sessionState': {
            'sessionAttributes': session_attributes,
            'dialogAction': {
                'type': 'ElicitSlot',
                "slotToElicit": "RoomType",
            },
            'intent': intent_request['sessionState']['intent']
        },
        'messages': [message],
        'sessionId': intent_request['sessionId'],
        'requestAttributes': intent_request['requestAttributes']
    };
}

function get_slot(intent_request, slotName) {
    const slots = intent_request['sessionState']['intent']['slots'];
    if(slots[slotName] && slots[slotName].value && slots[slotName].value.interpretedValue){
        return slots[slotName].value.interpretedValue;
    }
   return null;
}
   
    


Solution 1:[1]

Twilio developer evangelist here.

The Twilio API for WhatsApp currently only supports messages with buttons via registered template messages.

Once your template with buttons has been approved, you can send buttons as part of your WhatsApp messages. To send a button, you need to send a message that contains the body of the template. The buttons are automatically appended to the message.

In your case, you will need to send a message body so that the template can be matched and include the buttons.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1