本文介绍了允许iOS应用程序用户通过Twilio号码,解析后端彼此通话和发短信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着Twilio代理的推出,这个问题非常有争议!检查出来.它基本上可以完成我想做的事情,但是方法更好,而且在我自己的目的上减少了人工组织.

This question is very moot with the rollout of Twilio Proxy! Check that out instead. It basically does what I wanted to do but way better, and with less manual organization on my own end.

我正在构建一个具有可让您致电或发短信给与之匹配的其他用户的功​​能的应用程序.但是,我不想透露用户的信息,因此我试图用Twilio拥有的号码来掩盖用户的号码.

I am building an app with a feature that let's you call or text another user you are matched with. However, I don't want to give out user's information, so I am trying to mask the user's numbers with a number I have through Twilio.

我将Parse用作后端,这使我可以在其服务器上运行云代码和托管文件.每次比赛都是暂时的,因此不必永久分配号码,例如此示例似乎存储了每个用户最多100个连接的所有用户的地图,一对仅必须只有一个连接,并且仅在需要建立连接的时候.

I am using Parse as my backend, which allows me to run cloud code and host files on their server. Each match is temporary, so the numbers don't have to be permanently assigned, such as this example which seems to store the map of all users with a max of 100 connections each, a pair only ever has to have one connection, and only at the time the connection needs to be made.

我认为我将在其Parse User对象上存储每个用户的电话号码字符串,并且当用户按下按钮以呼叫另一个用户时,将其设置为按下用户的numberToCall属性的按钮.然后,我将通过saveInBackgroundWithBlock调用保存当前用户,并且在该块内部,将提示您调用我的twilio号码.我将请求URL更改为[MyApp] .parseapps.com/[MyFunction].在这里,我将执行解析查询"以找出传入呼叫属于哪个用户,并将呼叫转发到他们的numberToCall属性.

I think I'm going to be storing the phone number string for each user on their Parse User object, and when a user hits the button to call another, have it set that string as the button pressing user's numberToCall attribute. Then, I will save the current user with a saveInBackgroundWithBlock call, and inside of the block, I will prompt a call to my twilio number. I changed the Request URL to [MyApp].parseapps.com/[MyFunction]. There, I will perform a Parse Query to figure out which user the incoming call belongs to, and forward the call to their numberToCall attribute.

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

I have been able to set up [MyApp].parseapps.com/[MyFunction] to play [Message] when my Twilio number is called using the following code:

    // 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文档,但找不到相关信息.如果有人能指出正确的方向,我将不胜感激.

I have performed the Parse Query, so I have the number string in the format '+1XXXXXXXXXX'. Now I just need to figure out how to connect the two users. I have tried searching through Twilio's API documentation, but I haven't been able to find the relative info. If anybody could point me in the right direction, I'd really appreciate it.

推荐答案

在我的历史记录中刚刚看到这个问题,有人喜欢它,所以我想与我分享我的答案.

Just saw this question in my history, and someone favorited it, so I thought I'd share my answer.

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

My Twilio numbers are configured with the Request URLs "https://[MY APP NAME].parseapp.com/calls" and "https://[MY APP NAME].parseapp.com/texts"In my Parse cloud code file I included the following:

// 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是第二个用户与该工作相关联的号码.这意味着,如果客户呼叫提供商,并且他们挂断了电话,则提供商可以拨打与他们接听电话并连接到该客户的电话相同的号码.我在短信方面做了类似的事情,以便用户可以从其消息传递应用程序中无缝地互相发短信.

Basically, each ActiveJob has a customer's phone number, a provider's phone number, and the twilio number that each one will associate with the job. I can only do one thing when a user calls my twilio number, based on their number, so I find the active job where they are either the customer or provider, and the number they have associated with that job is the twilio number that they called. I then dial out to the other user associated with the job, with the caller ID being the number that the second user associates with the job. This means that if customer calls the provider, and they hang up, the provider could call the same number they received the call from and be connected to the customer. I did a similar thing with texting, so that users could seamlessly text each other from their messaging app.

这样做的一个缺点是,随着工作的完成和新工作的创建,用户将在新工作上重用相同的旧twilio编号,因此1)我限制了用户根据我拥有的twilio用户数量,以及2)如果某位用户在工作结束后尝试从旧的提供者那里拨打电话,则他们可能与当前工作所连接的提供者不同.

The one downside to this is that as jobs are fulfilled and new ones created, users will be reusing the same old twilio numbers on the new jobs, so 1) I am limiting the number of active jobs users can have based on the number of twilio users I have, and 2) if a user tries to call a number from an old provider after the job is over, they may be connected to a different provider from a current job.

希望这对某人有帮助.

这篇关于允许iOS应用程序用户通过Twilio号码,解析后端彼此通话和发短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 16:55