class Ruboty::Callng::Actions::Call

Public Instance Methods

call() click to toggle source
# File lib/ruboty/callng/actions/call.rb, line 8
def call
  phone_call(generate_text)
end
call_with_message() click to toggle source
# File lib/ruboty/callng/actions/call.rb, line 12
def call_with_message
  phone_call(message[:text])
end

Private Instance Methods

detect_language(text) click to toggle source
# File lib/ruboty/callng/actions/call.rb, line 53
def detect_language(text)
  case text
  when /\A[[:ascii:]]+\z/
    'en-US'
  else
    'ja-JP'
  end
end
from() click to toggle source
# File lib/ruboty/callng/actions/call.rb, line 18
def from
  ENV['RUBOTY_PHONE_NUMBER']
end
generate_text() click to toggle source
# File lib/ruboty/callng/actions/call.rb, line 22
def generate_text
  channel = message.original[:channel] ? "at #{message.original[:channel]['name']} channel" : ''
  "#{message.original[:user]['name']} calls you #{channel} in Slack. Please open Slack"
end
phone_call(text) click to toggle source
# File lib/ruboty/callng/actions/call.rb, line 27
def phone_call(text)
  twilio_client.api.account.calls.create({
    :to => to,
    :from => from,
    :url => twiml_url(text),
    :method => 'GET',
    :fallback_method => 'GET',
    :status_callback_method => 'GET',
  })
  message.reply("Calling")
rescue => err
  message.reply("Calling failed: #{err}")
end
to() click to toggle source
# File lib/ruboty/callng/actions/call.rb, line 41
def to
  message[:to]
end
twilio_client() click to toggle source
# File lib/ruboty/callng/actions/call.rb, line 45
def twilio_client
  # put your own credentials here
  account_sid = ENV['TWILIO_ACCOUNT_SID']
  auth_token = ENV['TWILIO_AUTH_TOKEN']

  Twilio::REST::Client.new account_sid, auth_token
end
twiml(text) click to toggle source
# File lib/ruboty/callng/actions/call.rb, line 62
        def twiml(text)
          <<TWIML

<Response>
    <Say voice="alice" language="#{detect_language(text)}">#{text}</Say>
</Response>
TWIML
        end
twiml_url(text) click to toggle source
# File lib/ruboty/callng/actions/call.rb, line 71
def twiml_url(text)
  "http://twimlets.com/echo?Twiml=#{URI.escape(twiml(text))}"
end