commit fa34e0c422a526582153e8f4c9c7af18681f6289 Author: mose Date: Sat Jul 7 11:46:11 2018 +0800 initial draft diff --git a/.bundle/config b/.bundle/config new file mode 100644 index 0000000..ba922fd --- /dev/null +++ b/.bundle/config @@ -0,0 +1,2 @@ +--- +BUNDLE_PATH: "vendor" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7c37cbe --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +config.yml +vendor diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..aab5835 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gem 'rocketchat' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..2af0362 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,13 @@ +GEM + remote: https://rubygems.org/ + specs: + rocketchat (0.1.15) + +PLATFORMS + ruby + +DEPENDENCIES + rocketchat + +BUNDLED WITH + 1.16.2 diff --git a/config.yml.sample b/config.yml.sample new file mode 100644 index 0000000..329daa6 --- /dev/null +++ b/config.yml.sample @@ -0,0 +1,3 @@ +server: 'http://your.server.address' +user: 'user' +pass: 'password' diff --git a/lib/rocketchat/channel.rb b/lib/rocketchat/channel.rb new file mode 100644 index 0000000..41ea1a4 --- /dev/null +++ b/lib/rocketchat/channel.rb @@ -0,0 +1,16 @@ +module RocketChat + module Messages + class Channel < Room + def history(room: nil, since: nil, count: 10) + response = session.request_json( + '/api/v1/channels.history', + body: { + roomId: room, + oldest: since, + count: count + }) + response['messages'] if response['success'] + end + end + end +end \ No newline at end of file diff --git a/lib/rocketweekly.rb b/lib/rocketweekly.rb new file mode 100644 index 0000000..d4346bd --- /dev/null +++ b/lib/rocketweekly.rb @@ -0,0 +1,6 @@ +$:.unshift File.dirname(__FILE__) unless $:.include? File.dirname(__FILE__) + +require 'rocketchat' +require 'rocketchat/channel' + +require 'rocketweekly/connect' \ No newline at end of file diff --git a/lib/rocketweekly/connect.rb b/lib/rocketweekly/connect.rb new file mode 100644 index 0000000..9561993 --- /dev/null +++ b/lib/rocketweekly/connect.rb @@ -0,0 +1,19 @@ +module RocketWeekly + class Connect + attr_reader :session + + def initialize + config = YAML.load_file(File.expand_path('../../../config.yml', __FILE__)) + server = RocketChat::Server.new config['server'] + @session = server.login(config['user'], config['pass']) + end + + def channels_list + @session.channels.list(offset: 0, count: 200) + end + + def channel_history(id) + @session.channels.history(room: id) + end + end +end \ No newline at end of file diff --git a/run.rb b/run.rb new file mode 100644 index 0000000..78e0df9 --- /dev/null +++ b/run.rb @@ -0,0 +1,21 @@ +require 'yaml' +require_relative './lib/rocketweekly' + +rocket = RocketWeekly::Connect.new + +output = {} +rocket.channels_list.each do |channel| + output[channel.name] = { + 'id' => channel.id, + 'owner' => channel.owner ? channel.owner['username'] : '----' + } + puts '--------------' + puts channel.name + rocket.channel_history(channel.id).each do |m| + printf "%s %-20s\n", m['u']['username'], m['msg'] + puts + end + break +end + +puts '--------------'