class Ruboty::Adapters::Teams

Public Instance Methods

run() click to toggle source
# File lib/ruboty/adapters/teams.rb, line 20
def run
  @res = {}
  listen
end
say(message) click to toggle source
# File lib/ruboty/adapters/teams.rb, line 25
def say(message)
  body = if message[:formatted]
           message[:body]
         elsif message[:code]
           "<pre>#{message[:body]}</pre>"
         else
           format(message[:body])
         end
  @res = {
    type: "message",
    text: body,
  }
end

Private Instance Methods

auth?(text, hmac) click to toggle source
# File lib/ruboty/adapters/teams.rb, line 103
def auth?(text, hmac)
  return if hmac.nil?
  hash = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), secret, text)).strip
  "HMAC #{hash}" == hmac
end
cert() click to toggle source
# File lib/ruboty/adapters/teams.rb, line 81
def cert
  params = {
    SSLCertificate: OpenSSL::X509::Certificate.new(File.read(ENV["TEAMS_SERVER_CERT"])),
    SSLPrivateKey: OpenSSL::PKey::RSA.new(File.read(ENV["TEAMS_SERVER_KEY"])),
  }

  if ENV["TEAMS_SERVER_CHAIN_CERT"]
    params.merge!(
      SSLExtraChainCert: [OpenSSL::X509::Certificate.new(File.read(ENV["TEAMS_SERVER_CHAIN_CERT"]))],
    )
  end
  params
end
dir() click to toggle source
# File lib/ruboty/adapters/teams.rb, line 95
def dir
  ENV["TEAMS_SERVER_ENDPOINT"] ||= '/webhooks'
end
format(text) click to toggle source
# File lib/ruboty/adapters/teams.rb, line 118
def format(text)
  text
    .gsub(/```\n?(.+?)\n?```/m, '<pre>\1</pre>')
    .gsub(/\n/, '<br>')
    .gsub(URI::DEFAULT_PARSER.make_regexp(%w[http https]), '[\0](\0)')
end
ip_address() click to toggle source
# File lib/ruboty/adapters/teams.rb, line 73
def ip_address
  ENV["TEAMS_SERVER_IP_ADDRESS"] ||= '0.0.0.0'
end
listen() click to toggle source
# File lib/ruboty/adapters/teams.rb, line 41
def listen
  server.start
end
parse_content(body) click to toggle source
# File lib/ruboty/adapters/teams.rb, line 109
def parse_content(body)
  json = JSON.parse(body)
  {
    text: '@' + HTMLEntities.new.decode(Sanitize.fragment(json['text'])).gsub(/\xC2\xA0/, ' ').strip,
    from: json['from']['name'],
    id: json['id'],
  }
end
port() click to toggle source
# File lib/ruboty/adapters/teams.rb, line 77
def port
  ENV["TEAMS_SERVER_PORT"] ||= 443
end
secret() click to toggle source
# File lib/ruboty/adapters/teams.rb, line 99
def secret
  ENV["TEAMS_SECURITY_TOKEN"].unpack("m")[0]
end
server() click to toggle source
# File lib/ruboty/adapters/teams.rb, line 45
def server
  server = WEBrick::HTTPServer.new({
                                     BindAddress: ip_address,
                                     Port: port,
                                     SSLEnable: true,
                                   }.merge(cert))

  server.mount_proc dir do |req, res|
    hmac = req.header.dig('authorization', 0)
    body = req.body

    if auth?(body, hmac)
      message = parse_content(body)

      robot.receive(
        body: message[:text],
        from: message[:from],
        message_id: message[:id],
      )
      res.body = @res.to_json
      @res = {}
    else
      res.status = 403
    end
  end
  server
end