今天发现在一个开放开发平台,可以通过RestAPI 的方式开发自己语音机器人和呼叫中心系统。
这是不适用官方的AI引擎自己做简单测试,使用官方AI机器人引擎3.0下次有时间再上传步骤。
1、去http://open.astyun.com/user/reg.html 注册一个账号,实名通过后,建立以一个应用
2、看看他提供接口在http://api.astyun.com/index.php
include_once("../SDK/AstyunRestSDK.php"); // 调用官方群里的 sdk
//主帐号
$accountSid= 'abcdefghijklmnopqrstuvwxyz012345';
//主帐号Token
$accountToken= '';
//应用Id
$appId='';
//请求地址,格式如下,不需要写https://
$serverIP='app.astyun.com';
//请求端口
$serverPort='8433';
//REST版本号
$softVersion='2017-04-10';
/**
* IVR外呼
* @param number 待呼叫号码,为Dial节点的属性
* @param userdata 用户数据,在<startservice>通知中返回,只允许填写数字字符,为Dial节点的属性
* @param record 是否录音,可填项为true和false,默认值为false不录音,为Dial节点的属性
*/
function ivrDial($number,$userdata,$record)
{
// 初始化REST SDK
global $accountSid,$accountToken,$appId,$serverIP,$serverPort,$softVersion;
$rest = new REST($serverIP,$serverPort,$softVersion);
$rest->setAccount($accountSid,$accountToken);
$rest->setAppId($appId);
// 调用IVR外呼接口
$result = $rest->ivrDial($number,$userdata,$record);
if($result == NULL ) {
echo "result error!";
return;
}
echo $result;
}
ivrDial("18173142287",$userdata,"false"); // 发起呼叫指令
发起呼叫有。还需要建立自己 接收开放平台推送的消息服务(这个地址在主账号里面可以自己设置) 进行 IVR 互动 直接上代码
<?php
error_reporting(2);
//url示例:http://localhost/?startservice&appid=qw
$ref=$_SERVER['REQUEST_URI'] ; //云通讯回调请求页面的URI
if(strpos($ref,'startservice')){
//云通讯IVR启动业务的回调请求,请求的url中固定相对地址startservice
startservice();
}elseif(strpos($ref,'stopservice')){//云通讯IVR结束业务的回调请求,请求的url中固定相对地址stopservice
stopservice();
}
elseif(strpos($ref,'enterflow')){//云通讯坐席状态改变的回调请求,IVR 入口流程
enterflow();
}
elseif(strpos($ref,'fangchanwanliuflow')){
fangchanwanliuflow();
}
elseif(strpos($ref,'fangchanflow')){
fangchanflow();
}
elseif(strpos($ref,'yuyinshibie')){
yuyinshibie();
}
elseif(strpos($ref,'palyyuyinshibie')){
palyyuyinshibie();
}
//IVR启动业务,在IVR呼入或者IVR外呼后由云通讯平台发起的回调请求
function startservice()
{
//获取参数
$appid = $_REQUEST['appid']; //应用id
$callid = $_REQUEST['callid']; //通话id,由云通讯平台产生的一路通话的唯一标识
$from = $_REQUEST['from']; //主叫号码,当IVR呼入方式为主叫的号码,当IVR外呼的方式则为空
$to = $_REQUEST['to']; //被叫号码,当IVR呼入方式为IVR接入号,当IVR外呼的方式则为外呼的号码
$direction = $_REQUEST['direction']; //呼叫方式,IVR呼入为0,IVR外呼为1
$userdata = $_REQUEST['userdata'];
//自定义参数,只有发起当外呼请求中带有才会返回,可以通过该参数和callid绑定来确定发起的请求
enterflow();
}
//IVR结束业务,在用户挂机后由云通讯平台发起的回调请求
function stopservice()
{
//获取参数
$appid = $_REQUEST['appid']; //应用id
$callid = $_REQUEST['callid']; //通话id,由云通讯平台产生的一路通话的唯一标识
$starttime = $_REQUEST['starttime']; //主叫通话的开始时间
$endtime = $_REQUEST['endtime']; //主叫通话的结束时间
$callduration = $_REQUEST['callduration']; //主叫通话时长
$sstime = $_REQUEST['sstime']; //被叫通话的开始时间
$setime = $_REQUEST['setime']; //被叫通话的结束时间
$seccallduration = $_REQUEST['seccallduration']; //被叫通话时间
$recordurl = $_REQUEST['recordurl']; //录音文件下载地址
$recordid = $_REQUEST['recordid']; //录音文件Id
$errorcode = $_REQUEST['errorcode']; //通话中异常的错误值,可以根据错误知道出现异常的原因
//对云通讯平台回调请求的响应包体,因为不需要后续流程响应空命令
echo "<?xml version='1.0' encoding='UTF-8' ?>
<Response>
<CmdNone/>
</Response>";
}
#主流程
function enterflow()
{
echo "<?xml version='1.0' encoding='UTF-8'?> \
<Response><PlayAsr action='selectflow' tts='true' asr_appid='5a54bbcc'
playfilename='请说房产、酒水、语音识别 进入测试模块'/>
<Redirect>enterflow</Redirect></Response>";
}
#流程选择
#请问你要进入哪个测试流程,比如,房产推销,语音识别测试
function selectflow()
{
$message = $_REQUEST['asr_text'];
$arr = explode(";",$message);
$asr_text="";
foreach($arr as $val){
$asr_text .= substr(strstr($val,"."),1);
}
if(strstr($asr_text,"房") || strstr($asr_text,"产") || strstr($asr_text,"推") ||
strstr($asr_text,"销")){
#欢迎进入房产话术测试流程,现在开始测试,先生你好,我是售楼部的,请问你最近有打算买房吗
echo "<?xml version='1.0' encoding='UTF-8'?> \
<Response><PlayAsr action='fangchanflow' asr_endkeys=''
asr_appid='5a54bbcc' playfilename='c:/voc/fangchan.wav'/>
<Redirect>enterflow</Redirect></Response>";
}
else if( strstr($asr_text,"语") || strstr($asr_text,"语音") ||
strstr($asr_text,"别") || strstr($asr_text,"识") || strstr($asr_text,"识别")){
#现在开始语音识别测试吧,你随便说点什么吧,比如早上好,我爱中国,需要买房吗,等等
echo "<?xml version='1.0' encoding='UTF-8'?> \
<Response><PlayAsr action='yuyinshibie' asr_endkeys=''
asr_appid='5a54bbcc' playfilename='c:/voc/yuyinceshi.wav'/>
<Redirect>enterflow</Redirect></Response>";
}
else
{
#刚刚没听清,请问你要进入哪个测试流程,比如,房产推销,语音识别测试
echo "<?xml version='1.0' encoding='UTF-8'?> \
<Response><PlayAsr action='selectflow' asr_endkeys='' asr_appid='5a54bbcc'
playfilename='c:/voc/notlisten.wav'/>
<Redirect>enterflow</Redirect></Response>";
}
}
#房产挽留流程
function fangchanwanliuflow()
{
$message = $_REQUEST['asr_text'];
$arr = explode(";",$message);
$asr_text="";
foreach($arr as $val){
$asr_text .= substr(strstr($val,"."),1);
}
#我们楼盘最近准备开盘,位置非常好,开盘有优惠活动,你需要了解一下吗
echo "<?xml version='1.0' encoding='UTF-8'?> \
<Response><PlayAsr action='fangchanflow' asr_endkeys=''
asr_appid='5a54bbcc' playfilename='c:/voc/fangchanwanliu1.wav'/>
<Redirect>enterflow</Redirect></Response>";
}
#房产流程
function fangchanflow()
{
$message = $_REQUEST['asr_text'];
$arr = explode(";",$message);
$asr_text="";
foreach($arr as $val){
$asr_text .= substr(strstr($val,"."),1);
}
if(strstr($asr_text,"不") || strstr($asr_text,"没") || strstr($asr_text,"再") ||
strstr($asr_text,"拜") || strstr($asr_text,"挂") ){
#我们最近有一个学区房准备开盘,位置非常好,开盘有优惠活动,你都不考虑一下吗
echo "<?xml version='1.0' encoding='UTF-8'?> \
<Response><PlayAsr action='fangchanwanliuflow' asr_endkeys=''
asr_appid='5a54bbcc' playfilename='c:/voc/fangchanwanliu2.wav'/>
<Redirect>enterflow</Redirect></Response>";
}
else if(strstr($asr_text,"有") || strstr($asr_text,"嗯") ||
strstr($asr_text,"要") || strstr($asr_text,"ok") || strstr($asr_text,"可以") ||
strstr($asr_text,"好") || strstr($asr_text,"行") || strstr($asr_text,"中") ||
strstr($asr_text,"想")){
#好的,我等下把我的微信号通过短信发给你,你加一下我的微信号,我通过微信发送优惠信息给你,谢谢,祝你生活愉快
echo "<?xml version='1.0' encoding='UTF-8'?> \
<Response><PlayAsr action='enterflow' asr_endkeys='' asr_appid='5a54bbcc'
playfilename='c:/voc/fangchanok.wav'/>
<Redirect>enterflow</Redirect></Response>";
}
else if(strstr($asr_text,"钱") || strstr($asr_text,"价") ||
strstr($asr_text,"各") || strstr($asr_text,"贵") || strstr($asr_text,"咋") ||
strstr($asr_text,"优惠") || strstr($asr_text,"均") || strstr($asr_text,"平") ||
strstr($asr_text,"米")){
#价格面谈,你都不考虑一下吗
echo "<?xml version='1.0' encoding='UTF-8'?> \
<Response><PlayAsr action='fangchanflow' asr_endkeys=''
asr_appid='5a54bbcc' playfilename='c:/voc/fangchanjiage.wav'/>
<Redirect>enterflow</Redirect></Response>";
}
else if(strstr($asr_text,"那") || strstr($asr_text,"哪里") ||
strstr($asr_text,"哪") || strstr($asr_text,"位置") || strstr($asr_text,"地方") ||
strstr($asr_text,"片") || strstr($asr_text,"偏") || strstr($asr_text,"市") ||
strstr($asr_text,"区")){
#位置很好,你都不考虑一下吗
echo "<?xml version='1.0' encoding='UTF-8'?> \
<Response><PlayAsr action='fangchanflow' asr_endkeys=''
asr_appid='5a54bbcc' playfilename='c:/voc/fangchanweizhi.wav'/>
<Redirect>enterflow</Redirect></Response>";
}
else
{
#我们楼盘最近准备开盘,位置非常好,开盘有优惠活动,你需要了解一下吗
echo "<?xml version='1.0' encoding='UTF-8'?> \
<Response><PlayAsr action='fangchanflow' asr_endkeys=''
asr_appid='5a54bbcc' playfilename='c:/voc/fangchanwanliu1.wav'/>
<Redirect>enterflow</Redirect></Response>";
}
}
#语音识别结果
function palyyuyinshibie()
{
$message = $_REQUEST['asr_text'];
$arr = explode(";",$message);
$asr_text="";
foreach($arr as $val){
$asr_text .= substr(strstr($val,"."),1);
}
$orig = "刚刚的识别结果是, " . $asr_text;
echo "<?xml version='1.0' encoding='UTF-8' ?>
<Response>
<PlayTTS>$orig</PlayTTS>
<Redirect>reyuyinshibie</Redirect>
</Response>";
}
#语音识别流程
function reyuyinshibie()
{
#请继续说话测试吧
echo "<?xml version='1.0' encoding='UTF-8'?> \
<Response><PlayAsr action='yuyinshibie' asr_endkeys=''
asr_appid='5a54bbcc' playfilename='c:/voc/yuyinshibie.wav'/>
<Redirect>enterflow</Redirect></Response>";
}
?>
这样就完成 呼通电话 欢迎你进入语音机器人演示系统,请说房产、酒水、语音识别 进入测试模块。
热门工具 换一换