Skip to content

Instantly share code, notes, and snippets.

@TrevorS
Created December 27, 2016 22:24
Show Gist options
  • Save TrevorS/d7b39278b2c289f7fe6a6a0c2f40d8db to your computer and use it in GitHub Desktop.
Save TrevorS/d7b39278b2c289f7fe6a6a0c2f40d8db to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
require 'httparty'
require 'csv'
GROUP_ID = 'PUT THE GROUP ID HERE'
BEFORE_ID = 'PUT THE LAST MESSAGE BEFORE ID HERE'
class GroupMe
include HTTParty
@token = 'PUT THE API TOKEN HERE'
base_uri 'https://api.groupme.com/v3'
def self.messages(group_id, before_id, limit)
get("/groups/#{group_id}/messages?token=#{@token}&before_id=#{before_id}&limit=#{limit}")
end
def self.all_messages(group_id, starting_before_id)
msgs = []
new_before_id = nil
loop do
before_id = new_before_id || starting_before_id
results = messages(group_id, before_id, 100)
if results['meta'] && results['meta']['code'] == 200
puts 'success'
elsif results['meta'] && results['meta']['code'] != 200
puts "failed: #{results}"
break
else
puts 'done'
break
end
new_msgs = results['response']['messages']
new_before_id = new_msgs.last['id']
msgs.concat(new_msgs)
end
msgs
end
end
messages = GroupMe.all_messages(GROUP_ID, BEFORE_ID)
puts 'Writing CSV'
CSV.open("chat_log_#{Time.now.to_i}.csv", 'wb') do |csv|
csv << %w(created_at channel name text)
messages.reverse.each do |msg|
csv << [
msg['created_at'],
'general',
msg['name'],
msg['text']
]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment