语音消息回执---PHP-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

语音消息回执---PHP

猫饭先生 2017-10-25 11:56:30 1271


简介


当您使用语音的API接口发送外呼后,可以通过使用MNS的Queue模型来接收语音的回执消息。

消息的订阅


云通信的所有业务消息都用过MNS消息服务向外发送。用户每订阅一个类别的消息(比如语音呼叫消息VoiceReport),系统都会为用户分配一个独立的消息队列。用户可以通过阿里云账号拿到一个临时的token用于获取队列中的消息。用户可以下载demo,编写简单的消息处理类即可完成消息处理的任务。在页面上订阅消息,订阅完消息后,能拿到消息队列名称(queueName)。比如:Alicom-Queue-xxxxxx-VoiceReport 。队列名字每个用户都不同。

消息类型


语音提供一种消息类型,呼叫记录消息(VoiceReport)

语音呼叫记录消息VoiceReport消息体格式

名称类型描述示例是否必须
call_idString呼叫ID100001616500^100001871490必须
start_timeString通话开始时间,未接通则为空2017-06-01 10:00:00可选
end_timeString通话结束时间,未接通则为空2017-06-01 10:00:00可选
durationString通话时长,未接通为010可选
status_codeString呼叫结果状态码200010必须
status_msgString结果描述执行完成可选
out_idString扩展字段回传,将调用api时传入的字段返回123456可选
dtmfStringDTMF按键123456可选


技术对接步骤



下载消息SDK


下载对应语音的消息DEMO工程,工程所需要的所有依赖php包都放在DEMO工程的录下,将对于的php包引入到您的工程当中既可按照DEMO样例编写接收消息的程序。
SDK&DEMO[ 下载地址]

编写样例程序

  1. [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]ReceiveAlicomMsgDemo[backcolor=transparent] [backcolor=transparent]{
  2. [backcolor=transparent]    [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]function[backcolor=transparent] dealMessage[backcolor=transparent]([backcolor=transparent]$message[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  3. [backcolor=transparent]        echo $message[backcolor=transparent];
  4. [backcolor=transparent]        [backcolor=transparent]//TODO 这里开始写业务代码
  5. [backcolor=transparent]        [backcolor=transparent]return[backcolor=transparent] [backcolor=transparent]true[backcolor=transparent];[backcolor=transparent]//返回true,则工具类自动删除已拉取的消息。返回false,消息不删除可以下次获取
  6. [backcolor=transparent]    [backcolor=transparent]}
  7. [backcolor=transparent]    [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]function[backcolor=transparent] receiveMsg[backcolor=transparent](){
  8. [backcolor=transparent]        $accessKeyId [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"your_accessKeyId"[backcolor=transparent];
  9. [backcolor=transparent]        $accessKeySecret [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"your_accessKeySecret"[backcolor=transparent];
  10. [backcolor=transparent]        $messageType [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"VoiceReport"[backcolor=transparent];[backcolor=transparent]//短信回执:SmsReport,短息上行:SmsUp,语音呼叫:VoiceReport,流量直冲:FlowReport
  11. [backcolor=transparent]        $queueName [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"your_queueName"[backcolor=transparent];[backcolor=transparent] [backcolor=transparent]//在云通信页面开通相应业务消息后,就能在页面上获得对应的queueName
  12. [backcolor=transparent]        [backcolor=transparent]DefaultProfile[backcolor=transparent]::[backcolor=transparent]addEndpoint[backcolor=transparent]([backcolor=transparent]"cn-hangzhou"[backcolor=transparent],[backcolor=transparent]"cn-hangzhou"[backcolor=transparent],[backcolor=transparent]"Dybaseapi"[backcolor=transparent],[backcolor=transparent]"dybaseapi.aliyuncs.com"[backcolor=transparent]);
  13. [backcolor=transparent]        $tokenGetterForAlicom [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]TokenGetterForAlicom[backcolor=transparent]([backcolor=transparent]$accessKeyID[backcolor=transparent],[backcolor=transparent]$accessKeySecret[backcolor=transparent]);
  14. [backcolor=transparent]        $i [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]0[backcolor=transparent];
  15. [backcolor=transparent]        [backcolor=transparent]while[backcolor=transparent] [backcolor=transparent]([backcolor=transparent] $i [backcolor=transparent]<=[backcolor=transparent] [backcolor=transparent]3[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{[backcolor=transparent]//取回执消息失败5 次停止循环拉取
  16. [backcolor=transparent]            $i[backcolor=transparent]++;
  17. [backcolor=transparent]            [backcolor=transparent]try
  18. [backcolor=transparent]            [backcolor=transparent]{
  19. [backcolor=transparent]                $tokenForAlicom [backcolor=transparent]=[backcolor=transparent] $tokenGetterForAlicom[backcolor=transparent]->[backcolor=transparent]getToeknByMessageType[backcolor=transparent]([backcolor=transparent]$messageType[backcolor=transparent],[backcolor=transparent]$queueName[backcolor=transparent]);
  20. [backcolor=transparent]                $queue [backcolor=transparent]=[backcolor=transparent] $tokenForAlicom[backcolor=transparent]->[backcolor=transparent]getClient[backcolor=transparent]()->[backcolor=transparent]getQueueRef[backcolor=transparent]([backcolor=transparent]$queueName[backcolor=transparent]);
  21. [backcolor=transparent]                [backcolor=transparent]// 3. receive message
  22. [backcolor=transparent]                $receiptHandle [backcolor=transparent]=[backcolor=transparent] NULL[backcolor=transparent];
  23. [backcolor=transparent]                [backcolor=transparent]// when receiving messages, it's always a good practice to set the waitSeconds to be 30.
  24. [backcolor=transparent]                [backcolor=transparent]// it means to send one http-long-polling request which lasts 30 seconds at most.
  25. [backcolor=transparent]                $res [backcolor=transparent]=[backcolor=transparent] $queue[backcolor=transparent]->[backcolor=transparent]receiveMessage[backcolor=transparent]([backcolor=transparent]2[backcolor=transparent]);
  26. [backcolor=transparent]                echo [backcolor=transparent]"ReceiveMessage Succeed! \n"[backcolor=transparent];
  27. [backcolor=transparent]                $bodyMD5 [backcolor=transparent]=[backcolor=transparent] md5[backcolor=transparent]([backcolor=transparent]base64_encode[backcolor=transparent]([backcolor=transparent]$res[backcolor=transparent]->[backcolor=transparent]getMessageBody[backcolor=transparent]()));
  28. [backcolor=transparent]                $receiptHandle [backcolor=transparent]=[backcolor=transparent] $res[backcolor=transparent]->[backcolor=transparent]getReceiptHandle[backcolor=transparent]();
  29. [backcolor=transparent]                [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]strtoupper[backcolor=transparent]([backcolor=transparent]$bodyMD5[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]==[backcolor=transparent] $res[backcolor=transparent]->[backcolor=transparent]getMessageBodyMD5[backcolor=transparent]())
  30. [backcolor=transparent]                [backcolor=transparent]{
  31. [backcolor=transparent]                    [backcolor=transparent]if[backcolor=transparent]([backcolor=transparent]$this[backcolor=transparent]->[backcolor=transparent]dealMessage[backcolor=transparent]([backcolor=transparent]$res[backcolor=transparent]->[backcolor=transparent]getMessageBody[backcolor=transparent]())){
  32. [backcolor=transparent]                        $res [backcolor=transparent]=[backcolor=transparent] $queue[backcolor=transparent]->[backcolor=transparent]deleteMessage[backcolor=transparent]([backcolor=transparent]$receiptHandle[backcolor=transparent]);
  33. [backcolor=transparent]                        echo [backcolor=transparent]"DeleteMessage Succeed! \n"[backcolor=transparent];
  34. [backcolor=transparent]                    [backcolor=transparent]}
  35. [backcolor=transparent]                [backcolor=transparent]}
  36. [backcolor=transparent]            [backcolor=transparent]}
  37. [backcolor=transparent]            [backcolor=transparent]catch[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]MnsException[backcolor=transparent] $e[backcolor=transparent])
  38. [backcolor=transparent]            [backcolor=transparent]{
  39. [backcolor=transparent]                echo [backcolor=transparent]"ex:"[backcolor=transparent].([backcolor=transparent]$e[backcolor=transparent]->[backcolor=transparent]getMnsErrorCode[backcolor=transparent]())[backcolor=transparent] [backcolor=transparent];
  40. [backcolor=transparent]                echo [backcolor=transparent]"ReceiveMessage Failed: "[backcolor=transparent] [backcolor=transparent].[backcolor=transparent] $e[backcolor=transparent];
  41. [backcolor=transparent]                echo [backcolor=transparent]"<br>"[backcolor=transparent];
  42. [backcolor=transparent]                [backcolor=transparent]#return;
  43. [backcolor=transparent]            [backcolor=transparent]}
  44. [backcolor=transparent]        [backcolor=transparent]}
  45. [backcolor=transparent]    [backcolor=transparent]}
  46. [backcolor=transparent]}
  47. [backcolor=transparent]$demo [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent]  [backcolor=transparent]ReceiveAlicomMsgDemo[backcolor=transparent]();
  48. [backcolor=transparent]$demo[backcolor=transparent]->[backcolor=transparent]receiveMsg[backcolor=transparent]();
  49. [backcolor=transparent]echo [backcolor=transparent]"<br>end"[backcolor=transparent];
消息中间件 API PHP 开发工具
分享到
取消 提交回答
全部回答(0)
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题