Twilio Call Forwarding Number Always Blank - twilio

I'm currently trying to connect two users via twillio and am masking their identities. I am doing this by having:
User A dials my Twillio # ---> hits my app URL endpoint --> dials User B and has the call ID show up as my Twillio #
When trying this, the TwiML is being rendered with the dialed/forwarded number always blank. Even when I hardcode it, Twillio never forward the call anywhere.
def make_twillio_call_PSTN
incoming_number = params[:From]
#strip +1 from number
incoming_number = incoming_number.byte_slice(2,10)
response = Twilio::TwiML::Response.new do |r|
r.Dial callerId: MY_TWILIO_MOBILE do |d|
d.Number MY_NUMBER
end
render xml: response.to_xml
end
end

Related

Twillio enable listener to press * to ask to speak during conference

I would like to enable the listener to press * key to ask for unmuting during a conference call, with the moderator able to unmute him/her from a console.
I have a controller with the following :
def conference_connect
#room_name = flash[:room_name]
#room_id = flash[:event_id]
case params['Digits']
when "1" # listener
#muted = "true"
when "3" # moderator
#moderator = "true"
end
response = Twilio::TwiML::VoiceResponse.new
response.say(voice: 'alice', language: 'en-US', message: 'You are in, press * at anytime to ask a question')
dial = Twilio::TwiML::Dial.new(hangupOnStar: true)
dial.conference(#room_name,
wait_url: "http://twimlets.com/holdmusic?xxxxxxx&",
muted: #muted || "false",
start_conference_on_enter: #moderator || "false",
end_conference_on_exit: #moderator || "false",
)
gather = Twilio::TwiML::Gather.new(action: '/redirectIntoConference?name= ' + #room_name, digits: 1)
response.append(dial)
end
I have the following error :
No template found for TwilioController#conference_connect, rendering head :no_content
I would like to send a message to the moderator (or update some params) to notify him that a listener has a question to ask.
Twilio developer evangelist here.
You have a couple of issues here. Firstly, your error is because you are not returning the TwiML you built in your controller action and Rails is looking for a template instead.
At the end of the action call render like this:
response.append(dial)
render xml: response.to_xml
end
As for requesting to speak on * you are halfway there. Firstly, the <Gather> is not going to help you, so get rid of the line:
gather = Twilio::TwiML::Gather.new(action: '/redirectIntoConference?name= ' + #room_name, digits: 1)
Instead, you have hangupOnStar set to true in your <Dial> this will disconnect the user from the conference (which sounds bad, but is what you want for this). You just need to setup what happens to the user after they hangup.
In this case, you want to make the request to the moderator and then have them rejoin the conference. You do this with an action parameter on the <Dial> that points to a URL that will be requested when the caller leaves the conference.
Within this action you need to somehow alert your moderator (I'm not sure how you're planning that) and then return TwiML to enter the caller back into the conference. Don't forget to set the conference up in the same way, with hangupOnStar and an action.
Ulitmately your action should look a bit like this:
def conference_connect
#room_name = flash[:room_name]
#room_id = flash[:event_id]
case params['Digits']
when "1" # listener
#muted = "true"
when "3" # moderator
#moderator = "true"
end
response = Twilio::TwiML::VoiceResponse.new
response.say(voice: 'alice', language: 'en-US', message: 'You are in, press * at anytime to ask a question')
dial = Twilio::TwiML::Dial.new(hangupOnStar: true, action: '/redirectIntoConference?name= ' + #room_name)
dial.conference(#room_name,
wait_url: "http://twimlets.com/holdmusic?xxxxxxx&",
muted: #muted || "false",
start_conference_on_enter: #moderator || "false",
end_conference_on_exit: #moderator || "false",
)
response.append(dial)
render xml: response.to_xml
end
Let me know if that helps at all.

Scanning phone number for format to send SMS messages

I have code for scanning phone numbers to put into the correct format and then have the SMS sent to the phone number. Although, I don't know how to apply this the "best" way.
In my create method I have:
if #order.phone_number.present?
account_sid = '1234567890'
auth_token = '1234567890'
client = Twilio::REST::Client.new(account_sid, auth_token)
message = client.messages.create(
from: '+12017541209',
to: '+120188854064',
body: "You received Order from #{#order.listing.name} - View it here: localhost.com/order/#{#order.order_token}/order_confirmation" )
end
I then have this code for scanning:
def clean_number
number = self.phone_number.scan(/\d+/).join
number[0] == "1" ? number[0] = '' : number
number unless number.length != 10
end
The format I need is 11231234567
Whether the text_input is:
1-123-123-1234
123-123-1234
+1(123)-123-1234
etc.
Now, the SMS works but I haven't applied the clean_number method yet. Should i be putting this in my model, and then call it in the controller, or have it in the private within the controller?

Twilio conference sid not getting in params using rails 4

using twilio am making conference room. I successfully created the conference between two users. But i can't able to get conference sid from twilio. Can any one please help me in that.
twiml1 = Twilio::TwiML::Response.new do |r|
r.Say "You have joined the conference."
r.Dial do |d|
d.Conference "#{conference_title}",
waitUrl: " ",
muted: "false",
startConferenceOnEnter: "true",
endConferenceOnExit: "true",
maxParticipants: 5,
end
end
This is how am connecting connecting conference in between two user.
Twilio developer evangelist here.
You don't get the parameters for the Conference SID at this point because you could well be creating the conference with the return of this TwiML. Instead, there are a couple of other ways to get the SID.
Conference events
With the statusCallback attribute on the <Conference> element you can set a URL to receive various events as webhooks regarding the conference (pick those events with the statusCallbackEvent attribute). You could listen for the start event and you will receive the Conference SID as a parameter in the webhook request.
Checking the REST API
Otherwise, you can make an API call to the REST API Conference resource.
You can search for your conference by the name you give it. For example, you can search for the conference called "MyRoom" using the following Ruby (if you have the twilio-ruby gem installed):
#client = Twilio::REST::Client.new YOUR_ACCOUNT_SID, YOUR_AUTH_TOKEN
# Loop over conferences and print out the SID for each one
#client.account.conferences.list({
:status => "in-progress",
:friendly_name => "MyRoom"}).each do |conference|
puts conference.sid
end
Let me know if that helps at all.
[edit]
An example of the statusCallback in Rails.
First I'm going to adjust the TwiML you used initially:
twiml1 = Twilio::TwiML::Response.new do |r|
r.Say "You have joined the conference."
r.Dial do |d|
d.Conference "#{conference_title}",
waitUrl: " ",
muted: "false",
startConferenceOnEnter: "true",
endConferenceOnExit: "true",
maxParticipants: 5,
statusCallback: "/conferences/callback",
statusCallbackEvent: "start"
end
end
I've added the statusCallback attribute with a path to an action in your Rails application (you can use whatever path you want, I just made this up for now). I also added the statusCallbackEvent attribute with the value "start". There are other events available, but to get the SID, the start event will do.
Now we need an action to receive that callback.
class ConferencesController < ApplicationController
def callback
# Do something with the conference SID.
Rails.logger.info(params["ConferenceSid"])
render :nothing => true
end
end
You'll need to point a POST route at that controller action too, but I'll leave that up to you to sort out.

Twilio returning authenticate error for my local machine only

My team and I are using twilio to send sms messages. Everything seems to work fine on everyone else's local machine (with the same exact code) except twilio always returns an authenticate error to me. I'm sending the messages to their "special number" so it won't actually send a real text message but it still returns an authenticate error.
here's some of our code to send the message:
def send_sms
self.from_phone_number = CONFIG['live_twilio'] ? self.customer.assigned_phone_number : CONFIG['test_phone_number']
self.to_phone_number = CONFIG['live_twilio'] ? self.customer.customer_phone_number : CONFIG['test_phone_number']
begin
report_to_new_relic_insights
send_message_with_twilio!
rescue Twilio::REST::RequestError => e
self.error_description = e.message
end
self.dispatched_at = Time.now
self.save(validate: false)
return e
end
def send_message_with_twilio!
unless self.customer.example_customer?
twilio_params = {
from: "+1#{from_phone_number}",
to: "+1#{to_phone_number}",
body: self.text
}
if ENV['RECORD_TWILIO_STATUSES'].in?(['1', 'true'])
twilio_params[:status_callback] = "#{CONFIG['secure_domain']}/messages/#{id}/update_status"
end
client.account.messages.create(twilio_params)
else
# #onboarding_flow
# don't send the actual SMS to the example customer
self.customer.send_reply_as_example_customer! if self.customer.first_reply?
end
end
def client
#client ||= begin
account_sid = ENV['ACCOUNT_SID'] || CONFIG['account_sid']
auth_token = ENV['AUTH_TOKEN'] || CONFIG['auth_token']
Twilio::REST::Client.new account_sid, auth_token
end
end
every time this line runs: client.account.messages.create(twilio_params)
it returns authenticate error. it works on every other local machine except for mine. all of the code is exactly the same, the auth tokens are exactly the same. any ideas what the problem could be? (the auth tokens are getting pulled from config.yml
More info: even when running the bare bones twilio client in console with the same exact info in both machines, mine returns an error and my coworkers returns valid
Twilio evangelist here.
This could be one of many things, but the only reason Twilio would complain about authentication, is if you have a wrong account SID or auth token.
So can pretty much guarantee this is the bit that is going wrong:
def client
#client ||= begin
account_sid = ENV['ACCOUNT_SID'] || CONFIG['account_sid']
auth_token = ENV['AUTH_TOKEN'] || CONFIG['auth_token']
Twilio::REST::Client.new account_sid, auth_token
end
end
My suggestion here is for you to open up your terminal and run:
$ printenv
This should print all your environment variables for you. You're interested on ACCOUNT_SID and AUTH_TOKEN, so you could get those specific values from terminal as follows:
$ printenv | grep "ACCOUNT_SID\|AUTH_TOKEN"
Then check with your peers if you are using the same values. Please let me know the outcome.

Resubscribe an email address in Campaign Monitor

Has anyone had success in resubscribing an email address after being unsubscribed via the Campaign Monitor API.
I ask as i want to keep a list of Active User's email addresses in my CM Active List. When they are suspended they get removed, when they join or pay their fees before getting deleted they are (re)subscribed.
Looking at the Rails API docs:
# File lib/campaign_monitor.rb, line 241
def remove_subscriber(email)
response = #cm_client.Subscriber_Unsubscribe("ListID" => #id, "Email" => email)
Result.new(response["Message"], response["Code"].to_i)
end
# File lib/campaign_monitor.rb, line 445
def unsubscribe(list_id)
response = #cm_client.Subscriber_Unsubscribe("ListID" => list_id, "Email" => #email_address)
Result.new(response["Message"], response["Code"].to_i)
end
On the CM website to move an email in the subscriber list to the active list you need to confirm you have permission to resubscribe them, can anyone say for sure that this applies to the API too?
I've just found the Subscriber.AddAndResubscribe method, undocumented in http://campaignmonitor.rubyforge.org/

Resources