function startChatContact()

in src/initiate-chat-lambda/index.js [19:86]


function startChatContact(body) {
    let contactFlowId = "";
    if (body.hasOwnProperty('ContactFlowId')) {
        contactFlowId = body["ContactFlowId"];
    }
    console.log("CF ID: " + contactFlowId);

    let instanceId = "";
    if (body.hasOwnProperty('InstanceId')) {
        instanceId = body["InstanceId"];
    }
    console.log("Instance ID: " + instanceId);

    let initialMsgContent = "";
    let initialMsgContentType = "";
    if (body.hasOwnProperty("InitialMessage")) {
        if (body["InitialMessage"].hasOwnProperty("Content")) {
            initialMsgContent = body["InitialMessage"]["Content"];

        }
        if (body["InitialMessage"].hasOwnProperty("ContentType")) {
            initialMsgContentType = body["InitialMessage"]["ContentType"];
        }
    }
    
    let topic = "";
    if (body.hasOwnProperty("Attributes")) {
        if (body["Attributes"].hasOwnProperty("topic")) {
            topic = body["Attributes"]["topic"];
        }
                    
    }

    return new Promise(function(resolve, reject) {
        const startChat = {
            "InstanceId": instanceId == "" ? process.env.INSTANCE_ID : instanceId,
            "ContactFlowId": contactFlowId == "" ? process.env.CONTACT_FLOW_ID : contactFlowId,
            "Attributes": {
                "customerName": body["ParticipantDetails"]["DisplayName"],
                "topic" : topic
            },
            "ParticipantDetails": {
                "DisplayName": body["ParticipantDetails"]["DisplayName"]
            }
        };
        
        if (initialMsgContent && initialMsgContentType != "" ){
            startChat.InitialMessage = {
                "Content": initialMsgContent,
                "ContentType": initialMsgContentType
            };
        };
        
        console.log('startChat params', startChat);
        connect.startChatContact(startChat, function(err, data) {
            if (err) {
                console.log("Error starting the chat.");
                console.log(err, err.stack);
                reject(err);
            }
            else {
                console.log("Start chat succeeded with the response: " + JSON.stringify(data));
                resolve(data);
            }
        });

    });
}