This topic describes how to use the Alibaba Cloud SDK for Python to call the SendMessageWithTemplate operation. This API operation allows you to send text messages to Mainland China.

Request parameters

Parameter Type Required Example Description
To String Yes 45***0121 The mobile phone number to receive the text message. Note that you must add the country code to the beginning of the mobile phone number.
From String No XXCompany The ID of the sender. This parameter specifies the text message signature when you send the text message to Mainland China.
TemplateCode String Yes SMS_0000 The ID of the text message template. You can view the ID of the text message template in the console of Short Message Service (SMS).
TemplateParam String No {"code":"1234","product":"ytx"} The values of the variables that are included in the text message template. The values must use the JSON format. If line feeds are required in JSON-formatted data, the line feeds must meet the relevant requirements that are specified in the standard JSON protocol.
SmsUpExtendCode String No 90999 The extension code of the upstream text message. This parameter is optional. You can skip this parameter based on your business requirements.

Response parameters

Parameter Type Example Description
To String 45***0121 The mobile phone number that received the text message. Note that the country code is added to the beginning of the mobile phone number.
MessageId String 1008***3003003 The ID of the text message. You can call the QueryMessage operation to query the delivery status of the text message based on the ID of the text message.
ResponseCode String OK The status code of the request. If the API operation returns OK, the request is successful. For more information about the other error codes, see Error codes.
ResponseDescription String The text message is received. The description of the status code.
Segments String 1 The number of the text messages that incurred fees.

Install the core library of the Alibaba Cloud SDK for Python

  • Method 1: Use the pip package installer.
    pip install aliyun-python-sdk-core
    					
  • Method 2: Download the aliyun-python-sdk-core library by using the following link: Core library of the Alibaba Cloud SDK for Python. The Alibaba Cloud SDK for Python allows you to call SMS API operations.

Sample requests

The considerations are described as follows:

  • When you construct the AcsClient object, you must use the default value of the regionId parameter. The default value is ap-southeast-1. Note that regionId is the third parameter when you construct the AcsClient object.
  • You must use the default domain name: dysmsapi.ap-southeast-1.aliyuncs.com.
  • You must use the default version number: 2018-05-01.
#! /usr/bin/env python
#coding=utf-8

import os
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
client = AcsClient('<accessKeyId>', '<accessSecret>', 'ap-southeast-1')

request = CommonRequest()
request.set_accept_format('json')
request.set_domain('dysmsapi.ap-southeast-1.aliyuncs.com')
request.set_method('POST')
request.set_version('2018-05-01')
request.set_action_name('SendMessageWithTemplate')

request.add_query_param('To', '8615200000000')
request.add_query_param('From', 'SMS signature')
request.add_query_param('TemplateCode', 'SMS_225001')
request.add_query_param('TemplateParam', '{"code":"1234"}')
request.add_query_param('SmsUpExtendCode', '12345')

response = client.do_action(request)
# python2: print(response) 
print(str(response, encoding = 'utf-8'))