Sha256: 27bb32200a4fd97b0f72c415d622cbca31dd6426b548675bccdfa557a14611b7

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

#!/usr/bin/env ruby

require 'pry'
require 'set'

def rate_limited?(response)
	pp response
	
	response[:error] == "ratelimited"
end

require 'async/rest'

URL = "https://slack.com/api"
TOKEN = "xoxp-your-api-token"

Async::REST::Resource.for(URL) do |resource|
	authenticated = resource.with(parameters: {token: TOKEN})
	delete = authenticated.with(path: "chat.delete")
	
	page = 1
	while true
		search = authenticated.with(path: "search.messages", parameters: {page: page, count: 100, query: "from:@username before:2019-02-15"})
		representation = search.get
		
		messages = representation.value[:messages]
		matches = messages[:matches]
		
		puts "Found #{matches.count} messages on page #{page}..."
		
		break if matches.empty?
		
		matches.each do |message|
			text = message[:text]
			channel_id = message[:channel][:id]
			channel_name = message[:channel][:name]
			timestamp = message[:ts]
			
			pp [timestamp, channel_name, text]
			
			message_delete = Async::REST::Representation.new(
				delete.with(parameters: {channel: channel_id, ts: timestamp})
			)
			
			response = message_delete.post
			if rate_limited?(response.read)
				puts "Rate limiting..."
				Async::Task.current.sleep 2
			end
		end
		
		page += 1
	end
end

puts "Done"

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
async-rest-0.7.3 examples/slack/clean.rb