腾讯云——语音消息

文章目录


前言

使用腾讯云语音消息之前需要安装sdk,本文以python sdk为例

python 复制代码
pip install tencentcloud-sdk-python

一、语音消息配置

创建一个配置文件

tencentVoicePhoneConfig.py

python 复制代码
# 腾讯云语音消息配置

# 地域信息
# 华北地区(北京) ap-beijing      华南地区(广州) ap-guangzhou
REGION = "ap-guangzhou"

# 实例化一个 http 选项,可选,无特殊需求时可以跳过
# POST 请求(默认为 POST 请求)
REQMETHOD = "POST"

# 请求超时时间,单位为秒(默认60秒)
REQTIMEOUT = 30

# 指定接入地域域名(默认就近接入)
ENDPOINT = "vms.tencentcloudapi.com"

# 实例化一个客户端配置对象,可以指定超时时间等配置
# 指定算法签名
SIGNMETHOD = "TC3-HMAC-SHA256"

# 指定接口返回的语言
LANGUAGE = "en-US"

# 国家码或地区码
COUNTRYCODE = "86"

# 用户session
SESSIONCONTEXT = ""

# 播放次数,可选,最多3次,默认2次
PLAYTIMES = 2

二、语音消息使用

注意secretId和secretKey被放入到了数据库中

secretId和secretKey的获取:

已开通语音消息服务,具体操作请参见 快速入门。

已准备依赖环境:Python 2.7, 3.6-3.9 版本。

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

SecretID 用于标识 API 调用者的身份。

SecretKey 用于加密签名字符串和服务器端验证签名字符串的密钥,SecretKey 需妥善保管,避免泄露。

语音消息的调用地址为 vms.tencentcloudapi.com

语音消息官方文档

tencent_voice_phone.py

注意paramset需要根据你申请的语音消息格式来确定

python 复制代码
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import (
    TencentCloudSDKException,
)

# 导入 VMS 模块的 client models
from tencentcloud.vms.v20200902 import vms_client, models

# 导入可选配置类
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from apps.system.models import Profile
from ops_monitor_admin.tencentVoicePhoneConfig import *
import logging

error_logger = logging.getLogger('tencent-error')
info_logger = logging.getLogger('tencent')

class TencentPhone():

    def __init__(self):
    	# 注意此处的cred 便是通过从数据库取出了secretId、secretKey
        self.tencent_config = Profile.objects.filter(name="tencent_voice_phone").first().configuration
        cred = credential.Credential(
            self.tencent_config.get("secretId"), self.tencent_config.get("secretKey")
        )
        clientProfile = self.create_http_profile()
        self.client = vms_client.VmsClient(cred, REGION, clientProfile)

    def create_http_profile(self):
        # 实例化一个 http 选项,可选,无特殊需求时可以跳过
        httpProfile = HttpProfile()
        httpProfile.reqMethod = REQMETHOD
        httpProfile.reqTimeout = REQTIMEOUT
        httpProfile.endpoint = ENDPOINT

        # 非必要步骤:
        # 实例化一个客户端配置对象,可以指定超时时间等配置
        clientProfile = ClientProfile()
        clientProfile.signMethod = SIGNMETHOD
        clientProfile.language = LANGUAGE
        clientProfile.httpProfile = httpProfile

        return clientProfile

    def send_voice_phone(self, paramset, phone):
        try:
            req = models.SendTtsVoiceRequest()

            req.TemplateId = self.tencent_config.get("TemplateId")
            req.TemplateParamSet = paramset
            req.CalledNumber = "+" + COUNTRYCODE + phone
            req.VoiceSdkAppid = self.tencent_config.get("VoiceSdkAppid")
            req.PlayTimes = PLAYTIMES
            req.SessionContext = SESSIONCONTEXT

            # 通过 client 对象调用 SendTtsVoice 方法发起请求,注意方法名与请求的对象是对应的
            rep = self.client.SendTtsVoice(req)
            if rep["Response"].get("Error"):
                error_logger.error(str({"code": rep["Response"]["code"], "msg": rep["Response"]["msg"], "paramset": paramset}))
            else:
                info_logger.info(str(rep) + str(paramset))

        except TencentCloudSDKException as e:
            error_logger.error(str(e))


tencent_phone = TencentPhone()

def create_voice_phone(paramset, mobile):
    # 各成员语音电话发信
    try:
        tencent_phone.send_voice_phone(paramset, mobile)
    except Exception as e:
        error_logger.error(str(e))

具体使用时只需要引入tencent_voice_phone.py中的create_voice_phone方法即可

python 复制代码
from tencent_voice_phone.py import create_voice_phone
paramset = [参数1, 参数2, 参数3 ....]# 根据申请格式来 
create_voice_phone(paramset, mobile) # mobile为手机号
相关推荐
@HNUSTer2 小时前
基于 GEE 的 MODIS 昼夜地表温度数据可视化与导出全流程解决方案
云计算·数据集·遥感大数据·gee·云平台·modis·地表温度(lst)
王道长服务器 | 亚马逊云4 小时前
AWS WAF 实战篇|如何防御爬虫、CC攻击与恶意POST请求
云计算·aws
@HNUSTer5 小时前
基于 GEE MODIS 数据实现 7 大遥感指数计算与可视化
云计算·数据集·遥感大数据·gee·云平台·modis·遥感指数
TG_yunshuguoji21 小时前
阿里云国际代理商:有哪些文件适合阿里云CDN分发?
阿里云·云计算
哲霖软件1 天前
什么是机械设备制造ERP?哲霖软件如何助力企业实现降本增效?
云计算
光影少年1 天前
云计算生态及学习方向和就业领域方向
学习·云计算
Akamai中国2 天前
Linebreak赋能实时化企业转型:专业系统集成商携手Akamai以实时智能革新企业运营
人工智能·云计算·云服务
2401_865382502 天前
工信部发布→《云计算综合标准化体系建设指南(2025版)》
网络安全·云计算·信息安全技术
云宏信息2 天前
【深度解析】VMware替代的关键一环:云宏ROW快照如何实现高频业务下的“无感”数据保护?
服务器·网络·数据库·架构·云计算·快照
wanhengidc2 天前
如何使用云手机进行游戏挂机?
运维·服务器·游戏·智能手机·云计算