Skip to content

Instantly share code, notes, and snippets.

@Maroo-b
Created August 23, 2018 08:12
Show Gist options
  • Save Maroo-b/e41b9a89d40dc37e53f43511178a319b to your computer and use it in GitHub Desktop.
Save Maroo-b/e41b9a89d40dc37e53f43511178a319b to your computer and use it in GitHub Desktop.
Twilio call logs exporter
require "csv"
class TwilioCallExporter
DATE_FORMAT = "%Y-%m-%d %H:%M UTC"
attr_reader :to_phone_number, :start_date
def initialize(to_phone_number:, start_date:)
@to_phone_number = to_phone_number
@start_date = start_date
end
def export
@client = Twilio::REST::Client.new
calls = @client.calls.list(to: to_phone_number, start_time_after: start_date)
attributes = ["From", "Start Time", "End Time", "Duration", "Status", "Forwarded From"]
::CSV.open("export_twilio_calls.csv", "wb") do |csv|
csv << attributes
calls.each do |res|
csv << [
res.from,
res.start_time.strftime(DATE_FORMAT),
res.end_time.strftime(DATE_FORMAT),
res.duration,
res.status,
res.forwarded_from
]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment