2016-02-12 42 views
2

嗨我工作的東西,有以下問題:twilio說些什麼,主叫方(node.js的)

實際AIM - 如果有人打電話,我想讓聲音說:「力爭達到某人」然後從數組中調用一些數字。 (不能做那個atm,因爲我需要打個電話)

ATM AIM - 這就是爲什麼我至少想對那些回答Twilio電話的人說些什麼,以便我確信它會「工作」。

因此,我通過發送twilio將通過本地主機http服務器發送的url(如果它獲得一個調用)來僞造一個調用。目前爲止這麼好,我的電話被叫了。但是那個女人不說我想讓她說什麼...她說:謝謝你試用我們的文檔,然後等待音樂。

AND:該call.status是ALLWAYS排隊,或者我不在正確的地方抓住它:/,記住我的電話響起來,因此至少應具有的地位響起......

這是什麼我在那一刻:

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 i a 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; 

回答

0

Twilio開發者傳道這裏。

你大部分都是在那裏,但是你沒有把你的應用程序設置在這裏。

當您撥打電話時,您所得到的回撥僅指撥打電話是否正確開始。這不是一個回調,你需要返回TwiML來告訴Twilio如何處理這個調用。

相反,當Twilio發起呼叫時,會發送一個HTTP請求到您在首次撥打電話時提供的URL。該URL應該位於您的應用程序中,並可供Twilio使用。

這篇關於using Twilio with Node.js的博客文章應該能夠向您展示我所說的全部內容,併爲您提供一種在本地測試此功能的好方法。

編輯

感謝您更新的代碼。

你的問題是,你不告訴Twilio一旦連接就問你該怎麼做。

當您使用API​​創建呼叫時,您需要3個參數,要呼叫的號碼,要呼叫的號碼以及URL。當Twilio連接呼叫時,它會向您提供的URL請求HTTP請求,詢問下一步該做什麼,這是您提供一些TwiML以告訴Twilio如何處理呼叫的地方。

目前你提供這個網址:http://demo.twilio.com/docs/voice.xml

如果通過演示Twilio URL點擊那裏,你會看到正在返回TwiML,爲什麼你聽到一個消息,你是不是期待。由於該URL不是您的,因此您的應用程序無法控制該呼叫。

您需要發送一個指向應用程序中路由的URL,並且該路由需要使用所需的TwiML進行響應。您可以使用名爲ngrok的工具將您的本地服務器暴露給Twilio,這將允許您對此進行測試。

我建議你按照我之前鏈接的教程,本教程using ngrok to text your incoming HTTP requests from Twiliotutorial on creating a click to call application with Twilio

,而不是試圖處理「/demo.twilio.com/welcome/voice/」這是不是你的,你應該處理,控制URL

在應用程序的情況下,說:

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

然後用ngrok創建隧道到您的應用程序和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); 
     } 
    } 
} 

讓我知道是否有幫助。

+0

所以我做了教程,我實際上得到了xml文檔,但是如果我得到這個調用仍然會得到同樣的問題......有一個想法爲什麼? – nova

+0

你現在的代碼是什麼樣的?您是否使用ngrok將您的本地Web服務器公開到Internet? – philnash

+0

你能用你當前的代碼編輯你的問題嗎? – philnash