Python3 短信功能
环境版本:
Python3.7.2
当我们需要一些实时信息并且具有推送功能时,可以使用短信方式来告知开发者。
这里,博主使用阿里云的短信模块。申请短信签名和模块不多讲述,阿里云文档中讲述很清楚。这里将分享短信的批量发送和单一发送的案例。
首先,需要我们部署短信模块
pip install aliyun-python-sdk-core
部署完成之后,就可以使用了。(需要你的AK和签名)
这里直接上代码,博主将阿里云的短信库在调用的时候,已经装饰了一下,可以直接使用
# -*- coding:utf-8 import json from aliyunsdkcore.client import AcsClient from aliyunsdkcore.request import CommonRequest def sms_batch_module(phone: list, data: list, order: str) -> str: """ # sms_modules函数,批量发送短信 Args: phone: list 联系方式 data: list 数据列表 order: str 模板代码 Returns: 阿里云状态码 """ client = AcsClient(AccessKeyId, AccessSecret, Location) 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('SendBatchSms') # SendBatchSms request.add_query_param('RegionId', "cn-hangzhou") request.add_query_param('PhoneNumberJson', json.dumps(phone)) # 这里是签名,需要写两个,如果有3个人,就需要写3个 SignNameJson request.add_query_param('SignNameJson', [SignNameJson, SignNameJson]) request.add_query_param('TemplateCode', order) request.add_query_param('TemplateParamJson', json.dumps(data)) response = client.do_action(request) return str(response, encoding='utf-8')
注释采用
string
返回值,这是批量发送短信函数if __name__ == '__main__': print(sms_batch_module(phone=["phone", "phone"], order="SMS_xxxxxx", data=[{"name": "小明", "time": "2019-12-17", "element": "00001"}, {"name": "大明", "time": "2019-12-18", "element": "00002"}]))
看到这里,短信批量发送是可以正常使用的,但需要重点说一下短信模板的问题
# 正常来说一个短信结构是这个样子 【菜鸟网络】 尊敬的${name},你好!于${time}时,你产生了一笔交易,订单号为${element}。 # 当你需要数据传入的时候,可以看一下上面的数据传入。假设你要给两个人发送短信 # PhoneNumberJson是这个样子, phone是你要发送给谁 phone = ["phone", "phone"] # 模板代码却只用写一个,签名却需要写两个一样的,按照你实际的短信签名和模板来 order = "SMS_xxxxxx" # 数据是这个样子 data=[{"name": "小明", "time": "2019-12-17", "element": "00001"}, {"name": "大明", "time": "2019-12-18", "element": "00002"}] # 如果还是看不懂的话,那么下面这个是应该能的吧~ phone = [phone1, phone2, phone3] data = [ {"name1": "1", "time": "1", "element": "1"}, {"name2": "2", "time2": "2", "element2": "2"}, {"name3": "3", "time3": "3", "element3": "3"} ]
这块代码时发送单个短信的代码,和上面
def sms_module(phone, name, time, element, order) -> str: """ # sms_module函数,指定发送短信 Args: phone: 联系方式(电话) name: 名称 element: 机床编号 time: 当前时间 order: 模板代码 Returns: 阿里云状态码 """ client = AcsClient(AccessKeyId, AccessSecret, Location) 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') # SendBatchSms request.add_query_param('RegionId', "cn-hangzhou") request.add_query_param('PhoneNumbers', phone) request.add_query_param('SignName', "你的签名") request.add_query_param('TemplateCode', order) request.add_query_param('TemplateParam', str({'name': '{}'.format(name), 'time': '{}'.format(time), 'element': '{}'.format(element)})) response = client.do_action(request) return str(response, encoding='utf-8')
如果还有什么不明白的地方,可以在下方留言,博主将会第一时间回复!谢谢浏览和包容不足!