class Ruboty::Adapters::Chiwawa

Public Instance Methods

run() click to toggle source
# File lib/ruboty/adapters/cww.rb, line 17
def run
  init
  listen
end
say(message) click to toggle source
# File lib/ruboty/adapters/cww.rb, line 22
def say(message)
  url = URI.parse(message_endpoint_path)
  req = Net::HTTP::Post.new(url.path, headers)
  req.body = JSON.generate(post_message_data(message))
  https = Net::HTTP.new(url.host, access_port)
  https.use_ssl = true
  res = https.start { |https| https.request(req) }
end

Private Instance Methods

access_port() click to toggle source
# File lib/ruboty/adapters/cww.rb, line 133
def access_port
  "443"
end
api_token() click to toggle source
# File lib/ruboty/adapters/cww.rb, line 101
def api_token
  ENV["CHIWAWA_API_TOKEN"]
end
bot_user_id() click to toggle source
# File lib/ruboty/adapters/cww.rb, line 109
def bot_user_id
  ENV["CHIWAWA_BOT_USER_ID"]
end
bot_user_name() click to toggle source
# File lib/ruboty/adapters/cww.rb, line 113
def bot_user_name
  ENV["CHIWAWA_BOT_USER_NAME"]
end
chiwawa_host() click to toggle source
# File lib/ruboty/adapters/cww.rb, line 137
def chiwawa_host
  "#{company_id}.#{chiwawa_root_domain}"
end
chiwawa_root_domain() click to toggle source
# File lib/ruboty/adapters/cww.rb, line 141
def chiwawa_root_domain
  "chiwawa.one"
end
company_id() click to toggle source
# File lib/ruboty/adapters/cww.rb, line 97
def company_id
  ENV["CHIWAWA_COMPANY_ID"]
end
get_messages() click to toggle source
# File lib/ruboty/adapters/cww.rb, line 47
def get_messages
  url = URI.parse(message_get_path)
  req = Net::HTTP::Get.new(url.request_uri, headers)
  https = Net::HTTP.new(url.host, access_port)
  https.use_ssl = true
  res = https.start { |https| https.request(req) }
  messages = JSON.parse(res.body)["messages"] 
  messages = messages.sort_by { |hash| hash['createdAt'].to_i }
  unless messages.last.nil?
    @last_created_at_from = messages.last["createdAt"]
  end

  return messages
rescue => e
  puts "Ruboty::Cww::Adapters::Cww.get_messages error[#{e.message}]."
  return []
end
group_id() click to toggle source
# File lib/ruboty/adapters/cww.rb, line 117
def group_id
  ENV["CHIWAWA_GROUP_ID"]
end
group_name() click to toggle source
# File lib/ruboty/adapters/cww.rb, line 121
def group_name
  ENV["CHIWAWA_GROUP_NAME"]
end
headers() click to toggle source
# File lib/ruboty/adapters/cww.rb, line 145
def headers
  {
    "X-Chiwawa-API-Token" => api_token,
    "Content-Type" => "application/json"
  }
end
init() click to toggle source
# File lib/ruboty/adapters/cww.rb, line 33
def init
  ENV["RUBOTY_NAME"] ||= bot_user_name
  @last_created_at_from = "0"
  @last_fetch_message_ids = []
  set_last_fetch_message_ids(get_messages)
end
listen() click to toggle source
# File lib/ruboty/adapters/cww.rb, line 40
def listen
  loop do
    listen_group
    sleep(3)
  end
end
listen_group() click to toggle source
# File lib/ruboty/adapters/cww.rb, line 65
def listen_group
  messages = get_messages
  messages.each do |m|
    if m["createdBy"] === bot_user_id
      next
    end
    if @last_fetch_message_ids.include?(m["messageId"])
      next
    end

    # botに送りたい情報を設定
    robot.receive(
      body: m["text"]
    )
  end
  set_last_fetch_message_ids(messages)
end
message_endpoint_path() click to toggle source
# File lib/ruboty/adapters/cww.rb, line 125
def message_endpoint_path
  "#{protcol}://#{chiwawa_host}#{public_api_path}/groups/#{group_id}/messages"
end
message_get_path() click to toggle source
# File lib/ruboty/adapters/cww.rb, line 156
def message_get_path
  "#{message_endpoint_path}?createdAtFrom=#{@last_created_at_from}"
end
post_message_data(message) click to toggle source
# File lib/ruboty/adapters/cww.rb, line 90
def post_message_data(message)
  # メッセージ投稿APIに送りたいJSONを設定
  {
    "text": message[:body]
  }
end
protcol() click to toggle source
# File lib/ruboty/adapters/cww.rb, line 129
def protcol
  "https"
end
public_api_path() click to toggle source
# File lib/ruboty/adapters/cww.rb, line 152
def public_api_path
  "/api/public/v1"
end
set_last_fetch_message_ids(messages) click to toggle source
# File lib/ruboty/adapters/cww.rb, line 83
def set_last_fetch_message_ids(messages)
  @last_fetch_message_ids = []
  messages.each do |m|
    @last_fetch_message_ids << m["messageId"]
  end
end
verify_token() click to toggle source
# File lib/ruboty/adapters/cww.rb, line 105
def verify_token
  ENV["CHIWAWA_VERIFY_TOKEN"]
end