腾讯云——语音消息

文章目录


前言

使用腾讯云语音消息之前需要安装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为手机号
相关推荐
容器魔方19 小时前
Bloomberg 正式加入 Karmada 用户组!
云原生·容器·云计算
AKAMAI2 天前
Sport Network 凭借 Akamai 实现卓越成就
人工智能·云原生·云计算
10岁的博客2 天前
《云计算如何驱动企业数字化转型:关键技术与实践案例》
云计算
m0_694845573 天前
教你使用服务器如何搭建数据库
linux·运维·服务器·数据库·云计算
shinelord明3 天前
【数据行业发展】可信数据空间~数据价值的新型基础设施
大数据·架构·云计算·创业创新
XINVRY-FPGA3 天前
XCKU15P-2FFVA1760I AMD 赛灵思 Xilinx Kintex UltraScale+ FPGA
arm开发·嵌入式硬件·阿里云·fpga开发·云计算·硬件工程·fpga
王道长服务器 | 亚马逊云3 天前
一个迁移案例:从传统 IDC 到 AWS 的真实对比
java·spring boot·git·云计算·github·dubbo·aws
世间小小鱼3 天前
【爬坑指南】亚马逊文件中心 AWS S3 预签名URL 前端直传
前端·云计算·aws
TG_yunshuguoji3 天前
亚马逊云代理商:AWS亚马逊云的独特优势与实用价值
服务器·云计算·aws
阿雄不会写代码3 天前
AWS strands agents 当智能体作为独立服务/容器部署时,它们无法共享进程内状态
云计算·aws