forked from tyage/slack-patron
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Slack Events API対応 #39
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fda7b92
bundle add rack
cookie-s 7bc4346
logger: implement events api receiver
cookie-s a9bca05
logger: events api http port
cookie-s 919969c
fix
cookie-s 86bbf9b
logger: fix
cookie-s 6fdb70b
use_events_api
cookie-s 71c8bba
logger: port 9293
cookie-s File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1305,6 +1305,7 @@ DEPENDENCIES | |
mongo | ||
nokogiri | ||
puma | ||
rack (~> 2.2) | ||
rubyzip | ||
sinatra | ||
slack-ruby-client | ||
|
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
require 'rack' | ||
|
||
class SlackEventsReceiver | ||
attr_reader :logger | ||
|
||
def initialize(team_id) | ||
@team_id = team_id | ||
raise ArgumentError unless @team_id and @team_id.start_with? 'T' | ||
end | ||
|
||
def start!(logger) | ||
@logger = logger | ||
|
||
Rack::Server.start( | ||
app: lambda do |env| | ||
req = Rack::Request.new(env) | ||
|
||
begin | ||
Slack::Events::Request.new(req).verify! | ||
process(req.body) | ||
rescue Slack::Events::Request::MissingSigningSecret, | ||
Slack::Events::Request::InvalidSignature, | ||
Slack::Events::Request::TimestampExpired => e | ||
warn 'bad request: %s' % e | ||
[400, {}, ''] | ||
end | ||
end, | ||
Port: 9293, | ||
) | ||
end | ||
|
||
def process(body) | ||
# https://api.slack.com/apis/connections/events-api | ||
|
||
data = JSON.parse(body.read) | ||
type = data['type'] | ||
|
||
case type | ||
when 'url_verification' | ||
[200, { 'content-type': 'text/plain' }, data['challenge']] | ||
when 'event_callback' | ||
process_event data['event'] if data['team_id'] == @team_id | ||
[204, {}, ''] | ||
else | ||
[400, {}, ''] | ||
end | ||
end | ||
|
||
def process_event(event) | ||
type = event['type'] | ||
event.delete 'type' | ||
|
||
case type | ||
when 'message' | ||
# https://api.slack.com/events/message | ||
return if logger.is_private_channel(event['channel']) | ||
return if logger.is_direct_message(event['channel']) | ||
|
||
puts 'new message' | ||
|
||
logger.insert_message(event) | ||
|
||
when 'team_join' | ||
puts 'new user has joined' | ||
logger.update_users | ||
|
||
when 'user_change' | ||
puts 'user data has changed' | ||
logger.update_users | ||
|
||
when 'channel_created' | ||
puts 'channel has created' | ||
logger.update_channels | ||
|
||
when 'channel_rename' | ||
puts 'channel has renamed' | ||
logger.update_channels | ||
|
||
when 'emoji_changed' | ||
puts 'emoji has changed' | ||
logger.update_emojis | ||
end | ||
end | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
class SlackRTMReceiver | ||
def start!(logger) | ||
realtime = Slack::RealTime::Client.new | ||
|
||
realtime.on :message do |m| | ||
if logger.is_private_channel(m['channel']) | ||
next | ||
end | ||
if logger.is_direct_message(m['channel']) | ||
next | ||
end | ||
|
||
puts 'new message' | ||
logger.insert_message(m) | ||
end | ||
|
||
realtime.on :team_join do |e| | ||
puts "new user has joined" | ||
logger.update_users | ||
end | ||
|
||
realtime.on :user_change do |e| | ||
puts "user data has changed" | ||
logger.update_users | ||
end | ||
|
||
realtime.on :channel_created do |c| | ||
puts "channel has created" | ||
logger.update_channels | ||
end | ||
|
||
realtime.on :channel_rename do |c| | ||
puts "channel has renamed" | ||
logger.update_channels | ||
end | ||
|
||
realtime.on :emoji_changed do |c| | ||
puts "emoji has changed" | ||
logger.update_emojis | ||
end | ||
|
||
# if connection closed, restart the realtime logger | ||
realtime.on :close do | ||
puts "websocket disconnected" | ||
start! logger | ||
end | ||
|
||
realtime.start! | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
require './lib/slack' | ||
|
||
require './lib/slack_logger' | ||
require './lib/slack_events_receiver' | ||
require './lib/slack_rtm_receiver' | ||
|
||
config = YAML.load_file('./config.yml') | ||
|
||
SlackLogger.new.start | ||
receiver = config['slack']['use_events_api'] ? SlackEventsReceiver.new(config['slack']['team_id']) : SlackRTMReceiver.new | ||
SlackLogger.new.start receiver |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
参考:
https://github.com/slack-ruby/slack-ruby-client/pull/369