使用python进行配置的间接:https://cloud.tencent.com/document/product/382/11672
封装成一个包
settings.py
# 短信应用 SDK AppID
APPID = 1400009099 # SDK AppID 以1400开头
# 短信应用 SDK AppKey
APPKEY = "9ff91d87c2cd7cd0ea762f141975d1df37481d48700d70ac37470aefc60f9bad"
# 短信模板ID,需要在短信控制台中申请
TEMPLATE_ID= 7839 # NOTE: 这里的模板 ID`7839`只是示例,真实的模板 ID 需要在短信控制台中申请
# 签名
SMS_SIGN = "腾讯云" # NOTE: 签名参数使用的是`签名内容`,而不是`签名ID`。这里的签名"腾讯云"只是示例,真实的签名需要在短信控制台中申请
sms.py
from qcloudsms_py import SmsSingleSender
from .settings import *
from luffyapi.utils.logging import logger
import random
#mac系统的ssl安全验证
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
sender = SmsSingleSender(APPID,APPKEY)
class Send_sms:
def __init__(self,mobile,exp):
self.mobile = mobile
self.code = self.get_code()
self.exp = exp
# 短信发送模块
def send_sms(self):
try:
response = sender.send_with_param(86, self.mobile, TEMPLATE_ID, (self.code, self.exp), sign=SMS_SIGN, extend="", ext="")
except Exception as e:
logger.error('sms error: %s' % e)
return False
if response and response['result'] == 0:
return True
logger.error('sms error:%s' % response['errmsg'])
return False
# 随机验证码生成模块
def get_code(self):
self.code = ''
for i in range(4):
self.code += str(random.randint(0, 9))
return self.code
init.py
from .sms import Send_sms
提醒:
qcloudsms_py模块别忘记安装了,指令如下
pip install qcloudsms_py