python Ali cloud messaging service access process

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_43193386/article/details/100286796

Ali cloud messaging service access process

Recently the company projects business requirements include text with Ali cloud, so writing a blog and share with you using python sdk docking Ali cloud the short interest api process.

Ali goes official documents

Link
Here Insert Picture Description
on the left navigation bar, there are many parts, focusing see sdk api reference and reference to
Here Insert Picture Description
Here Insert Picture Description

You must first download and install python core SDK library

Sdk library where there are two versions, install the latest version.
Legacy:

pip install aliyun-python-sdk-core

The new version:

pip install aliyun-python-sdk-core-v3

You can install one or more (I installed the new version), because there are other demands I also installed a dependent package

pip install  aliyun-python-sdk-ecs

After installation is complete, the local environment even ready to set the stage.

Creating AccessKey

Ali cloud api want to call must have its own accesskey, this is a required parameter when the interface is called.
Access key AccessKey (AK) corresponds to the password, but using different scenarios. AccessKey invoke the procedures for Short Message Service API, and login password to log into the console.
AccessKey including AccessKeyId and AccessKeySecret.
AccessKeyId identifies the user.
AccessKeySecret is used to verify the user's key. AccessKeySecret must be kept confidential.
Create a connection
Follow the steps to create can be.

And create a signature template

Top right, click Login, then click on the console
Here Insert Picture Description
to find the SMS service, click to enter
Here Insert Picture Description
Here Insert Picture Description
click on domestic news, you can see the signature management and template management. And then add a signature template, your signature is sent text messages will be written on top xxx, xxx for your signature, marking a major role to play, template format your text messages. Usually reviewed within two hours. This is manually added signatures and templates, you can also directly call Ali cloud api add templates. Here Insert Picture Description
I will not explain here, later will be unified interpretation.

send messages

Click api Reference sendsms, which is something of a similar interface to the document, there is a request of the above parameters, return parameters, we have a detailed description. Click debugging fill the parameters you will get a python code
Here Insert Picture Descriptionfollowing is my code:

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest

def send_sms(template):
    # client = AcsClient('<accessKeyId>', '<accessSecret>', 'default')
    client = AcsClient('你的accessKeyId', '你的accessSecret', 'default')
    request = CommonRequest()
    request.set_accept_format('json')
    request.set_domain('dysmsapi.aliyuncs.com')
    request.set_method('POST')
    request.set_protocol_type('https')  # https | http
    request.set_version('2017-05-25')
    # 以上部分是公用的不变
    request.set_action_name('SendSms')
    # set_action_name 这个是选择你调用的接口的名称,如:SendSms,SendBatchSms等
    request.add_query_param('RegionId', "default")
    # 这个参数也是固定的
    
    request.add_query_param('PhoneNumbers', "13222222222") # 发给谁
    request.add_query_param('SignName', "博客") # 签名
    request.add_query_param('TemplateCode', "SMS_111111111") # 模板编号
    request.add_query_param('TemplateParam', f"{template}") # 发送验证码内容
    response = client.do_action_with_exception(request)
    print(str(response, encoding='utf-8'))

if __name__ == '__main__':
    template = {
        'code':'88888',
    }
    send_sms(template) 

Because many of the above things are fixed, so I simply modified a bit, do some of the more common

def aliyun_send_sms_common_api(action, query_param_dict):
    """
    阿里云短信服务公用接口
    :param action: 指明短信相关的哪些接口
    :param query_param_dict: 短信相关接口的参数(字典形式)
    :return:
    """
    # client = AcsClient('<accessKeyId>', '<accessSecret>', 'default')
    client = AcsClient(ACCESSKEYID, ACCESSSECRET, "default")
    request = CommonRequest()
    request.set_accept_format('json')
    request.set_domain('dysmsapi.aliyuncs.com')
    request.set_method('POST')
    request.set_protocol_type('https')  # https | http
    request.set_version('2017-05-25')
    request.set_action_name(action)
    request.add_query_param('RegionId', "default")
    for k, v in query_param_dict.items():
        if type(v) == "enum":
            return
        request.add_query_param(k, v)
    response = client.do_action_with_exception(request)
    response = str(response, encoding='utf-8')
    return response
if __name__ == "__main__":
	action = "你要调用的接口名称"
	query_param_dict = {
		"PhoneNumbers":"13222222222"
	} # 接口中需要的参数,注意字典中的key要和接口文档中的一样,不能忽略大小写
	response = aliyun_send_sms_common_api(action, query_param_dict)
	print(response)

send messages

Here Insert Picture Description
After successful results returned, also made on cell phone received, if the error or return the message is not ok, you can go to the center api error to view the error messages
api error Center

Note: Sending text messages is to spend money they must ensure adequate Ali cloud center account balance before texting, other interfaces, just set_action_name and some necessary parameters are not the same, the other is the same.

There are shortcomings, but also please exhibitions in the comments section, I will first reply.

Guess you like

Origin blog.csdn.net/qq_43193386/article/details/100286796