允许iOS应用用户通过Twilio号码,Parse后端来相互呼叫和发短信

我正在构build一个function的应用程序,让你打电话或发短信与你匹配的另一个用户。 但是,我不想泄露用户的信息,所以我试图用我通过Twilio获得的号码来掩盖用户的号码。

我使用Parse作为后端,这使我可以在其服务器上运行云代码和主机文件。 每个匹配都是临时的,所以数字不需要永久分配,比如这个例子似乎存储了每个用户最多100个连接的地图,一对只有一个连接,在需要连接的时候。

我想我将要为每个用户的电话号码string存储在他们的parsing用户对象,当用户点击button调用另一个,让它设置该string作为button按用户的numberToCall属性。 然后,我将使用saveInBackgroundWithBlock调用保存当前用户,并在该块内部,我会提示对我的twilio号码的呼叫。 我将请求URL更改为[MyApp] .parseapps.com / [MyFunction]。 在那里,我将执行一个分析查询来确定来话呼叫属于哪个用户,并将呼叫转移到他们的numberToCall属性。

我已经能够使用以下代码设置[MyApp] .parseapps.com / [MyFunction]播放[Message]时,我的Twilio号码被调用:

// Include Cloud Code module dependencies var express = require('express'), twilio = require('twilio'); // Create an Express web app (more info: http://expressjs.com/) var app = express(); // Create a route that will respond to am HTTP GET request with some // simple TwiML instructions app.get('/hello', function(request, response) { // Create a TwiML response generator object var twiml = new twilio.TwimlResponse(); // add some instructions twiml.say('Hello there! Isn\'t Parse cool?', { voice:'woman' }); // Render the TwiML XML document response.type('text/xml'); response.send(twiml.toString()); }); // Start the Express app app.listen(); 

我已经执行了分析查询,所以我有格式为“+ 1XXXXXXXXXX”的数字string。 现在我只需要弄清楚如何连接两个用户。 我试图通过Twilio的API文档search,但我一直没有find相关的信息。 如果有人能指引我正确的方向,我会非常感激。

刚才在我的历史中看到了这个问题,有人collections了,所以我想我会分享我的答案。

我的Twilio号码是通过请求URL“ https:// [我的 APP名称] .parseapp.com / calls”和“ https:// [我的 APP名称] .parseapp.com / texts”configuration的。在我的Parse云代码文件我包括以下内容:

 // Include Cloud Code module dependencies var express = require('express'), twilio = require('twilio'); // Create an Express web app (more info: http://expressjs.com/) var app = express(); app.get ('/calls', function(request, response) { // Create a TwiML response generator object var twiml = new twilio.TwimlResponse(); var query1 = new Parse.Query(ActiveJob); //query1 will look to see if a customer made this call var twilioNumber = request.param('To'); var fromNumber = request.param('From'); query1.equalTo("customerNumber", fromNumber); query1.equalTo("customerTwilioNumber", twilioNumber); query1.first ({ success: function(result) { //Forward call to the provider that the customer was trying to reach var Job = result; var receiverNumber = Job.get("providerNumber"); var receiverTwilioNumber = Job.get("providerTwilioNumber"); twiml.dial({callerId:receiverTwilioNumber}, receiverNumber); response.type('text/xml'); response.send(twiml.toString('')); }, error: function(error) { //No customer made this call. See if a provider made the call with query2 var query2 = new Parse.Query(ActiveJob); query2.equalTo("providerNumber", fromNumber); query2.equalTo("providerTwilioNumber", twilioNumber); query2.first ({ success: function(result) { //Forward the call to the customer the provider was trying to call var Job = result; var receiverNumber = Job.get("customerNumber"); var receiverTwilioNumber = Job.get("customerTwilioNumber"); twiml.dial({callerId:receiverTwilioNumber}, receiverNumber); response.type('text/xml'); response.send(twiml.toString('')); }, error: function(error) { //The phone number used to make this call has not been assigned to this Twilio //number as a customer nor a provider response.error("This number has not been assigned to a Job for the current user"); twiml.say('Connection failed. Users must use their Lawn Guru verified number for communication.', {voice:'woman'}); } }); } }); //Since queries are ran in background, this will play while/before the call is connected twiml.say('Connecting.', {voice:'woman'}); }); 

基本上,每个ActiveJob都有一个客户的电话号码,一个提供商的电话号码,以及每个人将与该工作关联的Twilio号码。 当用户根据他们的电话号码拨打我的电话号码时,我只能做一件事,所以我find了他们是客户或提供商的活跃工作,他们与这个工作相关联的电话号码是他们拨打的电话号码。 然后,我拨打与该作业相关的其他用户,主叫方ID是第二个用户与该作业相关联的号码。 这意味着,如果客户打电话给供应商,他们挂断,供应商可以拨打他们接到电话的号码,并连接到客户。 我做了一个类似的事情发短信,以便用户可以无缝地从他们的消息应用程序相互文字。

这样做的一个缺点是,随着工作的完成以及新的工作的完成,用户将在新工作中重复使用旧的工作数量,所以1)我限制了用户可以根据工作数量我有的用户,以及2)如果用户在工作结束后试图从旧的提供者拨打电话号码,他们可能被连接到与当前工作不同的提供者。

希望这有助于某人。