Help centre
العربية Back to sayvara.com

Amazon Connect

Send a survey after each answered Amazon Connect call, from the contact event your AWS account already publishes.

Updated

What it does

When a voice contact ends in Amazon Connect, AWS publishes a DISCONNECTED contact event in your AWS account. A small function in your account adds the customer's number and sends the event to Sayvara. Sayvara queues one survey for each answered call with a customer.

Status

Works with Amazon Connect. Sayvara reads the DISCONNECTED contact event in the format AWS documents in its Amazon Connect administrator guide ("Sample contact event for when a voice call is disconnected"). Contact records (the contact-record stream) instead of events: add-on, available on request (below).

What you need first

  • Everything on Phone systems.
  • In AWS: someone who can create an EventBridge rule, a Lambda function and its IAM role in the account and region of your Amazon Connect instance.

Set it up

1. Create the function

Create a Lambda function (Node.js 20 or newer) named sayvara-call-ended. Give its role permission for connect:DescribeContact on your instance. Set two environment variables: SAYVARA_API_KEY (your API key) and, only when signed triggers are on, SAYVARA_SIGNING (your signing secret). Keep both in AWS Secrets Manager if your policy asks for it.

import { createHmac } from 'node:crypto';
import { ConnectClient, DescribeContactCommand } from '@aws-sdk/client-connect';

const connect = new ConnectClient({});
const url = 'https://api.sayvara.com/v1/triggers/pbx/amazon-connect';

export const handler = async (event) => {
  const d = event.detail;
  const instanceId = d.instanceArn.split('/').pop();
  const { Contact } = await connect.send(new DescribeContactCommand({ InstanceId: instanceId, ContactId: d.contactId }));
  const body = JSON.stringify({ ...event, detail: { ...d, customerEndpoint: Contact?.CustomerEndpoint } });
  const headers = { 'Content-Type': 'application/json', Authorization: `Bearer ${process.env.SAYVARA_API_KEY}` };
  if (process.env.SAYVARA_SIGNING) {
    const t = Math.floor(Date.now() / 1000);
    const mac = createHmac('sha256', process.env.SAYVARA_SIGNING).update(`${t}.${body}`).digest('hex');
    headers['Sayvara-Signature'] = `t=${t},v1=${mac}`;
  }
  const res = await fetch(url, { method: 'POST', headers, body });
  if (res.status >= 500 || res.status === 429) throw new Error(`Sayvara answered ${res.status}`); // AWS retries
  return res.status;
};

2. Send ended calls to the function

In EventBridge, create a rule on the default event bus with this pattern, and the function as its target:

{
  "source": ["aws.connect"],
  "detail": {
    "eventType": ["DISCONNECTED"],
    "channel": ["VOICE", "Voice"]
  }
}

Add "instanceArn": ["<your instance ARN>"] inside detail if the account has more than one instance. If your Amazon Connect contact events are off, turn them on for the instance first.

3. Test

Make a short test call to your Amazon Connect number and answer it as an agent. The function's log shows 202. The new contact appears in Sayvara under Contacts, with an invitation from the source API.

How the call is read

Sayvara Amazon Connect
Call id (one survey per call) initialContactId, else contactId: a transferred call gets one survey, queued when its first leg ends; the following legs answer already_surveyed
Customer number customerEndpoint.Address, added by the function from DescribeContact
Direction initiationMethod: INBOUND and transfers are inbound; OUTBOUND, API, CALLBACK and campaigns are outbound; MONITOR and disconnect flows get no survey
Answered The contact reached an agent (connectedToAgentTimestamp)
Talk time From reaching the agent to disconnectTimestamp
End time disconnectTimestamp

Contact records instead of events

Add-on, available on request. If you already stream contact records (Kinesis) and prefer them to events, we set this up with you. The recipe: a function on the Kinesis stream decodes each record (Records[].kinesis.data, base64 JSON) and sends it to the same address, signed the same way, without DescribeContact. Sayvara reads ContactId, InitialContactId, CustomerEndpoint, InitiationMethod, Agent and DisconnectTimestamp. The function above reads events only; it does not read stream records. Records arrive after events, and both give the same call id, so send either events or records for the same calls, not both.

Troubleshooting

What you see What to do
"reason": "no_customer_number" The function did not add customerEndpoint. Check its role allows connect:DescribeContact.
"reason": "not_answered" The customer hung up before reaching an agent.
401 Check the API key, and the signing secret if signed triggers are on.

For every other answer, see Phone systems.