{"size": 3055, "ext": "js", "max_stars_repo_path": "examples/inbound-call/index.js", "max_stars_repo_name": "cmreigrut/amazon-chime-private-bot-demo", "max_stars_repo_stars_event_min_datetime": "2019-05-15T03:25:20.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-05T22:36:37.000Z", "max_issues_repo_path": "examples/inbound-call/index.js", "max_issues_repo_name": "cmreigrut/amazon-chime-private-bot-demo", "max_issues_repo_issues_event_min_datetime": "2019-06-22T23:28:41.000Z", "max_issues_repo_issues_event_max_datetime": "2020-02-21T00:34:09.000Z", "max_forks_repo_path": "examples/inbound-call/index.js", "max_forks_repo_name": "cmreigrut/amazon-chime-private-bot-demo", "max_forks_repo_forks_event_min_datetime": "2019-05-28T01:23:42.000Z", "max_forks_repo_forks_event_max_datetime": "2021-10-30T20:28:48.000Z"}
{"max_stars_count": 13, "max_issues_count": 2, "max_forks_count": 10, "avg_line_length": 31.4948453608, "max_line_length": 86, "alphanum_fraction": 0.6860883797}
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0

const requestPromise = require('request-promise');
const AWS = require('aws-sdk');

const CUSTOMER_NAME_KEY = 'CustomerName';
const PRODUCT_NAME_KEY = 'ProductName';
const ZIP_CODE_KEY = 'CustomerZipCode';

const chime = new AWS.Chime({endpoint: 'service.chime.aws.amazon.com.'});

const handleInboundCall = async (event, context) => {
    try {
      const email = process.env.Email;
      const incomingwebhook = process.env.IncomingWebhook;
      const user = await getUser(email);
      const notificationMessage = buildNotification(event, user);
      await notifyRoom(incomingwebhook, notificationMessage);
      return {
          'OutboundNumber': user.PrimaryProvisionedNumber
      };
    } catch (err) {
      console.log(`Cannot forward call to business calling number, error = ${err}`);
      throw err;
    }
};

const buildNotification = (event, user) => {
  let customerName = getAttribute(event, CUSTOMER_NAME_KEY);
  const productName = getAttribute(event, PRODUCT_NAME_KEY);
  const zipCode = getAttribute(event, ZIP_CODE_KEY);
  const customerPhoneNumber = getContactData(event).CustomerEndpoint.Address;
  return `
    Received customer call, redirecting to <@${user.UserId}|${user.DisplayName}>
    Name: ${capitalize(customerName)}
    Product interested: ${capitalize(productName)}
    Zip code: ${zipCode}
    Phone Number: ${customerPhoneNumber}`
}

const capitalize = (input) => {
  return input.charAt(0).toUpperCase() + input.slice(1);
}

const getContactData = (event) => event.Details.ContactData;

const getAttribute = (event, attributeName) => {
  try {
    const contactData = getContactData(event);
    return contactData.Attributes[attributeName];
  } catch (error) {
    console.log(`Cannot find ${attributeName}, error = ${error}`);
    throw new Error(`Cannot find ${attributeName}`);
  }
};

const getUserAccountId = async (email) => {
  if (!email) {
    throw new Error('User email cannot be empty');
  }
  const response = await chime.listAccounts({UserEmail: email}).promise();
  if (!response || !response.Accounts || response.Accounts.length < 1) {
    throw new Error(`Cannnot find account for email ${email}`);
  }
  return response.Accounts[0].AccountId;
};

const getUser = async (email) => {
  const accountId = await getUserAccountId(email);
  if (!accountId) {
    throw new Error('User account id cannot be empty');
  }
  const params = {
    AccountId: accountId, /* required */
    UserEmail: email /* required */
  };
  const response = await chime.listUsers(params).promise();
  if (!response || !response.Users || response.Users.length < 1) {
    throw new Error(`Cannnot find user for email ${email}, accountId = ${accountId}`);
  }
  return response.Users[0];
}

const notifyRoom = (webhook, messageBody) => {
  return requestPromise({
    method: 'POST',
    uri: webhook,
    body: {
        Content: messageBody
    },
    json: true
  });
};

exports.handler = handleInboundCall;
