Skip to content

Commit

Permalink
Merge pull request #268 from timkinnane/convert-es6
Browse files Browse the repository at this point in the history
Convert es6 update with new SDK dependency
  • Loading branch information
timkinnane authored Mar 19, 2018
2 parents 4c9e6e3 + 6a53e61 commit 69172df
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 796 deletions.
36 changes: 25 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Adapter = require('hubot/src/adapter')
const Response = require('hubot/src/response')
const { TextMessage, EnterMessage, LeaveMessage } = require('hubot/src/message')
const pkg = require('./package.json')
const { driver } = require('rocketchat-bot-driver')
const { driver } = require('rocketchat-sdk')

/** Take configs from environment settings or defaults */
const config = {
Expand Down Expand Up @@ -36,6 +36,9 @@ class AttachmentMessage extends TextMessage {
this.text = text
this.id = id
}
toString () {
return this.attachment
}
}

/** Main API for Hubot on Rocket.Chat */
Expand Down Expand Up @@ -84,7 +87,8 @@ class RocketChatBotAdapter extends Adapter {
throw err
})
.then(() => {
driver.reactToMessages(this.receive.bind(this))
driver.reactToMessages(this.receive.bind(this)) // reactive callback
this.emit('connected') // tells hubot to load scripts
})
}

Expand All @@ -111,7 +115,6 @@ class RocketChatBotAdapter extends Adapter {
if (!config.listenOnAllPublic && !isDM && !msgOpts.roomParticipant) return

// Set current time for comparison to incoming
console.log(message)
let currentReadTime = new Date(message.ts.$date)

// Ignore edited messages if configured to
Expand Down Expand Up @@ -146,13 +149,19 @@ class RocketChatBotAdapter extends Adapter {
})
.then(() => {
// Prepare message type for Hubot to receive...
this.robot.logger.info('Sending message to Hubot middleware')
this.robot.logger.info('Filters passed, will receive message')

// Room joins, receive without further detail
if (message.t === 'uj') return this.robot.receive(new EnterMessage(user, null, message._id))
if (message.t === 'uj') {
this.robot.logger.debug('Message type EnterMessage')
return this.robot.receive(new EnterMessage(user, null, message._id))
}

// Room exit, receive without further detail
if (message.t === 'ul') return this.robot.receive(new LeaveMessage(user, null, message._id))
if (message.t === 'ul') {
this.robot.logger.debug('Message type LeaveMessage')
return this.robot.receive(new LeaveMessage(user, null, message._id))
}

// Direct messages prepend bot's name so Hubot can `.respond`
const startOfText = (message.msg.indexOf('@') === 0) ? 1 : 0
Expand All @@ -172,11 +181,14 @@ class RocketChatBotAdapter extends Adapter {
attachment.link = `${config.url}${attachment.video_url}`
attachment.type = 'video'
}
this.robot.logger.debug('Message type AttachmentMessage')
return this.robot.receive(new AttachmentMessage(user, attachment, message.msg, message._id))
}

// Standard text messages, receive as is
return this.robot.receive(new TextMessage(user, message.msg, message._id))
let textMessage = new TextMessage(user, message.msg, message._id)
this.robot.logger.debug(`TextMessage: ${textMessage.toString()}`)
return this.robot.receive(textMessage)
})
}

Expand Down Expand Up @@ -220,8 +232,7 @@ class RocketChatBotAdapter extends Adapter {

/** Starting config print outs, split-out from logic for easy reading */
startupLogs () {
this.robot.logger.info(`Starting Rocketchat adapter version ${pkg.version}`)
this.robot.logger.info(`Once connected to rooms I will respond to the name: ${this.robot.name}`)
this.robot.logger.info(`[startup] Respond to the name: ${this.robot.name}`)
this.robot.alias = (this.robot.name === config.user || this.robot.alias) ? this.robot.alias : config.user

if (this.robot.alias) {
Expand All @@ -240,10 +251,13 @@ class RocketChatBotAdapter extends Adapter {
this.robot.logger.warning(`No services ROCKETCHAT_USER provided to Hubot, using ${config.user}`)
}

this.robot.logger.info(`Rooms Specified: ${config.room}`)
this.robot.logger.info(`[startup] Rooms specified: ${config.room}`)
}
}

module.exports = {
use: (robot) => new RocketChatBotAdapter(robot)
use: (robot) => {
robot.logger.info(`[startup] Rocket.Chat adapter version ${pkg.version} in use`)
return new RocketChatBotAdapter(robot)
}
}
37 changes: 5 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@
"main": "index.js",
"dependencies": {
"hubot": "https://github.com/hubotio/hubot.git#v3.0.1",
"rocketchat-bot-driver": "^0.0.3"
"rocketchat-sdk": "0.0.0-develop"
}
}
Loading

0 comments on commit 69172df

Please sign in to comment.