python 调用云语音服务,文本模板转语音通知
阿里云语音服务接口
语音产品相关的SDK及DEMO程序
文件清单:
-
api_demo(语音服务API接口调用DEMO工程)
-
api_sdk(语音服务API接口依赖的SDK)
-
msg_demo(语音回执消息的DEMO)
-
msg_sdk(语音回执消息的SDK)
对于我们来说,只要用到api_demo文件夹即可。里面的aliyun-python-sdk-core也没用,这个是给python2安装sdk用的;
对于我们python3来说,直接安装核心库pip install aliyun-python-sdk-core-v3即可
其实还需要安装一个 pip install aliyun-python-sdk-dyvmsapi==3.2.0
我们实际只使用到
#!/usr/bin/env python #-*- coding:utf-8 -*- #Author:lzd # import sys import importlib,sys from aliyunsdkdyvmsapi.request.v20170525 import SingleCallByTtsRequest # from aliyunsdkdyvmsapi.request.v20170525 import SingleCallByVoiceRequest from aliyunsdkcore.client import AcsClient import uuid from aliyunsdkcore.profile import region_provider """ 语音业务调用接口示例,版本号:v20170525 Created on 2017-06-12 @author: seven """ # reload(sys) importlib.reload(sys) # sys.setdefaultencoding('utf8') # 注意:不要更改 REGION = "cn-hangzhou" PRODUCT_NAME = "Dyvmsapi" DOMAIN = "dyvmsapi.aliyuncs.com" # ACCESS_KEY_ID/ACCESS_KEY_SECRET 根据实际申请的账号信息进行替换 ACCESS_KEY_ID = "xxxxxxxxxxxxxx" ACCESS_KEY_SECRET = "xxxxxxxxxxxxxxxx" acs_client = AcsClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET, REGION) region_provider.add_endpoint(PRODUCT_NAME,REGION,DOMAIN) def tts_call(business_id, called_number, called_show_number, tts_code, tts_param=None): ttsRequest = SingleCallByTtsRequest.SingleCallByTtsRequest() # 申请的语音通知tts模板编码,必填 ttsRequest.set_TtsCode(tts_code) # 设置业务请求流水号,必填。后端服务基于此标识区分是否重复请求的判断 ttsRequest.set_OutId(business_id) # 语音通知的被叫号码,必填。 ttsRequest.set_CalledNumber(called_number) # 语音通知显示号码,必填,阿里云外拨显示号码 不可必填。 ttsRequest.set_CalledShowNumber(called_show_number) # tts模板变量参数 if tts_param is not None: ttsRequest.set_TtsParam(tts_param) # 调用tts文本呼叫接口,返回json ttsResponse = acs_client.do_action_with_exception(ttsRequest) # TODO 业务处理 return ttsResponse __name__ = 'tts' if __name__ == 'tts': __business_id = uuid.uuid1() print(__business_id) params = "{\"weizhi\":\"杭州机房\",\"name\":\"测试服务器\",\"title\":\"测试\"}" print((tts_call(__business_id, "13900000000", "0125456655", "TTS_4564654", params)).decode(encoding='utf-8'))
阿里云短信服务接口
https://help.aliyun.com/document_detail/55491.html?spm=a2c4g.11186623.6.616.fbfa6540keFjAB
# -*- coding: utf-8 -*- import sys from aliyunsdkdysmsapi.request.v20170525 import SendSmsRequest from aliyunsdkdysmsapi.request.v20170525 import QuerySendDetailsRequest from aliyunsdkcore.client import AcsClient import uuid from aliyunsdkcore.profile import region_provider from aliyunsdkcore.http import method_type as MT from aliyunsdkcore.http import format_type as FT import const """ 短信业务调用接口示例,版本号:v20170525 Created on 2017-06-12 """ try: reload(sys) sys.setdefaultencoding('utf8') except NameError: pass except Exception as err: raise err # 注意:不要更改 REGION = "cn-hangzhou" PRODUCT_NAME = "Dysmsapi" DOMAIN = "dysmsapi.aliyuncs.com" acs_client = AcsClient(const.ACCESS_KEY_ID, const.ACCESS_KEY_SECRET, REGION) region_provider.add_endpoint(PRODUCT_NAME, REGION, DOMAIN) def send_sms(business_id, phone_numbers, sign_name, template_code, template_param=None): smsRequest = SendSmsRequest.SendSmsRequest() # 申请的短信模板编码,必填 smsRequest.set_TemplateCode(template_code) # 短信模板变量参数 if template_param is not None: smsRequest.set_TemplateParam(template_param) # 设置业务请求流水号,必填。 smsRequest.set_OutId(business_id) # 短信签名 smsRequest.set_SignName(sign_name) # 数据提交方式 # smsRequest.set_method(MT.POST) # 数据提交格式 # smsRequest.set_accept_format(FT.JSON) # 短信发送的号码列表,必填。 smsRequest.set_PhoneNumbers(phone_numbers) # 调用短信发送接口,返回json smsResponse = acs_client.do_action_with_exception(smsRequest) # TODO 业务处理 return smsResponse if __name__ == '__main__': __business_id = uuid.uuid1() #print(__business_id) params = "{\"code\":\"12345\",\"product\":\"云通信\"}" #params = u'{"name":"wqb","code":"12345678","address":"bz","phone":"13000000000"}' print(send_sms(__business_id, "13000000000", "云通信测试", "SMS_5250008", params))
阿里云查看通话详情接口
依赖下载 pip install aliyun-python-sdk-dyvmsapi==3.2.0
#!/usr/bin/env python #coding=utf-8 from aliyunsdkcore.client import AcsClient from aliyunsdkcore.acs_exception.exceptions import ClientException from aliyunsdkcore.acs_exception.exceptions import ServerException from aliyunsdkdyvmsapi.request.v20170525.QueryCallDetailByCallIdRequest import QueryCallDetailByCallIdRequest client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-hangzhou') request = QueryCallDetailByCallIdRequest() request.set_accept_format('json') request.set_CallId("129788714830^116567684834") request.set_ProdId("11000000300006") request.set_QueryDate("1657103351000") response = client.do_action_with_exception(request) # python2: print(response) print(str(response, encoding='utf-8'))
每天逼着自己写点东西,终有一天会为自己的变化感动的。这是一个潜移默化的过程,每天坚持编编故事,自己不知不觉就会拥有故事人物的特质的。 Explicit is better than implicit.(清楚优于含糊)