Skip to content

Instantly share code, notes, and snippets.

@yutackall
Created February 6, 2015 05:23
Show Gist options
  • Save yutackall/6e38063ae0dbe0f86427 to your computer and use it in GitHub Desktop.
Save yutackall/6e38063ae0dbe0f86427 to your computer and use it in GitHub Desktop.
Rails から twilio を使って電話発信してみた ( SMS は対象外ね ) ref: http://qiita.com/yutackall/items/8d3b3f5dd98cd136891f
<Response>
<Pause length="2"/>
<Say voice="woman" language="ja-jp">こちらは、ホゲホゲです。ユーザー様より予約がはいりました。管理画面から予約の確認をお願いします。</Say>
</Response>
path = Rails.root.join('config/twilio.yml')
if path.exist?
Hoge::Application.config.twilio_config = YAML.load(path.read)[Rails.env].to_options
else
Hoge::Application.config.twilio_config = {}
end
default: &default
account_sid: ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
auth_token: yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
from_tel: '05000000000'
development:
<<: *default
production:
<<: *default
test:
<<: *default
class TwilioCall < ActiveRecord::Base
validates :from_tel, :to_tel, :url, presence: true
after_create do
call
end
def self.twilio_client
account_sid = Hoge::Application.config.twilio_config[:account_sid]
auth_token = Hoge::Application.config.twilio_config[:auth_token]
@client ||= Twilio::REST::Client.new account_sid, auth_token
end
private
def call
TwilioCall.twilio_client.account.calls.create(
from: from_tel.gsub('-', '').gsub(/\A0/,'+81'), # 発信元電話番号
to: to_tel.gsub('-', '').gsub(/\A0/,'+81'), # 発信先電話番号
url: url # TwiML URL
)
update(result: 0, path: result.uri) # 成功した場合は、ログのパスを保存
rescue
code = $!.code rescue nil # TimeOut 等の場合は、code が返ってこないので
# 失敗した場合は、エラーコードと、エラーメッセージを保存
update(result: 1, error_code: code, error_message: $!.message)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment