Python SDK call example
This topic provides an example of how to use Python to call the SingleCallByVoice OpenAPI operation and send a voice call to a specified phone number.
Step 1: View the OpenAPI document
Before you call an OpenAPI operation, read the documentation for SingleCallByVoice to learn about its required parameters and permissions. For more information, see API overview.
Step 2: Create a RAM user and grant permissions
An Alibaba Cloud account has full access permissions for all APIs. For security purposes, we recommend that you create and use a RAM user to call APIs or perform routine O&M tasks.
You can use an Alibaba Cloud account, a RAM user, or a RAM role to call this operation. For more information about the differences between these identities, see Identities.
This example uses a RAM user to call the operation.
Log on to the RAM console.
In the navigation pane on the left, choose .
On the Users page, click Create User.
On the Create User page, set the Logon Name and Display Name. Set Access Mode to Console Access.
Click OK.
After the RAM user is created, record the logon name and password. You need them to log on to the Alibaba Cloud OpenAPI Developer Portal to call the OpenAPI operation.
Grant permissions to the RAM user.
NoteAliyunDyvmsReadOnlyAccess: Grants read-only access permissions for Voice Service.
AliyunDyvmsFullAccess: Grants full management permissions for Voice Service.
To create a custom policy, see Authorization information.
Go to the Users page.
In the Actions column of the target RAM user, click Add Permissions.
Under Access Policy, search for
AliyunDyvmsand select the AliyunDyvmsFullAccess policy.Click OK to complete the authorization.
On the details page of the RAM user, click the Credentials tab and then click Create AccessKey.
For more information, see Create an AccessKey pair.
Step 3: Call the OpenAPI operation
This topic uses the Python SDK as an example. You can use the SDKs for other languages in a similar way. For more information, see Voice Service SDK. You can also use other methods to call the operation. For more information about call methods, see Call methods.
Prepare the Python environment
Download and install Python 3. If Python is already installed, you can run the following command to check its version: python --version.
The Alibaba Cloud SDK for Python no longer supports Python 2.7, Python 3.6, or earlier versions. For more information, see Announcement on ending support for Python 2.7 and Python 3.6 in Alibaba Cloud Python SDK.
Before you call the operation, configure environment variables to read your access credentials. The environment variable for the AccessKey ID is VMS_AK_ENV, and the environment variable for the AccessKey secret is VMS_SK_ENV. For more information about how to configure environment variables, see Configure environment variables on Linux, macOS, and Windows.
Install dependencies
In your terminal, run the following commands to install the dependencies.
pip3 install alibabacloud_credentials
pip install alibabacloud_dyvmsapi20170525
pip3 install alibabacloud_tea_consoleDownload the sample code
The following steps describe how to call the SingleCallByVoice operation. You can select the operation that you want to call in the OpenAPI Portal.
Go to the SingleCallByVoice debugging page in the OpenAPI Portal.
On the left-side Parameter Settings tab, set the required parameters. In this example, set Service Endpoint to China (Hangzhou).
On the right-side SDK Sample tab, select Python and click the Download Project button to download the sample code package.
Decompress the package on your computer and navigate to the alibabacloud_sample folder.
Run the code
Run the following command:
python sample.pyThe following example shows a sample response:
[
LOG
] {
"headers": {
"date": "Wed, 12 Jul 2023 06:22:23 GMT",
"content-type": "application/json;charset=utf-8",
"content-length": "116",
"connection": "keep-alive",
"keep-alive": "timeout=25",
"access-control-allow-origin": "*",
"access-control-expose-headers": "*",
"x-acs-request-id": "7DD3DADA-B8D8-51B6-8A55-A22C26A8****",
"x-acs-trace-id": "db1857ded103cf003d8630e47f4d****",
"etag": "1CvtvpGzpFfc6RCnX0al****"
},
"body": {
"Message": "OK",
"RequestId": "4A2568E1-7D83-5491-8DEF-600D8AFF97E7",
"CallId": "136119591002^12288307****",
"Code": "OK"
}
}