Skip to content

Commit

Permalink
use middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
pyama committed Jan 23, 2024
1 parent 0c2b237 commit b077bb9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 43 deletions.
32 changes: 19 additions & 13 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ GEM
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
async (2.4.0)
async (2.8.0)
console (~> 1.10)
fiber-annotation
io-event (~> 1.1)
timers (~> 4.1)
async-io (1.34.3)
async-io (1.41.0)
async
async-websocket (0.8.0)
async-io
Expand All @@ -27,7 +28,8 @@ GEM
deep_merge (~> 1.2, >= 1.2.1)
dry-validation (~> 1.0, >= 1.0.0)
connection_pool (2.4.1)
console (1.16.2)
console (1.23.3)
fiber-annotation
fiber-local
deep_merge (1.2.2)
dotenv (2.8.1)
Expand All @@ -53,7 +55,8 @@ GEM
dry-logic (>= 1.4, < 2)
dry-types (>= 1.7, < 2)
zeitwerk (~> 2.6)
dry-types (1.7.1)
dry-types (1.7.2)
bigdecimal (~> 3.0)
concurrent-ruby (~> 1.0)
dry-core (~> 1.0)
dry-inflector (~> 1.0)
Expand All @@ -66,30 +69,32 @@ GEM
dry-schema (>= 1.12, < 2)
zeitwerk (~> 2.6)
event_stream_parser (1.0.0)
faraday (2.7.12)
base64
faraday-net_http (>= 2.0, < 3.1)
ruby2_keywords (>= 0.0.4)
faraday (2.9.0)
faraday-net_http (>= 2.0, < 3.2)
faraday-mashify (0.1.1)
faraday (~> 2.0)
hashie
faraday-multipart (1.0.4)
multipart-post (~> 2)
faraday-net_http (3.0.2)
faraday-net_http (3.1.0)
net-http
fiber-annotation (0.2.0)
fiber-local (1.0.0)
foreman (0.87.2)
gli (2.21.1)
hashie (5.0.0)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
io-event (1.1.6)
io-event (1.4.2)
kgio (2.11.4)
minitest (5.21.2)
multi_json (1.15.0)
multipart-post (2.3.0)
mustermann (3.0.0)
ruby2_keywords (~> 0.0.1)
mutex_m (0.2.0)
net-http (0.4.1)
uri
rack (3.0.8)
rack-protection (4.0.0)
base64 (>= 0.1.0)
Expand All @@ -99,7 +104,7 @@ GEM
raindrops (0.20.1)
redis (5.0.8)
redis-client (>= 0.17.0)
redis-client (0.17.1)
redis-client (0.19.1)
connection_pool
ruby-openai (6.3.1)
event_stream_parser (>= 0.3.0, < 2.0.0)
Expand Down Expand Up @@ -134,11 +139,12 @@ GEM
timers (4.3.5)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.4.2)
unicode-display_width (2.5.0)
unicorn (6.1.0)
kgio (~> 2.6)
raindrops (~> 0.7)
websocket-driver (0.7.5)
uri (0.13.0)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.6.12)
Expand Down
55 changes: 26 additions & 29 deletions bot.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
require 'slack-ruby-bot'
require './app'

server = SlackRubyBot::Server.new(
token: ENV['SLACK_API_TOKEN'],
hook_handlers: {
message: [->(client, data) {
return if data.subtype == "channel_join"
return if data.subtype == "bot_message"
message: [lambda { |client, data|
return if data.subtype == 'channel_join'
return if data.subtype == 'bot_message'
return if data.text =~ /\+\+|is up to [0-9]+ points!/
entries = RedisConnection.pool.lrange("registered", 0, -1)

entries = RedisConnection.pool.lrange('registered', 0, -1)
uids = entries.select do |entry|
data.text =~ /<@#{entry}>/
end
Expand All @@ -18,40 +18,37 @@

uids.each do |uid|
message = RedisConnection.pool.get(uid)
if message && c.fetch('enable', 1) == 1
user_presence = App::Model::Store.get(uid)
user_presence["mention_histotry"] ||= []
user_presence["mention_histotry"] = [] if user_presence["mention_histotry"].is_a?(Hash)
user_presence["mention_histotry"] << {
channel: data.channel,
user: data.user,
text: data.text && data.text.gsub(/<@#{uid}>/, ''),
event_ts: data.event_ts
}
App::Model::Store.set(uid, user_presence)
next unless message && c.fetch('enable', 1) == 1

client.say(text: "自動応答: #{message}", channel: data.channel,
thread_ts: data.thread_ts
)
end
user_presence = App::Model::Store.get(uid)
user_presence['mention_histotry'] ||= []
user_presence['mention_histotry'] = [] if user_presence['mention_histotry'].is_a?(Hash)
user_presence['mention_histotry'] << {
channel: data.channel,
user: data.user,
text: data.text && data.text.gsub(/<@#{uid}>/, ''),
event_ts: data.event_ts
}
App::Model::Store.set(uid, user_presence)

client.say(text: "自動応答: #{message}", channel: data.channel,
thread_ts: data.thread_ts)
end
}],
ping: [->(client, data) {
}],
pong: [->(client, data) {
ping: [lambda { |client, data|
}],
pong: [lambda { |client, data|
}]
}
)

Thread.new do
server = TCPServer.new('0.0.0.0', 1234)
loop do
begin
sock = server.accept
line = sock.gets
sock.write("HTTP/1.0 200 OK\n\nok")
sock.close
end
sock = server.accept
line = sock.gets
sock.write("HTTP/1.0 200 OK\n\nok")
sock.close
end
end

Expand Down
2 changes: 1 addition & 1 deletion config.ru
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'sinatra'
require_relative 'app'
require_relative 'app/api'

use Rack::RewindableInput::Middleware
run App::Api

0 comments on commit b077bb9

Please sign in to comment.