lib/slack/smart-bot/comm/ask.rb in slack-smart-bot-1.9.2 vs lib/slack/smart-bot/comm/ask.rb in slack-smart-bot-1.10.0

- old
+ new

@@ -1,63 +1,69 @@ class SlackSmartBot #context: previous message #to: user that should answer def ask(question, context = nil, to = nil, dest = nil) - if dest.nil? and Thread.current.key?(:dest) - dest = Thread.current[:dest] - end - if to.nil? - to = Thread.current[:user].name - end - if context.nil? - context = Thread.current[:command] - end - message = "#{to}: #{question}" - if dest.nil? - if config[:simulate] - open("#{config.path}/buffer_complete.log", "a") { |f| - f.puts "|#{@channel_id}|#{config[:nick_id]}|#{config[:nick]}|#{message}~~~" - } - else - if Thread.current[:on_thread] - client.message(channel: @channel_id, text: message, as_user: true, thread_ts: Thread.current[:thread_ts]) - else - client.message(channel: @channel_id, text: message, as_user: true) - end + begin + if dest.nil? and Thread.current.key?(:dest) + dest = Thread.current[:dest] end - if config[:testing] and config.on_master_bot - open("#{config.path}/buffer.log", "a") { |f| - f.puts "|#{@channel_id}|#{config[:nick_id]}|#{config[:nick]}|#{message}" - } + if to.nil? + to = Thread.current[:user].name end - elsif dest[0] == "C" or dest[0] == "G" # channel - if config[:simulate] - open("#{config.path}/buffer_complete.log", "a") { |f| - f.puts "|#{dest}|#{config[:nick_id]}|#{config[:nick]}|#{message}~~~" - } - else - if Thread.current[:on_thread] - client.message(channel: dest, text: message, as_user: true, thread_ts: Thread.current[:thread_ts]) - else - client.message(channel: dest, text: message, as_user: true) + if context.nil? + context = Thread.current[:command] + end + message = "#{to}: #{question}" + if dest.nil? + if config[:simulate] + open("#{config.path}/buffer_complete.log", "a") { |f| + f.puts "|#{@channel_id}|#{config[:nick_id]}|#{config[:nick]}|#{message}~~~" + } + else + if Thread.current[:on_thread] + client.message(channel: @channel_id, text: message, as_user: true, thread_ts: Thread.current[:thread_ts]) + else + client.message(channel: @channel_id, text: message, as_user: true) + end end + if config[:testing] and config.on_master_bot and !@buffered + @buffered = true + open("#{config.path}/buffer.log", "a") { |f| + f.puts "|#{@channel_id}|#{config[:nick_id]}|#{config[:nick]}|#{message}" + } + end + elsif dest[0] == "C" or dest[0] == "G" # channel + if config[:simulate] + open("#{config.path}/buffer_complete.log", "a") { |f| + f.puts "|#{dest}|#{config[:nick_id]}|#{config[:nick]}|#{message}~~~" + } + else + if Thread.current[:on_thread] + client.message(channel: dest, text: message, as_user: true, thread_ts: Thread.current[:thread_ts]) + else + client.message(channel: dest, text: message, as_user: true) + end + end + if config[:testing] and config.on_master_bot and !@buffered + @buffered = true + open("#{config.path}/buffer.log", "a") { |f| + f.puts "|#{dest}|#{config[:nick_id]}|#{config[:nick]}|#{message}" + } + end + elsif dest[0] == "D" #private message + send_msg_user(dest, message) end - if config[:testing] and config.on_master_bot - open("#{config.path}/buffer.log", "a") { |f| - f.puts "|#{dest}|#{config[:nick_id]}|#{config[:nick]}|#{message}" - } + if Thread.current[:on_thread] + qdest = Thread.current[:thread_ts] + else + qdest = dest end - elsif dest[0] == "D" #private message - send_msg_user(dest, message) + @answer[to] = {} unless @answer.key?(to) + @answer[to][qdest] = context + @questions[to] = context # to be backwards compatible #todo remove it when 2.0 + rescue Exception => stack + @logger.warn stack end - if Thread.current[:on_thread] - qdest = Thread.current[:thread_ts] - else - qdest = dest - end - @answer[to] = {} unless @answer.key?(to) - @answer[to][qdest] = context - @questions[to] = context # to be backwards compatible #todo remove it when 2.0 end end