2017-03-31 14 views
0

下面是一些我想完成的步驟:我怎樣才能改變通話這是正在進行的與Twilio和的NodeJS

  1. 呼叫,並最終文本,我的Twilio號
  2. 集聚了一​​批以呼叫。
  3. 創建我的當前呼叫並將其移至電話會議中。
  4. 調用收集的號碼。
  5. 將被叫號碼加入與我的會議。

目前我可以撥打我的Twilio號碼,收集一個號碼,創建一個電話會議 - 但是我迷路的是呼叫收集的號碼以及將它們添加到我創建的會議中。

app.post('/call', function(req, res) { 
 

 
    var twiml = new twilio.TwimlResponse(); 
 
    res.type('text/xml'); 
 

 
    if (req.body.From === '+15555555555) { 
 
    twiml.gather({ 
 
     action: '/call/initiate', 
 
     finishOnKey: '#', 
 
     numDigits: '10', 
 
     timeout: '5' 
 
    }, function() { 
 
     this.say('Enter your number', { 
 
     voice: 'man' 
 
     }); 
 
    }); 
 

 
    } 
 
    else { 
 

 
    twiml.redirect(VOICEMAIL_TWIMLET_OF_CHOICE); 
 

 
    } 
 

 
    res.send(twiml.toString()); 
 

 
}); 
 

 
// Initiate a call from text 
 
app.post('/call/initiate', function(req, res) { 
 
    // Create new Twiml response 
 
    var twiml = new twilio.TwimlResponse(); 
 
    // Phone number to call and add to conference 
 
    var to = req.body.Digits; 
 

 
    // Create random conference name 
 
    var conferenceName = Math.floor(Math.random() * 10000).toString(); 
 

 
    // Add myself to the conference call 
 
    twiml.dial((node) => { 
 
    node.conference(conferenceName, { 
 
     startConferenceOnEnter: true, 
 
     record: true, 
 
     beep: 'true' 
 
    }); 
 
    }); 
 

 
    // Redirect twiml to a new url 
 
    // Send conferenceName & Number 
 
    twiml.redirect('/join_conference?id=' + conferenceName + '&number=' + to); 
 

 
    res.set('Content-Type', 'text/xml'); 
 
    res.send(twiml.toString()); 
 
}); 
 

 
// Call and add caller to conference 
 
app.post('/join_conference', (req, res) => { 
 

 
    var conferenceName = req.query.id; 
 
    var to = '+1' + req.query.number; 
 

 
    // Create new Twiml response 
 
    var twiml = new twilio.TwimlResponse(); 
 

 
    // Call and add user to conference call 
 
    twiml.dial(to, (node) => { 
 
    node.conference(conferenceName, { 
 
     startConferenceOnEnter: true, 
 
    }); 
 
    }); 
 

 
    console.log('call called'); 
 

 
    res.set('Content-Type', 'text/xml'); 
 
    res.send(twiml.toString()); 
 
});

後,我進入了數字和打finishOnKey我聽到自動等待音樂。然而,在這一點上 - 應用程序只是掛起,並沒有放置電話。

回答

0

Twilio開發人員在這裏傳播。

問題是,您返回到您的/call/initiate端點的TwiML僅用於呼叫的第一站,您無法單獨使用TwiML創建另一個呼叫線路。

但是,您可以在同一請求中使用Twilio REST APIgenerate the second leg of the call。這裏有一個更新的端點,您可以改用:

app.post('/call/initiate', function(req, res) { 
    // Create new Twiml response 
    var twiml = new twilio.TwimlResponse(); 
    // Phone number to call and add to conference 
    var to = req.body.Digits; 

    // Create random conference name 
    var conferenceName = Math.floor(Math.random() * 10000).toString(); 

    // Add myself to the conference call 
    twiml.dial((node) => { 
    node.conference(conferenceName, { 
     startConferenceOnEnter: true, 
     record: true, 
     beep: 'true' 
    }); 
    }); 

    // Make call to the other party 
    // Send conferenceName as part of the URL 
    var client = new twilio(YOUR_ACCOUNT_SID, YOUR_AUTH_TOKEN); 
    client.calls.create({ 
    from: YOUR_TWILIO_NUMBER, 
    to: '+1' + to, 
    url: 'https://example.com/join_conference?id=' + conferenceName 
    }); 

    res.set('Content-Type', 'text/xml'); 
    res.send(twiml.toString()); 
}); 

那麼你的「/ join_conference」端點只需要撥打主叫方進入會議室,就像這樣:

app.post('/join_conference', (req, res) => { 

    var conferenceName = req.query.id; 

    // Create new Twiml response 
    var twiml = new twilio.TwimlResponse(); 

    // Call and add user to conference call 
    twiml.dial((node) => { 
    node.conference(conferenceName, { 
     startConferenceOnEnter: true, 
    }); 
    }); 

    console.log('call called'); 

    res.set('Content-Type', 'text/xml'); 
    res.send(twiml.toString()); 
}); 

讓我知道是否有幫助在所有。

編輯:該評論說,它仍然沒有打電話給對方。讓我們仔細看看命令來打電話給對方。創建的調用將返回一個Promise,所以我們可以看到會發生什麼以及它是成功還是失敗(以及爲什麼)。

var client = new twilio(YOUR_ACCOUNT_SID, YOUR_AUTH_TOKEN); 
    client.calls.create({ 
    from: YOUR_TWILIO_NUMBER, 
    to: '+1' + to, 
    url: 'http://example.com/join_conference?id=' + conferenceName 
    }).then(function(call) { 
    console.log(call.sid); 
    }).catch(function(err) { 
    console.error("Something went wrong creating the call."); 
    console.error(err); 
    }); 

嘗試更新代碼,看看會發生什麼。如果有錯誤,我相信它會幫助你解決這個問題。

+0

我試過這些更改沒有成功。這個過程仍然掛在'/ call/initiate'上。似乎根本沒有打電話給第二方,從日誌中我可以看到它從來沒有擊中'join_conference'端點。 – JorgeEstaAqui

+0

我編輯了我的答案。我會嘗試從呼叫生成中捕獲任何錯誤,這可能會顯示正在發生的事情。 – philnash

+0

謝謝 - 我應該知道這樣做。我更新了代碼以包含錯誤處理。現在我發現我的網址無效。 (https://www.twilio.com/docs/api/errors/21205)。我瀏覽了我的Twilio儀表板,並沒有看到需要爲這種類型的請求設置處理的位置。 – JorgeEstaAqui

相關問題