Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 当我使用RESTAPI购买twilio号码时,如何获取电话号码SID?_Ruby_Sinatra_Twilio - Fatal编程技术网

Ruby 当我使用RESTAPI购买twilio号码时,如何获取电话号码SID?

Ruby 当我使用RESTAPI购买twilio号码时,如何获取电话号码SID?,ruby,sinatra,twilio,Ruby,Sinatra,Twilio,我使用的是ruby、sinatra和twilio。下面的代码购买一个号码,然后将该号码插入我的数据库。我也想获得返回的34个字符的电话号码“sid”,并将其插入我的数据库,但我不知道如何访问它 代码如下: get '/buy_phone_number'do #get subbacount credentials from my db user_key = users.where(:validation => cookie).join(:credentials, :user_id =>

我使用的是ruby、sinatra和twilio。下面的代码购买一个号码,然后将该号码插入我的数据库。我也想获得返回的34个字符的电话号码“sid”,并将其插入我的数据库,但我不知道如何访问它

代码如下:

get '/buy_phone_number'do
#get subbacount credentials from my db
user_key = users.where(:validation => cookie).join(:credentials, :user_id => :id).get(:user_key)
user_token = users.where(:validation => cookie).join(:credentials, :user_id => :id).get(:user_token)
user_id = users.where(:validation => cookie).get(:id)
primary_number = users.where(:validation => cookie).get(:primary_number)
#search for the right number
@sub_account_client = Twilio::REST::Client.new(user_key, user_token)
@subaccount = @sub_account_client.account
@numbers = @subaccount.available_phone_numbers.get('GB').toll_free.list({})
#buy the number and set the voice/sms url aprameters
@subaccount.incoming_phone_numbers.create(:phone_number => @numbers[0].phone_number, :voice_url => 'http://6c93ce61.ngrok.com/hello', :voice_method => 'GET', :sms_url => 'http://6c93ce61.ngrok.com/receive_sms', :sms_method => 'GET')
phone_number = @numbers[0].phone_number
#[HERE IS THE PROBLEM]sid = @numbers[0].Sid
numbers.insert(:user_id => user_id, :number => phone_number, :sid => sid, :inbound_route => primary_number, :send_to_voicemail => 'false', :show_caller_id => 'false')
return "You just bought: " + phone_number
这一切都可以正常工作,但34个字符的电话号码sid不会由
@numbers[0]返回。sid
或@numbers[0]。电话号码sid或
@numbers[0]。数字号码sid

因此有两个问题:

1) 尤其是如何访问Sid号? 2) 如何查看购买号码后返回的所有物品


一如既往地感谢您的帮助……

我是Twilio开发者的福音传道者,希望我能帮上忙

这里的问题是,服务器不会向SIDs回复它返回的电话号码。您仅在购买号码时获得SID

您要做的是使用。因此,从示例的最后一部分开始,您可以这样做:

@numbers = @subaccount.available_phone_numbers.get('GB').toll_free.list({})
#buy the number and set the voice/sms url aprameters
@purchased_number = @subaccount.incoming_phone_numbers.create(:phone_number => @numbers[0].phone_number, :voice_url => 'http://6c93ce61.ngrok.com/hello', :voice_method => 'GET', :sms_url => 'http://6c93ce61.ngrok.com/receive_sms', :sms_method => 'GET')
phone_number = @purchased_number.phone_number
sid = @purchased_number.sid
numbers.insert(:user_id => user_id, :number => phone_number, :sid => sid, :inbound_route => primary_number, :send_to_voicemail => 'false', :show_caller_id => 'false')
return "You just bought: " + phone_number
如您所见,我将购买号码的响应分配给
@purchased\u number
,您可以从中读取sid和电话号码


如果有帮助,请告诉我。

为菲尔干杯。感谢上帝。这是非常令人困惑的,因为“资源属性”在相关api页面上以Sid的形式列出,但返回的对象是Sid(带有一个小的“s”)。或者更令人困惑的是,“FriendlyName”变成了“FriendlyName”;那么一个可怜的家伙该怎么办呢。这就解释了我问题的第二部分:一般来说,我怎样才能看到所有返回的对象?有什么想法吗?是的,你是对的,Ruby库并没有遵循核心文档。这使得它的行为更像Ruby开发人员所期望的,因为我们倾向于使用小写、带下划线的方法名。因此,您可以推断,如果文档中显示“FriendlyName”,那么将有一个“FriendlyName”属性。为了查看Twilio对象的属性,您可以尝试记录
对象的结果。检查
,尽管我不确定这是否会显示所有内容。你可以相信这些文件和转换方法。@philnash“福音传道者”我不知道剧本现在有宗教信仰,泰晤士报是怎么做的changed@PauAI这也许是个奇怪的头衔,但它意味着我是来帮忙的。这就是为什么我会这么做,这就是为什么我写关于Twilio的博客文章,以及为什么我总是可以收到关于它的反馈。我可以向你保证,这里没有宗教信仰。