其他分享
首页 > 其他分享> > 腾讯云短信的写法

腾讯云短信的写法

作者:互联网

腾讯云短信的写法

# 使用腾讯云短信的写法  需要 import random  from django.conf import settings
# from gongju.tengxun.sms import send_sms_single
def 短信(request):
    code = random.randrange(1000, 9999)  # 生成随机的验证码,在1000和9999之间
    tpl = request.GET.get('tpl')  # 得到用户从浏览器传入的值
    tpl_id = settings.TENGXUN_SMS_MUBAN.get(tpl)  #从设置中读取项目模板的id号
    if not tpl_id:
        return HttpResponse('模板不存在')
    res = send_sms_single("166xxxxx", tpl_id, [code, ])  # 实例化单发送短信的对象
    # print(res) {'result': 1014, 'errmsg': 'package format error, sdkappid not have this tpl_id', 'ext': ''}
    if res['result'] == 0:
        return HttpResponse('轰炸成功')
    return HttpResponse(res['errmsg'])

sms 的写法 放在外层工具里面

pip install qcloudsms_py   # 这里面有一个编码的错误,删掉就可以了

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import ssl
# ssl._create_default_https_context = ssl._create_unverified_context
from qcloudsms_py import SmsMultiSender, SmsSingleSender
from qcloudsms_py.httpclient import HTTPError
def send_sms_single(phone_num, template_id, template_param_list):
    """
    单条发送短信
    :param phone_num: 手机号
    :param template_id: 腾讯云短信模板ID
    :param template_param_list: 短信模板所需参数列表,例如:【验证码:{1},描述:{2}】,则传递参数 [888,666]按顺序去格式化模板
    :return:
    """
    appid = xxxx  # 自己应用ID
    appkey = "xxxxx"  # 自己应用Key
    sms_sign = "Python之路"  # 自己腾讯云创建签名时填写的签名内容(使用公众号的话这个值一般是公众号全称或简称)
    sender = SmsSingleSender(appid, appkey)
    try:
        response = sender.send_with_param(86, phone_num, template_id, template_param_list, sign=sms_sign)
    except HTTPError as e:
        response = {'result': 1000, 'errmsg': "网络异常发送失败"}
    return response
	
def send_sms_multi(phone_num_list, template_id, param_list):
    """
    批量发送短信
    :param phone_num_list:手机号列表
    :param template_id:腾讯云短信模板ID
    :param param_list:短信模板所需参数列表,例如:【验证码:{1},描述:{2}】,则传递参数 [888,666]按顺序去格式化模板
    :return:
    """
    appid = 112142311
    appkey = "8cc5b87123y423423412387930004"
    sms_sign = "Python之路"
    sender = SmsMultiSender(appid, appkey)
    try:
        response = sender.send_with_param(86, phone_num_list, template_id, param_list, sign=sms_sign)
    except HTTPError as e:
        response = {'result': 1000, 'errmsg': "网络异常发送失败"}
    return response
if __name__ == '__main__':
    result1 = send_sms_single("151xxxxxx", 548760, [666, ])
    print(result1)
    result2 = send_sms_single( ["151xxxxx", "151xxxx", "151xxxx", ],548760, [999, ])
    print(result2)

SMS 短信的配置文件

#    #################### SMS 短信的配置文件 #######################

# 自己应用id
TENGXUN_SMS_ID = 6666666

# 自己应用key
TENGXUN_SMS_KEY = '66666666666'

# 自己腾讯云创建签名时填写的签名内容(使用公众号的话这个值一般是公众号全称或简称)
TENGXUN_SMS_QIANMING ="铁铁说个人公众号"

# 发短信的模板消息类型
TENGXUN_SMS_MUBAN ={
    'denglu': 6666,
    'zhuce':6666,
    'chongzhi':6666,
}

标签:短信,list,sms,param,template,腾讯,写法,id
来源: https://www.cnblogs.com/tietiejiang/p/16227102.html