ColdFusion对接Vonage呼入语音API:NCCO对象及事件内容类型问题
问题详情
问题1:单动作Talk NCCO播放正常,但事件中出现内容类型错误
已通过Webhook接收呼入呼叫,使用单动作Talk的NCCO可正常播放语音,但事件日志中出现错误:
"Unexpected content type: text/html; charset=UTF-8"
对应代码片段:
<cfset ncco = ' { "action": "talk", "text": "Press 1, for maybe, and 2, for not sure, followed by the hash key.", "language": "en-US", "style": 6, "bargeIn": true }'> <cfhttp method="PUT" url="https://api.nexmo.com/v1/calls/#responsetxt.uuid#/talk" result="httpResponse"> <cfhttpparam type="header" name="Content-Type" value="application/json" /> <cfhttpparam type="header" name="Authorization" value="Bearer #jwtToken#" /> <cfhttpparam type="body" value='#ncco#' /> </cfhttp>
问题2:多动作NCCO在呼出正常,但呼入时呼叫直接断开
包含record、talk、input的多动作NCCO在呼出呼叫中可正常实现录音和转录,但用于处理呼入呼叫时,呼叫会直接断开。
多动作NCCO代码:
<cfset ncco = [ { "action": "record", "eventUrl": ["https://example.com/virrtualcall/input.cfm"], "transcription": { "eventMethod": "POST", "eventUrl":["https://example.com.com/virrtualcall/input.cfm"], "language": "en-US", "sentimentAnalysis": "true" } }, { "action": "talk", "text": "Please let us know what is your problem", "language": "en-US", "style": 6, "premium": true, "bargeIn": true }, { "action": "input", "type": [ "dtmf" ], "dtmf": { "submitOnHash": true, "timeOut": 10 } }, { "action": "talk", "text": "Thanks for your response, goodbye.", "language": "en-US", "style": 2 } ]>
呼出时正常使用该NCCO的代码:
<cfhttp method="POST" url="https://api.nexmo.com/v1/calls" result="httpResponse"> <cfhttpparam type="header" name="Content-Type" value="application/json" /> <cfhttpparam type="header" name="Authorization" value="Bearer #jwtToken#" /> <cfhttpparam type="body" value='{"to":[{"type":"phone","number":"#toNumber#"}],"from":{"type":"phone","number":"#fromNumber#"},"ncco":#serializeJSON(ncco)#}' /> </cfhttp>
解决方案
针对问题1的修复
- 避免手动拼接JSON字符串:改用ColdFusion结构体定义NCCO,再通过
serializeJSON()生成标准JSON,避免手动编写可能出现的语法错误或格式问题:<cfset nccoStruct = { action: "talk", text: "Press 1, for maybe, and 2, for not sure, followed by the hash key.", language: "en-US", style: 6, bargeIn: true }> <cfset ncco = serializeJSON(nccoStruct)> - 确认请求头生效:检查ColdFusion是否正确设置了
Content-Type: application/json,部分环境下需确保没有其他代码覆盖该请求头。 - 排查接口响应:错误提示中的
text/html通常是接口返回了错误页面(比如JWT无效、呼叫UUID错误),可输出httpResponse查看具体响应内容,验证令牌和UUID的有效性。
针对问题2的修复
- 设置Webhook响应头为application/json:呼入场景下,Webhook接口必须返回JSON格式的NCCO,且明确设置响应头
Content-Type: application/json,ColdFusion默认返回text/html会导致Vonage无法解析NCCO,呼叫直接断开。添加以下代码到Webhook页面开头:<cfheader name="Content-Type" value="application/json; charset=UTF-8"> - 修正NCCO中的错误链接:多动作NCCO的
transcription.eventUrl存在域名错误(example.com.com),修正为正确的https://example.com/virrtualcall/input.cfm,无效的eventUrl会触发呼叫中断。 - 确保Webhook端点可正常访问:验证
input.cfm能处理POST请求,且HTTPS证书有效(Vonage不接受自签名证书),避免因事件回调失败导致呼叫终止。 - 标准化NCCO输出:呼入Webhook返回NCCO时,使用
serializeJSON()生成标准JSON,确保格式正确:<cfoutput>#serializeJSON(ncco)#</cfoutput>
内容的提问来源于stack exchange,提问作者user3215061
相关产品推荐
相关产品推荐

