首页 文章

允许iOS应用用户通过Twilio数字,Parse后端相互发出呼叫和文本

提问于
浏览
0

编辑:这个问题与Twilio代理的推出非常没有实际意义!检查一下 . 它基本上做了我想要做的事情,但方式更好,并且我自己的手册组织更少 .

我正在构建一个具有一项功能的应用程序,让您可以呼叫或发送与您匹配的其他用户 . 但是,我不想透露用户的信息,所以我试图用我通过Twilio编号来掩盖用户的号码 .

我使用Parse作为我的后端,这允许我在他们的服务器上运行 Cloud 代码和主机文件 . 每个匹配都是临时的,因此数字不必永久分配,例如this example似乎存储了每个最多100个连接的所有用户的 Map ,一对只需要有一个连接,并且只有需要连接的时间 .

我想我将在每个用户的Parse User对象上存储电话号码字符串,当用户点击按钮调用另一个用户时,让它将该字符串设置为按下用户的numberToCall属性的按钮 . 然后,我将使用saveInBackgroundWithBlock调用保存当前用户,并且在块内部,我将提示调用我的twilio编号 . 我将请求URL更改为[MyApp] .parseapps.com / [MyFunction] . 在那里,我将执行一个Parse Query来确定来电所属的用户,并将调用转发到他们的numberToCall属性 .

当我使用以下代码调用我的Twilio号码时,我能够设置[MyApp] .parseapps.com / [MyFunction]来播放[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();

我已经执行了Parse查询,所以我的数字字符串格式为'1XXXXXXXXXX' . 现在我只需要弄清楚如何连接这两个用户 . 我试过搜索Twilio的API文档,但我找不到相关信息 . 如果有人能指出我正确的方向,我真的很感激 .

1 回答

  • 3

    刚刚在我的历史中看到了这个问题,有人对它有所帮助,所以我想我会分享我的答案 .

    我的Twilio号码配置了请求URL“[https://MY APP NAME] .parseapp.com / calls " and " [https://MY APP NAME] .parseapp.com / texts”在我的Parse Cloud 代码文件中,我包括以下内容:

    // 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号码 . 然后我拨打与该作业关联的其他用户,其中呼叫者ID是第二个用户与该作业关联的号码 . 这意味着,如果客户呼叫提供商,并且他们挂断,则提供商可以拨打他们接收呼叫的相同号码并连接到客户 . 我用短信做了类似的事情,这样用户就可以从他们的短信应用程序中无缝地发送文本 .

    这样做的一个缺点是,当工作完成并创建新工作时,用户将在新工作上重复使用相同的旧twilio数字,因此1)我根据twilio的数量限制用户可以拥有的活动工作数量我拥有的用户,2)如果用户在作业结束后尝试从旧提供商处拨打号码,他们可能会从当前作业连接到不同的提供商 .

    希望这有助于某人 .

相关问题