Sha256: b1c9c416117271c5dba2eb18b2f0492bd3a8bf2875d4dbef0cd8e211d8c0997b

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

class Fluent::TwilioOutput < Fluent::Output
  Fluent::Plugin.register_output('twilio', self)

  config_param :account_sid, :string
  config_param :auth_token, :string
  config_param :from_number, :string, :default => ''
  config_param :default_number, :string, :default => ''
  config_param :default_voice, :string, :default => 'woman'
  config_param :language, :string, :default => 'ja-jp'

  VOICE_MAP = ['woman']

  def initialize
    super
    require 'uri'
    require 'twilio-ruby'
  end

  def configure(conf)
    super

  end

  def emit(tag, es, chain)
    es.each do |time,record|
      number = record['number'].nil? ? @default_number : record['number']
      @voice = VOICE_MAP.include?(record['voice']) ? record['voice'] : @default_voice
      call(number, record['message'])
    end

    chain.next
  end

  def call(number, message)
    response = Twilio::TwiML::Response.new do |r|
      r.Say message, :voice => @voice, :language => @language
    end
    xml = response.text.sub(/<[^>]+?>/, '')
    url = "http://twimlets.com/echo?Twiml=#{URI.escape(xml)}"
    $log.info "twilio: generateing twiml: #{xml}"

    client = Twilio::REST::Client.new(@account_sid, @auth_token)
    account = client.account
    begin
      call = account.calls.create({:from => @from_number, :to => number, :url => url})
    rescue => e
      $log.error "twilio: Error: #{e.message}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fluent-plugin-twilio-0.0.1 lib/fluent/plugin/out_twilio.rb