• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

腾讯云短信个人记录

武飞扬头像
心诩
帮助5

腾讯云短信

注册创建云短信账号

https://pythonav.com/wiki/detail/10/81/

3.0 云短信SDK使用

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

安装sdk

pip install --upgrade tencentcloud-sdk-python

发短信代码

from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.sms.v20210111 import sms_client, models

from django.conf import settings


def send_message(phone, random_code, template_id="449739"):
    """
    :param phone: 手机号码
    :param random_code: 类型:str 随机验证码
    :param template_id: 类型:str 模版ID
    :return: True代表发送成功
    """
    try:
        # 手机号码拼接
        phone = "{}{}".format(" 86", phone)
        # 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey。
        cred = credential.Credential(settings.TENCENT_SECRET_ID, settings.TENCENT_SECRET_KEY)

        # 实例化要请求产品(以sms为例)的client对象
        # 第二个参数是地域信息,可以直接填写字符串ap-guangzhou
        client = sms_client.SmsClient(cred, settings.TENCENT_CITY)

        # 实例化一个请求对象,根据调用的接口和实际情况,可以进一步设置请求参数
        req = models.SendSmsRequest()
        req.SmsSdkAppId = settings.TENCENT_APP_ID
        # 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名
        req.SignName = settings.TENCENT_SIGN
        # 模板 ID: 必须填写已审核通过的模板 ID
        req.TemplateId = template_id
        # 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,,若无模板参数,则设置为空
        random_code = str(random_code)
        req.TemplateParamSet = [random_code]
        # 示例如: 8613711112222, 其中前面有一个 号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号
        req.PhoneNumberSet = [phone]

        resp = client.SendSms(req)

        # 输出json格式的字符串回包
        # print(resp.to_json_string(indent=2))
        if resp.SendStatusSet[0].Code == "Ok":
            return True

    except TencentCloudSDKException as err:
        print(err)

if __name__ == '__main__':
    # 第一个参数是电话号码,第二个是验证码,第三个是模板ID
    send_message(15533333333,"1234","449739")
学新通

调用函数使用

from utils.tencent.msg import send_message
from django.conf import settings

# 获取模板ID
template_id = settings.TENCENT_SMS_TEMPLATE.get("login")

# 发送短信,result是腾讯返回的信息
result = send_message(mobile, random_code, template_id)
if not result:
	return JsonResponse({'status': False, 'error': '短信发送失败'})

setting设置

已在访问管理控制台 >API密钥管理页面获取 SecretID 和 SecretKey。

  • SecretID 用于标识 API 调用者的身份。
  • SecretKey 用于加密签名字符串和服务器端验证签名字符串的密钥,SecretKey 需妥善保管,避免泄露
# SecretID
TENCENT_SECRET_ID = "secretId"

# SecretKey
TENCENT_SECRET_KEY = "secretKey"

# 地域信息参数
TENCENT_CITY = "ap-guangzhou"

# 应用 ID 可前往 [短信控制台](https://console.cloud.tencent.com/smsv2/app-manage) 查看
TENCENT_APP_ID = "1400787878"

# 签名信息可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-sign)查看
TENCENT_SIGN = "腾讯云"

# 模板 ID 可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-template)查看
TENCENT_SMS_TEMPLATE = {
    'register': "449739",
    'login': "449739",
    'reset': "449739",
    'fall': "449739",
}
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhaggk
系列文章
更多 icon
同类精品
更多 icon
继续加载