Day80 use third-party (Tencent cloud) SMS verification code Interface

Third party (Tencent cloud) SMS verification code Interface

Tencent cloud messaging features links:

https://cloud.tencent.com/product/sms

Tencent cloud messaging function API documentation:

https://cloud.tencent.com/document/product/382/38763

Specific program on their own Baidu

需要先注册一个腾讯云的开发者账号
创建短信应用
创建短信签名和模板

提示:新用户可以领取100条免费短信

SMS Interface Secondary Packaging

# settings
# 短信应用 SDK AppID
appid = 1400304547  # SDK AppID 以1400开头
# 短信应用 SDK AppKey
appkey = "0beef3d647e5c1336192f2e7d873fce4"
# 短信模板ID,需要在短信控制台中申请
template_id = 516903  # NOTE: 这里的模板 ID`7839`只是示例,真实的模板 ID 需要在短信控制台中申请
# 签名
sms_sign = "IT咸鱼之家"  # NOTE: 签名参数使用的是`签名内容`,而不是`签名ID`。这里的签名"腾讯云"只是示例,真实的签名需要在短信控制台中申请


# sms.py
import random


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


from qcloudsms_py import SmsSingleSender
from .settings import *

from utils.logging import logger

sender = SmsSingleSender(appid, appkey)

def send_sms(mobile, code, exp):
    params = [code, exp]  # 当模板没有参数时,`params = []`
    try:
        response = sender.send_with_param(86, mobile,
                                          template_id, params, sign=sms_sign, extend="", ext="")
        if response or response.get('result') == 0:
            return True
        logger.error('短信发送失败,状态码: %s, 错误信息: %s' % (response.get('result'), response.get('errmsg')))
        return False
    except Exception as e:
        logger.error('短信发送异常,异常信息: %s' % e)

Guess you like

Origin www.cnblogs.com/2222bai/p/12163929.html