腾讯云短信封装(v3版本)
1.版本介绍1.1 设置方面v3跟v2相比,在要求方面,增加了两样,一个API密钥(SecretId: SecretKey)一个是地域。但是取消了一个应用key腾讯云密钥生成https://console.cloud.tencent.com/cam/capi2. 腾讯云短信发送函数封装utilsimport jsonfrom tencentcloud.common import credentia
·
1.版本介绍
1.1 设置方面
v3跟v2相比,在要求方面,增加了两样,一个API密钥(SecretId: SecretKey)一个是地域。
但是取消了一个应用key
腾讯云密钥生成https://console.cloud.tencent.com/cam/capi
2. 腾讯云短信发送函数封装
utils
import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.sms.v20210111 import sms_client, models
from django.conf import settings
# pip3 install tencentcloud-sdk-python
def send_msg(telephone, tpl_id, code):
"""
:param telephone: 手机号 str
:param tpl_id: 短信模板的id,str格式
:param code: 格式必须是 ["验证码",参数] 参数可以为空,具体看自己的模板要求
发送短信验证码
验证码发送到手机上,购买服务器进行发送短信:腾讯云
1.注册腾讯云,开通腾讯云短信。
2.创建应用
SDK AppID = 1400302209
3.申请签名(个人:公众号)
ID 名称
260514 Python之路
4.申请模板
ID 名称
516680 miniprogram
5.申请腾讯云API https://console.cloud.tencent.com/cam/capi
SecretId: 密钥id 相对应v2,v3添加了密钥,而去除了应用key
SecretKey:
6.调用相关接口去发送短信 https://cloud.tencent.com/document/product/382/38778
SDK,写好的工具。
"""
try:
phone = '+86%s' % telephone
cred = credential.Credential(settings.TENCENT_SECRET_ID, settings.TENCENT_SECRET_KEY)
httpProfile = HttpProfile()
httpProfile.endpoint = "sms.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = sms_client.SmsClient(cred, "ap-guangzhou", clientProfile)
req = models.SendSmsRequest()
params = {
"PhoneNumberSet": [phone, ], # 手机号
"SmsSdkAppId": settings.TENCENT_APP_ID, # 应用AppId,v2要应用sdk/key,v3新增了密钥,取消了key
"SignName": settings.TENCENT_SIGN, # 签名的模版名称
"TemplateId": tpl_id, # 短信模板的id,str格式
"TemplateParamSet": code # 格式必须是 ["验证码",参数] 参数可以为空,具体看自己的模板要求
}
req.from_json_string(json.dumps(params))
resp = client.SendSms(req)
# print(resp.to_json_string())
if resp.SendStatusSet[0].Fee == 1:
return True
return
except TencentCloudSDKException as err:
print(err)
"""
{"SendStatusSet": [{"SerialNo": "2433:121624486616453503738803075", "PhoneNumber": "+8614203330752", "Fee": 1, "SessionContext": "", "Code": "Ok", "Message": "send success", "IsoCode": "CN"}], "RequestId": "2b2bae83-737e-489d-a2fb-b150f498f433"}
"""
view
from app02.u2 import send_msg
from rest_framework.views import APIView
import random
class T3(APIView):
def get(self, request, *args, **kwargs):
code = random.randrange(1000, 9999)
print(code)
# code 具体传几个,看自己的模板要求,但是一定是列表的形式
ret = send_msg(telephone=15203330752, tpl_id="993156", code=["12345", "888"])
return Response('ok')
settings
# ############################# 腾讯云短信配置 ##########################
TENCENT_SECRET_ID = "AKID4X8xxxxxxxxxxxTXKq53l" # 密钥id
TENCENT_SECRET_KEY = "SdWMo0xxxxxvZwjGWVE" # 密钥key
TENCENT_CITY = "ap-guangzhou" # 地域,默认即可,
TENCENT_APP_ID = "6xxxxxx6" # 应用id,v2要应用id/key,v3新增了密钥
TENCENT_SIGN = "python后端接口开发" # 签名的模版名称
TENCENT_SMS_TEMPLATES = {
'register': "993292",
'login': "993155"
}
ps 如果上述封装的函数失效了,可以自己去官方生成一个,
1. 注意填的参数(我标注的这些必须填,不要看他的选填,)
更多推荐
所有评论(0)