其他分享
首页 > 其他分享> > 腾讯短信功能二次封装

腾讯短信功能二次封装

作者:互联网

腾讯短信功能二次封装

# 腾讯云官方
	-短信申请:申请签名,申请模板
    -使用官方提供的sdk或者api发送短信
    -如果没有sdk就是用api调用
    -现在有sdk,优先使用sdk,简单---》不同语言封装的包,直接调用包的方法就可以完成相关操作
    - 2.0版本的sdk,3.0版本的sdk
    
    -安装3.0的sdk:pip install tencentcloud-sdk-python   # 腾讯云所有的功能都集成到这个包中了
    -安装2.0的sdk:pip install qcloudsms_py              # 腾讯云短信功能在这个包中

image-20220712120354302

封装v2版本

init.py

from .sms import get_code,send_sms_v2

settings.py

# 短信应用 SDK AppID
APPID =   # SDK AppID 以1400开头
# 短信应用 SDK AppKey
APPKEY = ""
# 需要发送短信的手机号码

# 短信模板ID,需要在短信控制台中申请
TEMPLATE_ID = 1470213  # NOTE: 这里的模板 ID`7839` 只是示例,真实的模板 ID 需要在短信控制台中申请
# 签名
SMS_SIGN = "咋啦叭呼"  # NOTE: 签名参数使用的是`签名内容`,而不是`签名ID`。这里的签名"腾讯云"只是示例,真实的签名需要在短信控制台中申请

sms.py

# 生成随机n位验证码的函数
import random

from qcloudsms_py import SmsSingleSender
from qcloudsms_py.httpclient import HTTPError
from . import settings  # 使用相对导入


def get_code(n=4):
    code = ''
    for i in range(n):
        code += str(random.randint(0, 9))
    return code


# 发送短信的函数
def send_sms(phone, code):
    phone_numbers = [phone, ]
    ssender = SmsSingleSender(settings.APPID, settings.APPKEY)
    params = [code, '1']  # 当模板没有参数时,`params = []`
    try:
        result = ssender.send_with_param(86, phone_numbers[0],
                                         settings.TEMPLATE_ID, params, sign=settings.SMS_SIGN, extend="", ext="")
    except Exception as e:
        return False
    return True

封装v3版本

init.py

from .sms import get_code,send_sms_v2

settings.py

SECRET_ID='AKIDZL2jO2WtBOWaXPE5qV9iKtPvRrCQZiiY'
SECRET_KEY='kKl5FF6oNvLZaR5WklZAZllY9XkneIl2'


APPID = '1400705789'  # SDK AppID 以1400开头
TEMPLATE_ID = '1470213'
# 签名
SMS_SIGN = "咋啦叭呼"


### v3版本的APPID  TEMPLATE_ID  都必须使用str类型,数字类型报错

sms.py

# 生成随机n位验证码的函数
import random

from qcloudsms_py import SmsSingleSender
from qcloudsms_py.httpclient import HTTPError
from . import settings  # 使用相对导入
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
# 导入对应产品模块的client models。
from tencentcloud.sms.v20210111 import sms_client, models
# 导入可选配置类
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from . import settings


def get_code(n=4):
    code = ''
    for i in range(n):
        code += str(random.randint(0, 9))
    return code


# 发送短信的函数
def send_sms(phone, code):
    try:
        cred = credential.Credential(settings.SECRET_ID, settings.SECRET_KEY)
        httpProfile = HttpProfile()
        httpProfile.reqMethod = "POST"
        httpProfile.reqTimeout = 30
        httpProfile.endpoint = "sms.tencentcloudapi.com"
        clientProfile = ClientProfile()
        clientProfile.signMethod = "TC3-HMAC-SHA256"
        clientProfile.language = "en-US"
        clientProfile.httpProfile = httpProfile
        client = sms_client.SmsClient(cred, "ap-guangzhou", clientProfile)
        req = models.SendSmsRequest()
        req.SmsSdkAppId = settings.APPID
        req.SignName = settings.SMS_SIGN
        req.TemplateId = settings.TEMPLATE_ID
        req.TemplateParamSet = [code, '5']
        req.PhoneNumberSet = ["+86" + phone, ]
        req.SessionContext = ""
        req.ExtendCode = ""
        req.SenderId = ""
        resp = client.SendSms(req)
        # print(resp.to_json_string(indent=2))
        return True
    except TencentCloudSDKException as err:
        # print(err)
        return False


标签:code,短信,settings,py,sms,封装,腾讯,import,ID
来源: https://www.cnblogs.com/zhengkaijian/p/16486853.html