SMS verification function

send messages

We need to send text messages using third-party platforms, such as Ali cloud, the cloud communications (for python3 unfriendly), Tencent cloud. As used herein as an exemplary cloud Tencent

Tencent cloud messaging preparations

1. Tencent cloud official website registration, real-name authentication, login (not nonsense, skip)

2. Click on the top right of the page "console"

3. Click on the navigation bar of cloud product, you can see a variety of products, found "message." For the first time will be transferred to "subscribe page", check agreed to begin access. (This is a personal certification, certification also requires some authentication-related material)

4. Access has three main functions: an application list, the package management packages, SDK & API.

* 应用列表:管理应用的;
* 套餐包管理:管理套餐包的;
* SDK & API:就是一些相关的指南手册(开发指南);

5. In the "Add Application" application list page, add a point to go after (such as the application name: the technology stack)

6. come home message, click the message content configuration

7. SMS signature -> to create a signature: to use micro-channel public number or applet (micro-channel public number registered Home shots), fill in Figure creating signatures

8. SMS text -> Create a text template: Fill Figure SMS text.

9. pending review

Background development

Reference SDK & API Developer's Guide in: Documentation Center> SMS> SDK Documentation> Python SDK https://cloud.tencent.com/document/product/382/11672

Configure SDK

pip install qcloudsms_py

To prepare the necessary parameters

# 短信应用 SDK AppID 以1400开头
appid = 1400009099  
# 短信应用 SDK AppKey 根据自己的短信应用配置
appkey = "9ff91d87c2cd7cd0ea762f141975d1df37481d48700d70ac37470aefc60f9bad"
# 需要发送短信的手机号码(非必填项,可在你的开发代码中传入)
phone_numbers = ["21212313123", "12345678902", "12345678903"]
# 短信模板ID,真实的模板 ID 需要在短信控制台中申请
template_id = 7839  # 这里的模板 ID`7839`只是示例,
# 签名,使用的是`签名内容`,而不是`签名ID`。这里的签名"腾讯云"只是示例,真实的签名需要在短信控制台中申请
sms_sign = "腾讯云"  # 发写个空字符串也行

Specify a single template ID texting

import random
from utils.logging import logger
from .settings import *
from qcloudsms_py import SmsSingleSender
ssender = SmsSingleSender(appid, appkey)
# 生成验证码
def get_code():
    code = ''
    for i in range(4):
        code += str(random.randint(0, 9))
    return code

def send_sms(mobile, code, exp):
    """
    发送短信
    :param mobile: 电话号码
    :param code: 验证码
    :param exp: 过期时间
    :return:
    """
    try:
        response = ssender.send_with_param(86, mobile, template_id, (code, exp), sign=sms_sign, extend="", ext="")
        # 短信发送成功的标识:没有异常且response大字典中的result为0
        if response and response['result']==0:
            return True
        logger.error('sms error: %s'% response['errmsg'])
        return
    except Exception as e:
        logger.error("sms error: %s" % e)
        return False
    
if __name__ == '__main__':
    code = get_code()
    print(code)
    result = send_sms('xxxxxxx',code,'1')  # 电话号码,验证码,过期时间
    print(result)

Guess you like

Origin www.cnblogs.com/863652104kai/p/11541077.html