Created
December 27, 2016 22:24
-
-
Save TrevorS/d7b39278b2c289f7fe6a6a0c2f40d8db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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