0

I want to add a simple user-password authentication to my coffescript for a Hubot. This should act like a "bridge" to send messages to rocketchat. I managed to make a HTTP-Post request without authentication to work.

The script i got so far works pretty good but without authentication:

module.exports = (robot) ->
  robot.router.post '/hubot/say', (req, res) ->

    recipient = req.body.recipient
    message = req.body.message
    dm = req.body.directmessage
    response = "OK"
    robot.logger.info "DM: #{dm}; Recipient: #{recipient}; Message: #{message}"

    if dm == '0'
      #robot.logger.info "Message '#{message}' received for room #{recipient}"

      room = recipient
      user = robot.brain.userForId 'broadcast'
      user.room = room
      user.type = 'groupchat'

      if message
        robot.adapter.send({room, user: {} }, message)

    if dm == '1'
      user = recipient

      if message
        robot.adapter.sendDirect({ user: { name: "#{user}"} }, message)

    res.writeHead 200, {'Content-Type': 'text/plain'}
    res.end response
M1K
  • 11
  • 2

1 Answers1

0

Hubot has a built-in express web framework, robot.router is actually an express router. Hence, you can use basic-auth library or refer to questions like Basic HTTP authentication with Node and Express 4.

Shipra
  • 1,259
  • 2
  • 14
  • 26