This topic describes how to configure an Interactive Voice Response (IVR) flow to call a third-party API and query a phone number's location.
Function overview
Cloud Contact Center is integrated with Alibaba Cloud Function Compute. As shown in the following figure, you can use Function Compute to enable the Cloud Contact Center IVR to call third-party services or your own business systems.

Imagine a Cloud Contact Center instance with different skill groups for different regions, such as Beijing, Hebei, and Shanghai. You can route incoming calls to the correct skill group based on the caller's location. This topic shows how to use Function Compute to create an IVR flow that does this.
Create a function
Activate and authorize Function Compute
First, activate the Function Compute service. Use your Alibaba Cloud account to log on to the Function Compute console. When you log on for the first time, you are prompted to activate Function Compute. Click Activate Now. For more information about billing, see Pricing of Function Compute. A RAM user can also perform this operation. To do so, grant the AliyunFCFullAccess permission to the RAM user to manage Function Compute (FC), or grant permissions across Alibaba Cloud accounts using a RAM role.
Create a new function
Before you create a function, learn about key concepts in Function Compute, such as service endpoints and functions.
A service endpoint specifies the region of the Function Compute service.
A Service is the basic unit for managing resources in Function Compute. Each service endpoint can contain multiple Services.
A Function is the basic unit for scheduling and execution. It is a piece of application code that performs a specific task. Each Service can contain multiple Functions.
Select a region and create a new Service. In this example, select China (Hangzhou) and create a Service named demo-service.

In demo-service, create a new Function. In this example, select a blank function template and create a Python 2.7 function named get_number_region.

Implement the function logic. For information about the function entry point, see Define a function handler. The function is implemented as follows:

# -*- coding: utf-8 -*- import logging import json import urllib2 def handler(event, context): try: phone = json.loads(event)['phone'] url ="https://cx.shouji.360.cn/phonearea.php?number={number}".format(number=phone) request = urllib2.Request(url) result = urllib2.urlopen(url).read() value = json.loads(result) return'{province}{city}'.format(province=value['data']['province'], city=value['data']['city']) except Exception as e: logging.error(" Unexpected Error: {}".format(e))
Test the function
After you edit the function, you can test it in the Function Compute console.

Click Execute. The result is null. This is because the first step in the function, `phone = json.loads(event)['phone']`, needs to retrieve the `phone` field from the input parameter, but no value was assigned. To test the function, click Triggering Event to the right of Execute, enter the parameters, and click Save.

Click Execute. The phone number's location is returned. The function is now created in the Function Compute console.

Add the function information in Cloud Contact Center
Log on to the Cloud Contact Center console. In the navigation pane on the left, choose Instance Management-V2. Click the endpoint of the target instance to open the instance console.

Click the menu button in the upper-right corner and select Voice Service to open the voice service console. In the navigation pane on the left, select Settings.
On the Settings page, click the IVR Integrated Functions tab.

Click Add. For this example, enter the details for the function that you created in the Function Compute console: set Region to China East 1, Service to demo_service, and Function to get_number_region.

Verify function connectivity
You already verified that the function works in the Function Compute console. After you add the function information, a Test button appears to the right of the function details. Click Test to check if the information is correct and if Cloud Contact Center can call the function. If the function call is successful, a result is returned. This indicates that Cloud Contact Center can successfully call your function in Function Compute.

Create an IVR with a function node
As shown in the following figure, create an IVR flow. When a customer calls, the Playback node plays a welcome message. The flow then proceeds to the Function node. Configure the Function node as follows:
Select function: In the Function node, select the function that you added.
Set input parameters: The parameter name must match the parameter name that is parsed by your function's code. Because the parameter name in the function is `phone`, set the input parameter name to `phone`. Set the parameter value to `${Start.caller}` to retrieve the caller's number. (`caller` is a system parameter. You can view system parameters in the Start node).
Function node exit: If the function is invoked successfully, the flow proceeds to the next node. In this example, the next node is a Branch node. The branch condition uses the `${Function.responseBody}` parameter. For example, you can create branches based on the location returned by the function, and then perform different operations. Configure the subsequent processing as required for your business flow.
Multiple output parameters: In the function node, enable the Multiple Parameters switch to allow the function to return a JSON object. For example, if the function returns `{title: 'Multi-parameter test', age: 10, name: 'Zhang San'}`, you can configure this feature as shown in the following figure. The parameter item is the custom parameter, and the assignment item is the key from the JSON object that is returned.
NoteThe defined parameter items cannot be duplicates or the same as the output parameter. Flow parameter names can contain only uppercase letters, lowercase letters, and underscores.
Our location query function relies on a third-party API, so we cannot guarantee its availability. As a precaution, connect a Transfer to Agent node to the function invocation failure exit. This ensures that if the function fails, the call is transferred to an agent instead of being disconnected. Configure this handling as required for your business flow.
If the function node runs successfully, the flow proceeds to the Branch node. In the Branch node, set the conditions based on the output parameters from the function node. This example provides two conditions for reference. Based on the result of the Branch node, the call is routed to the specified skill group.





