twilio说一些调用者(node.js)

嘿家伙我正在做的事情,并有以下问题:

实际目标 – 如果有人打电话,我想让声音说:“尝试联系到某人”,然后从数组中调出一些号码。 (不能做那个ATM,因为我需要打个电话)

自动取款机的目的 – 这就是为什么我至less想要回答这个问题的人说什么,我可以肯定它会“工作”。

所以我伪造了一个电话,发送了twilio通过本地主机http服务器发送的url(如果得到一个电话)。 到目前为止好,我的电话被调用。 但是那个女人不说我想让她说什么…她说:谢谢你试试我们的文档,然后等着听音乐。

和:call.status是ALLWAYS排队,或者我不把它在正确的地方:/,请记住我的手机正在响,所以它应该至less有状态振铃…

这是我现在所拥有的:

requestHandler.js:

var querystring = require("querystring"); var emergency = require("./emergency"); var twilio = require('twilio'); function callRequest(response) { var resp = new twilio.TwimlResponse(); resp.say({voice:'woman'}, 'ahoy hoy! Testing Twilio and node.js'); console.log("call incomming ! EMERGENCY 1 1 11 !"); //emergency.handleIncommingCall(); response.writeHead(200, {"Content-Type": "text/xml"}); response.end(resp.toString()); } exports.callRequest = callRequest; 

server.js:

 var http = require("http"); var url = require("url"); function start(route, handle) { function onRequest(request, response) { var pathname = url.parse(request.url).pathname; console.log("Request for " + pathname + " received."); route(handle, pathname, response, request); } http.createServer(onRequest).listen(1337); console.log("Server has started"); } exports.start = start; exports.http = http; 

router.js:

 function route(handle, pathname, response, request) { console.log("About to route a request for " + pathname); if (typeof handle[pathname] === 'function') { handle[pathname](response, request); }else{ console.log("No request handler found for " + pathname); response.writeHead(404, {"Content-Type": "text/html"}); response.write("404 Not found"); response.end(); } } exports.route = route; 

index.js:

 var server = require("./server"); var router = require("./router"); var requestHandler = require("./requestHandler"); var handle = { }; handle["/demo.twilio.com/welcome/voice/"] = requestHandler.callRequest; server.start(router.route, handle); 

emergency.js:

 var twilio = require('twilio'); var accountSid = 'cant tell ya'; var authToken = "cant tell ya"; var client = require('twilio')(accountSid, authToken); var firstCascadeNumber = "cant tell ya"; var secondCascadeNumber; var privateNumber; //enter privateNumber here var twiml = new twilio.TwimlResponse(); function handleIncommingCall (){ //twilio should say : we contact team internet pls wait //twilio should make music call(1,firstCascadeNumber); //cb if staus ist nicht rangegangen call(2) } function call (cascade,cascadeNumber){ client.makeCall({ url: "http://demo.twilio.com/docs/voice.xml", to: cascadeNumber, from: "cant tell ya" }, function(err, call) { if(err){ console.log("ERROR:", err.message); }else{ console.log("calling " + cascadeNumber); console.log("status: " + call.status); if(cascade == 1){ //twiml.say("Hello this ia test. Thank you for calling."); console.log("should say what i want it to say ! god damn it "); console.log("status: " + call.status); //if user geht ran //startConference() //if user geht nicht ran //call(2,secondCascadeNumber) }else if(cascade == 2){ //if user geht ran //startConference() //if user geht nicht ran //inform caller that no one is there }else{ console.log("Error: cascade doesnt exsist"); } } }); } function openConference(call,from,to){ //call.joinConference([roomName, option, cbOnEnd]) } exports.handleIncommingCall = handleIncommingCall; 

Twilio开发者在这里传道。

你大部分的方式,但你没有把你的应用程序放在这里。

当你打电话时,你所得到的回叫只是指电话是否正确启动。 这不是一个callback,你需要返回TwiML来告诉Twilio如何处理这个调用。

相反,当Twilio拨打电话时,会发生一个HTTP请求到您在首次拨打电话时提供的URL。 该URL应该位于您的应用程序中,并可供Twilio使用。

这篇关于在Node.js中使用Twilio的博客文章应该能够向你展示我的意思,并为你设置一个本地testing的好方法。

编辑

感谢您更新您的代码。

你的问题是,你没有告诉Twilio一旦连接就问你该怎么做。

当您使用API​​创build呼叫时,您需要3个参数,要呼叫的号码,要呼叫的号码以及URL。 当Twilio连接呼叫时,它会向您提供的URL提出HTTP请求,询问下一步该做什么,这是您提供TwiML以告诉Twilio如何处理呼叫的地方。

目前你正在提供这个URL: http : //demo.twilio.com/docs/voice.xml

如果您点击演示Twilio URL,您将看到正在返回的TwiML,以及为什么您会听到您不期待的消息。 由于该URL不是您的,因此您的应用程序无法控制该呼叫。

您需要发送指向应用程序中路由的URL,并且该路由需要使用所需的TwiML进行响应。 您可以使用名为ngrok的工具将您的本地服务器暴露给Twilio,这将允许您对此进行testing。

我build议你遵循我之前链接的教程,本教程中使用ngrok来从Twilio发送传入的HTTP请求 ,本教程使用Twilio 创build点击呼叫应用程序 。

在你的应用程序的情况下,而不是试图处理“/demo.twilio.com/welcome/voice/”这是不是你可以控制的URL,你应该处理,说:

 var handle = { }; handle["/calls"] = requestHandler.callRequest; 

然后使用ngrok创build一个到你的应用程序的隧道,并将URL传递给client.makeCall像这样:

 function call (cascade,cascadeNumber){ client.makeCall({ url: "http://YOUR_NGROK_SUBDOMAIN.ngrok.io/calls", to: cascadeNumber, from: "cant tell ya" }, function(err, call) { if (err) { console.log("Call could not be made", err); } else { console.log("Call created successfully. Call ID:", call.sid); } } } 

让我知道如果有帮助。