Lambdas are custom functions that can be fired using a trigger action. Creating a lambda allows you to execute a custom code you created using any triggers' available rules. Lambdas run within a Node.js 18.x environment and are given 256MB and 15 seconds to complete.
Lambdas are available on Sales Engage, Enterprise, and Connect plans.
Creating a Lambda
- Navigate to Flows → Lambdas.
- Enter a name for this lambda function.
- In the Code section, use the interface to write the custom code you wish to execute. The lambda interface gives you the ability to:
- Select font size
- Select your editor (Ace, Vim, or Emacs)
- Full-screen the editor
- Test the script by selecting a specific call
- If you wish to add environment variables to your lambda, click +Add Variable and specify the name and value for your variable.
- Click Save Changes.
Running Lambdas via Triggers
To execute your new lambda, you will need to create a trigger or add a new action to an existing trigger. Click here to learn more about trigger setup.
- Navigate to Flows → Triggers. If you wish to edit an existing trigger, click edit next to the trigger name you want to use to run the lambda. Otherwise, you will need to create a new trigger.
- Use the trigger settings to configure when the trigger runs, and add any necessary rules to the workflow.
- To run the lambda function as part of the trigger, click Add Action, then select Run Lambda Function.
- Use the drop-down menu to select the name of the lambda you wish to run.
- Click Save Changes.
API Details
Here is a simple lambda function that exports the handler method and passes an event and a context to it.
```js
exports.handler = async (event, context) => {
const ctm = context.ctm;
const call = event.activity;
}```
The event has a method event.activity that represents the call, text, form, or chat (i.e., the activity object). context has a few methods that may be helpful.
context.ctm is a helper object that provides an interface to the event.activity and the CTM API.
context.done is a useful method when calling a lambda function for voice routing or if you need to pass data to a lambda function.
If you are asked to score a call/activity when the weather is above 80 at the postal_code
location, then you can invoke the following method after helping to fetch the weather via the event.activity.postal_code:
await ctm.score("Hot", 5, 80, true, new Date())
You can also update a field on the activity record using the ctm.update method, passing it an object.
Update the name and assign the user_field1 custom field to xyz. In this case, the user can tell you they have a custom field user_field1:
await ctm.update({name: "First Last", custom_fields: {user_field1: "xyz"}})
You can generate code that calls an AI function to answer questions about the activity record, e.g.
const name = await ctm.ask("What was the name of the caller?")
const email = await ctm.ask("What was the email address the caller provided?")
Important: all of our integrations, e.g., Salesforce, use OAuth2 instead of username and password. For Salesforce, use jsforce.
We invoke all methods with a valid access token, but other options, such as instanceUrl, are still necessary.
Integration instanceUrl and accessToken are always exposed via the event object, e.g., for Salesforce, event.salesforce.token. or zoho, event.zoho.token.
IMPORTANT: We never ever update the id property with ctm.update
ctm.api_post(path, data)
ctm.api_patch(path, data)
ctm.api_put(path, data)
ctm.api_delete(path, data)
ctm.api_get(path)
for each of the above you can skip the https://api.calltrackingmetrics.com/api/v1/accounts/{{account_id}}/ prefix, as it is added automatically.
Tagging:
Tags can be added to the activity when updating it using tag_list. Keep in mind that tag_list will overwrite any previously set tags. For this reason, you should append your new tags to the list of tags. Also, keep in mind that tag_list is a comma-separated string of tags. Tags are always lowercase, so if you try to set a tag with an uppercase letter, it will be downcased automatically.
Running Lambdas via Voice Phone Calls.
In addition to running lambda functions that make external API calls and modify call/text and other activity data, you can also use them to process and route phone calls.
To do this, either select a single tracking number and set its Dial Route to point to the lambda function or, from a Voice Menu or Smart Router, direct the call to the lambda function.
Example Voice Routing Lambda function:
In the following example, we'll play a message to the caller and then route them to a receiving number, "dial_number".
```js
context.done(null, {say: "We are routing to the phone number now", route: dial_number});
```
You can also pass any object to the route, e.g., a voice menu, call queue, or agent by ID, e.g., VMIxyz, QUExyz, or USRxyz.
You may also want to whisper something to the receiving number using the whisper property.
```js
context.done(null, {whisper: "a call is being routed to you.", route: dial_number});
```
Collect input from a caller and pass some context data to the collection routine.
```js
context.done(null, {say: "Press one if they are interested in product 1, and press 2 if they are not.", gather: {
finishOnKey: '#',
numDigits: 1,
timeout: 15,
state: context_state
}});
```
After the caller either timeout is reached or the caller enters a keypress or # in the case above then the function will be called again but with both call.context_state the original context you passed in and the call.digits: the keypress options the caller entered.
Comments
Article is closed for comments.