2016-03-16 33 views
0

我的程序計劃:有人打電話給Twilio號碼,然後我的手機就會打電話。只要我不接受電話,來電者應該聽到音樂或類似的東西,如果我拿起,應該開始會議。如何用twilio開始會議?

目前: 有人正在撥打電話號碼,然後排隊等候音樂,然後我的手機就會被叫醒。聽起來不錯,但是當我拿起時,我們沒有連接。

所以我想我沒有得到妥如何Twilio會議的作品,你可能會有一些建議如何一步一步完成這個。

回答

2

Twilio開發商傳道這裏自動啓動。

好的,您正在構建Node.js,我知道我已經在其他問題中看到過您的代碼,但我將從頭開始創建這個代碼。

隨着Twilio你描述的流程應該看起來像這樣。

  • 人的電話您的Twilio號
  • Twilio發出HTTP請求數量的話音請求URL(指向了您的應用程序)
  • 您的應用程序現在需要做兩件事情
    • 打電話給你手機號碼
    • 將來電者放入持有音樂播放的會議
  • 首先,你需要在會​​議
  • 名字然後決定打電話給你的號碼,你的應用程序需要做的Twilio的REST API的調用來發起呼叫到您的手機
    • 您需要提供在此調用,這將讓你走進會議太回調URL,您可以通過包括會議名稱作爲URL
  • 你的應用程序還需要返回TwiML把主叫方進入會議一paramater做到這一點,使用相同的會議名稱
  • 最後,當您接聽電話Twilio將HTTP請求發送到您發起呼叫時
  • 這將要求您的網址應與TwiML迴應讓你在同一個電話會議

讓我們看看我們如何能做到這一點提供的網址在Node.js Express應用程序中:

您需要兩條路由,一條將接收初始請求,另一條將響應接聽電話時發出的請求。查看下面的代碼和評論。

var express = require("express"); 
var bodyParser = require("body-parser"); 
var twilio = require("twilio"); 

var app = express(); 
app.use(bodyParser.urlencoded({ extended: true })); 

var client = twilio(YOUR_ACCOUNT_SID, YOUR_AUTH_TOKEN); 

// This is the endpoint your Twilio number's Voice Request URL should point at 
app.post('/calls', function(req, res, next) { 
    // conference name will be a random number between 0 and 10000 
    var conferenceName = Math.floor(Math.random() * 10000).toString(); 

    // Create a call to your mobile and add the conference name as a parameter to 
    // the URL. 
    client.calls.create({ 
    from: YOUR_TWILIO_NUMBER, 
    to: YOUR_MOBILE_NUMBER, 
    url: "/join_conference?id=" + conferenceName 
    }); 

    // Now return TwiML to the caller to put them in the conference, using the 
    // same name. 
    var twiml = new twilio.TwimlResponse(); 
    twiml.dial(function(node) { 
    node.conference(conferenceName, { 
     waitUrl: "http://twimlets.com/holdmusic?Bucket=com.twilio.music.rock", 
     startConferenceOnEnter: false 
    }); 
    }); 
    res.set('Content-Type', 'text/xml'); 
    res.send(twiml.toString()); 
}); 

// This is the endpoint that Twilio will call when you answer the phone 
app.post("/join_conference", function(req, res, next) { 
    var conferenceName = req.query.id; 

    // We return TwiML to enter the same conference 
    var twiml = new twilio.TwimlResponse(); 
    twiml.dial(function(node) { 
    node.conference(conferenceName, { 
     startConferenceOnEnter: true 
    }); 
    }); 
    res.set('Content-Type', 'text/xml'); 
    res.send(twiml.toString()); 
}); 

讓我知道這是否有幫助。

+0

是啊謝謝! – nova

1

我解決了這個問題由我自己,你需要回答與會議兩個參與者,它會當對方接受呼叫

resp.say({voice:'woman'}, 'Welcome to our hotline. This could take a moment, please wait.') 
    .dial({},function(err){ 
     this.conference('example'); 
});