amazon-connect / amazon-connect-chat-interface

Amazon Connect Chat Interface - a browser-based customer chat widget for your website integrating with Amazon Connect ChatJS
https://docs.aws.amazon.com/connect/latest/adminguide/what-is-amazon-connect.html
MIT No Attribution
36 stars 39 forks source link

Contact Attributes not being set on contacts #134

Closed jjwilliams42 closed 1 year ago

jjwilliams42 commented 1 year ago

When initializing a chat, the contact attributes are never saved on the contact when looking at their record in Amazon Connect.

Here is our initialization code:

image

Here are the contact attributes that get stored in Amazon Connect (all of these came from the flow except customerName which came from chat):

image

xiajohn commented 1 year ago

Hi, could you try to steps below and see if the issue is still there?

https://github.com/amazon-connect/amazon-connect-chat-ui-examples/issues/108#issuecomment-1571103916

spencerlepine commented 1 year ago

Hi @jjwilliams42, are you having an issue passing an attribute from the chat widget to the contact flow? Could you try the steps listed below and let us know if this address the issue?

Thanks, Spencer

spencerlepine commented 1 year ago

Passing Custom Attribute to Contact Flow

When initializing the chat with a StartChatContact request, you can pass custom attributes to the the contact flow.

flowchart LR
    START["`Chat Widget
    StartChat Request`"] --> B["`Lambda (AWS-SDK)
    connect.startChatContact`"];
    B --> C["`Amazon Connect
    StartChatContact API`"];
    C --> END["`Amazon Connect
    Contact Flow`"];

Reference

Configuration

  1. If using the GitHub AmazonConnectChatWidget, pass in custom contactAttributes to the ChatInterface.initiateChat() method. This will pass "Attributes" key in the request body
// https://github.com/amazon-connect/amazon-connect-chat-interface/blob/master/src/components/Chat/ChatInitiator.js

connect.ChatInterface.initiateChat({
  name: customerName,
  region: ${region},
  contactFlowId: "${contactFlowId}",
  instanceId: "${instanceId}",
  apiGatewayEndpoint: "${apiGatewayEndpoint}",
  contactAttributes: JSON.stringify({
    "customerName": customerName,
    "customAttribute": "myCustomAttribute". // <------ CUSTOM ATTRIBUTE HERE
  }),

},successHandler, failureHandler)
  1. Update the lambda making the StartChatContact call, make sure to forward body["Attributes"] to connect.startChatContact()
/*
  Example `startChatContactAPI` lambda making a call to the Amazon Connect public StartChatContact API

  LINK: https://github.com/amazon-connect/amazon-connect-chat-ui-examples/blob/master/cloudformationTemplates/startChatContactAPI/js/startChatContact.js

  1) Chat Widget will make request to this Lambda
  2) Lambda will forward the request to the Amazon Connect Backend
*/

var AWS = require('aws-sdk');
AWS.config.update({region: process.env.REGION});
var connect = new AWS.Connect();

exports.handler = (event, context, callback) => {
    console.log("Received event: " + JSON.stringify(event));
    var body = JSON.parse(event["body"]);

    startChatContact(body).then((startChatResult) => {
        callback(null, buildSuccessfulResponse(startChatResult));
    }).catch((err) => {
        console.log("caught error " + err);
        callback(null, buildResponseFailed(err));
    });
};

function startChatContact(body) {
    return new Promise(function (resolve, reject) {
        var startChat = {
            "InstanceId": body["InstanceId"],
            "ContactFlowId": body["contactFlowId"],
            "Attributes": {
                "customerName": body["ParticipantDetails"]["DisplayName"],
                // ...
                ...body["Attributes"] // <------ CUSTOM ATTRIBUTE HERE
            },
            "ParticipantDetails": {
                "DisplayName": body["ParticipantDetails"]["DisplayName"]
            },
        };

        // https://docs.aws.amazon.com/connect/latest/APIReference/API_StartChatContact.html
        connect.startChatContact(startChat, function(err, data) {
            if (err) {
                console.log("Error starting the chat.", err);
                reject(err);
            } else {
                console.log("Start chat succeeded with the response: " + JSON.stringify(data));
                resolve(data);
            }
        });
    });
}
  1. Consume the new attribute in the contact flow. Refer to the Admin Guide - "Use Amazon Connect contact attributes"
mliao95 commented 1 year ago

Closing issue. Feel free to re-open if you are still experiencing any issues