ios - 允许 iOS 应用程序用户通过 Twilio 号码、Parse 后端相互调用电话和发短信

标签 ios parse-platform twilio phone-call

编辑:这个问题对于 Twilio 代理的推出非常没有实际意义!请检查一下。它基本上完成了我想做的事情,但做得更好,而且我自己的手动组织工作更少。

我正在构建一个应用程序,该应用程序具有一项功能,可让您给匹配的另一位用户打电话或发短信。但是,我不想泄露用户的信息,所以我试图用我通过 Twilio 获得的号码来掩盖用户的号码。

我使用 Parse 作为我的后端,它允许我在他们的服务器上运行云代码和托管文件。每个匹配项都是临时的,因此不必永久分配数字,例如 this example它似乎存储了所有用户的 map ,每个用户最多有 100 个连接,一对只需要有一个连接,并且只有在需要建立连接的时候。

我想我要将每个用户的电话号码字符串存储在他们的 Parse User 对象上,当用户点击按钮调用另一个用户时,将该字符串设置为按下用户 numberToCall 属性的按钮。然后,我将使用 saveInBackgroundWithBlock 调用保存当前用户,并在该 block 内提示对我的 twilio 号码的调用。我将请求 URL 更改为 [MyApp].parseapps.com/[MyFunction]。在那里,我将执行解析查询以确定传入调用属于哪个用户,并将调用转发到他们的 numberToCall 属性。

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

    // 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”的数字字符串。现在我只需要弄清楚如何连接这两个用户。我尝试搜索 Twilio 的 API 文档,但未能找到相关信息。如果有人能指出我正确的方向,我将不胜感激。

最佳答案

刚刚在我的历史记录中看到这个问题,有人收藏了它,所以我想我应该分享我的答案。

我的 Twilio 号码配置有请求 URL“https://[MY APP NAME].parseapp.com/calls”和“https://[MY APP NAME].parseapp.com/texts” 在我的 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 号码。当用户根据他们的号码调用我的 twilio 号码时,我只能做一件事,所以我找到他们是客户或提供商的事件工作,并且他们与该工作关联的号码是他们调用的 twilio 号码.然后,我拨出给与该工作相关联的其他用户,来电显示是第二个用户与该工作相关联的号码。这意味着,如果客户调用供应商,但他们挂断了电话,供应商可以调用他们接听电话的同一号码,并连接到客户。我对短信做了类似的事情,这样用户就可以通过他们的消息应用程序无缝地互相发短信。

这样做的一个缺点是,随着工作的完成和新工作的创建,用户将在新工作上重复使用相同的旧 twilio 数字,因此 1) 我限制了用户可以拥有的事件工作数量我拥有的 twilio 用户数量,以及 2) 如果用户在工作结束后尝试调用旧供应商的号码,他们可能会连接到与当前工作不同的供应商。

希望这对某人有帮助。

关于ios - 允许 iOS 应用程序用户通过 Twilio 号码、Parse 后端相互调用电话和发短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29310514/

相关文章:

ios - 您可以在 iOS 上保存编辑后的 ​​RAW .dng 文件吗?

ios - 数组索引超出范围 Query 和 CollectionCell

ios - 使用 Parse Push 时将应用内警报 View 通知与横幅 View 交换

ruby-on-rails - 在我的 Rails 应用程序中使用 twilio 发送短信的问题

通过 API 检查 Twilio SMS 状态(无 webhook)

iphone - 如何将UITabbar连接到ViewController

ios - 在国际化我的应用程序之前,如何更改 Xcode 中的开发语言?

node.js - Twilio 应答机检测

ios - StreamReader 的 Swift 版本仅流式传输整个文件(不是按 block 传输,这是需要的)

ios - 点赞按钮并非每次都起作用