申请AccessKey
得到access_key_id和access_key_secret
安装SDK
pip install alibabacloud_dysmsapi20170525==2.0.24
调用短信模块,进行发送短信测试
import json
from alibabacloud_dysmsapi20170525.client import Client
from alibabacloud_tea_openapi.models import Config
from alibabacloud_dysmsapi20170525.models import SendSmsRequest
from alibabacloud_tea_util.models import RuntimeOptions
class AliyunSMS:
access_key_id = ''
access_key_secret = ''
endpoint = 'dysmsapi.aliyuncs.com'
sign_name = '阿里云短信测试'
template_code = 'SMS_154950808'
def __init__(self):
self.config = Config(
access_key_id=self.access_key_id,
access_key_secret=self.access_key_secret,
endpoint=f'dysmsapi.aliyuncs.com'
)
def send(self, mobile: str, code: str):
"""
:param mobile:手机号
:param code:验证码
:return:
"""
# 创建客户端
client = Client(self.config)
# 创建短信对象
send_sms_request = SendSmsRequest(
phone_numbers=mobile,
template_param=json.dumps({'code': code}),
sign_name=self.sign_name,
template_code=self.template_code
)
# 设置允许时间选项
runtime = RuntimeOptions()
# 发送短信
client.send_sms_with_options(send_sms_request, runtime)
if __name__ == '__main__':
AliyunSMS().send(mobile='18888888888', code='888888')
更多信息参考:官方文档
© 版权声明
THE END
暂无评论内容