Skip to content

Commit

Permalink
Fix merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
geekgonecrazy committed Aug 22, 2016
2 parents 1fec2b6 + d3bd57b commit 9245376
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 67 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM node:0.12.4
MAINTAINER Sing Li <[email protected]>
MAINTAINER Rocket.Chat Team <[email protected]>

RUN npm install -g coffee-script yo generator-hubot && \
useradd hubot -m
Expand Down
40 changes: 37 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ Hubot adapter for Rocket.Chat!

Feel free to join us in the [#hubot](https://demo.rocket.chat/channel/hubot) channel to discuss hubot, and any scripts you might be working on.

## Important

v1.0.x of the adapter introduces breaking changes. It is only compatible with v0.37.0 and higher of Rocket.Chat Server.

If you are using an older version of Rocket.Chat please use v0.1.4 of the adapter.

#### NOTE
If you want to integrate Rocket.Chat with GitHub or GitLab. Make sure you visit the [Rocket.Chat.Ops](https://github.com/RocketChat/Rocket.Chat.Ops) project before starting. We already have many scripts that add webhook events and access GitHub/GitLab APIs. You can easily extend these scripts for your custom application.

Expand Down Expand Up @@ -54,6 +60,34 @@ docker run -it -e ROCKETCHAT_URL=<your rocketchat instance>:<port> \
rocketchat/hubot-rocketchat
```

### Docker-compose

If you want to use docker-compose for this task, add this for v0.1.4 adapter (this must be inserted in your docker-compose.yml):

```
# hubot, the popular chatbot (add the bot user first and change the password before starting this image)
hubot:
image: rocketchat/hubot-rocketchat:v0.1.4
environment:
- ROCKETCHAT_URL=your-rocket-chat-instance-ip:3000 (e.g. 192.168.2.240:3000)
- ROCKETCHAT_ROOM=
- LISTEN_ON_ALL_PUBLIC=true
- ROCKETCHAT_USER=username-of-your-bot
- ROCKETCHAT_PASSWORD=yourpass
- BOT_NAME=bot
- GOOGLE_API_KEY=yourgoogleapikey
# you can add more scripts as you'd like here, they need to be installable by npm
- EXTERNAL_SCRIPTS=hubot-help,hubot-seen,hubot-links,hubot-diagnostics,hubot-google,hubot-reddit,hubot-bofh,hubot-bookmark,hubot-shipit,hubot-maps
links:
- rocketchat:rocketchat
# this is used to expose the hubot port for notifications on the host on port 3001, e.g. for hubot-jenkins-notifier
ports:
- 3001:8080
```

If you wish that your bot listen to all public rooms and all private rooms he is joined to let the env "ROCKETCHAT_ROOM" empty like in the example above and set the env "LISTEN_ON_ALL_PUBLIC" to true.

Please take attention to some external scripts that are in the example above, some of them need your Google-API-Key in the docker compose file.

### Add adapter to hubot

Expand All @@ -71,15 +105,15 @@ Then you need to start the setup of the bot
```
mkdir myhubot
cd myhubot
yo hubot --adapter="rocketchat"
yo hubot --adapter="rocketchat@0.1"
```

It'll ask you a few questions.

Alternatively you can actually answer the questions in one command:

```
yo hubot --owner="OWNER <[email protected]>" --name="bot" --description="Bot" --adapter="rocketchat"
yo hubot --owner="OWNER <[email protected]>" --name="bot" --description="Bot" --adapter="rocketchat@0.1"
```

Also be sure to remember the name you specify. This is what the bot will respond to in Rocket.Chat.
Expand All @@ -101,7 +135,7 @@ Then start with: `bin/hubot -a rocketchat`
#### Existing install
If you already have hubot setup you can add the adapter.

By doing: `npm install hubot-rocketchat`
By doing: `npm install hubot-rocketchat@0.1`

You will need to tell the adapter where your install is and what login information to use.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hubot-rocketchat",
"version": "0.1.4",
"version": "1.0.2",
"author": {
"name": "Rocket.Chat",
"url": "http://rocket.chat/"
Expand Down
106 changes: 48 additions & 58 deletions src/rocketchat.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ Q = require 'q'
Chatdriver = require './rocketchat_driver'

RocketChatURL = process.env.ROCKETCHAT_URL or "localhost:3000"
RocketChatRoom = process.env.ROCKETCHAT_ROOM or "GENERAL"
RocketChatRoom = process.env.ROCKETCHAT_ROOM or "GENERAL" # Rooms to auto join
RocketChatUser = process.env.ROCKETCHAT_USER or "hubot"
RocketChatPassword = process.env.ROCKETCHAT_PASSWORD or "password"
ListenOnAllPublicRooms = process.env.LISTEN_ON_ALL_PUBLIC or "false"
RespondToDirectMessage = process.env.RESPOND_TO_DM or "false"
RespondToEditedMessage = (process.env.RESPOND_TO_EDITED or "false").toLowerCase()
ListenOnAllPublicRooms = (process.env.LISTEN_ON_ALL_PUBLIC or "false").toLowerCase() is 'true'
RespondToDirectMessage = (process.env.RESPOND_TO_DM or "false").toLowerCase() is 'true'
RespondToEditedMessage = (process.env.RESPOND_TO_EDITED or "false").toLowerCase() is 'true'
SSLEnabled = "false"

if ListenOnAllPublicRooms
RocketChatRoom = ''

# Custom Response class that adds a sendPrivate and sendDirect method
class RocketChatResponse extends Response
sendDirect: (strings...) ->
Expand Down Expand Up @@ -90,7 +93,7 @@ class RocketChatBotAdapter extends Adapter
roomids = []
for room in rooms
do(room) =>
roomids.push @chatdriver.getRoomId(room)
roomids.push @chatdriver.getRoomId(room)

return Q.all(roomids)
.catch((roomErr) =>
Expand All @@ -116,67 +119,52 @@ class RocketChatBotAdapter extends Adapter
# Subscribe to msgs in all rooms
.then((res) =>
@robot.logger.info "All rooms joined."
subs = []
for result, idx in res
@robot.logger.info "Successfully joined room: #{rooms[idx]}"
subs.push @chatdriver.prepMeteorSubscriptions({uid: userid, roomid: rooms[idx]})

return Q.all(subs)
return @chatdriver.prepMeteorSubscriptions({uid: userid, roomid: '__my_messages__'})
.catch((subErr) =>
@robot.logger.error "Unable to subscribe: #{JSON.stringify(subErr)} Reason: #{subErr.reason}"
throw subErr
)
)
# Setup msg callbacks
.then((results) =>
@robot.logger.info "All subscriptions ready."
for result, idx in results
@robot.logger.info "Successfully subscribed to room: #{rooms[idx]}"

@chatdriver.setupReactiveMessageList (newmsg) =>
if (newmsg.u._id isnt userid) || (newmsg.t is 'uj')
if (newmsg.rid in room_ids) || (ListenOnAllPublicRooms.toLowerCase() is 'true') || ((RespondToDirectMessage.toLowerCase() is 'true') && (newmsg.rid.indexOf(userid) > -1))
curts = new Date(newmsg.ts.$date)
if (RespondToEditedMessage is 'true') and (newmsg.editedAt?.$date?)
edited = new Date(newmsg.editedAt.$date)
curts = if edited > curts then edited else curts
@robot.logger.info "Message receive callback id " + newmsg._id + " ts " + curts
@robot.logger.info "[Incoming] #{newmsg.u.username}: #{if newmsg.file? then newmsg.attachments[0].title else newmsg.msg}"

if curts > @lastts
@lastts = curts
if newmsg.t isnt 'uj'
user = @robot.brain.userForId newmsg.u._id, name: newmsg.u.username, room: newmsg.rid

# check for the presence of attachments in the message
if newmsg.file? and newmsg.attachments.length
attachment = newmsg.attachments[0]

if attachment.image_url?
attachment.link = "#{RocketChatURL}#{attachment.image_url}"
attachment.type = 'image'
else if attachment.audio_url?
attachment.link = "#{RocketChatURL}#{attachment.audio_url}"
attachment.type = 'audio'
else if attachment.video_url?
attachment.link = "#{RocketChatURL}#{attachment.video_url}"
attachment.type = 'video'

message = new AttachmentMessage user, attachment, attachment.title, newmsg._id
else
message = new TextMessage user, newmsg.msg, newmsg._id

isDM = newmsg.rid.indexOf(userid) > -1
startOfText = if message.text.indexOf('@') == 0 then 1 else 0
robotIsNamed = message.text.indexOf(@robot.name) == startOfText || message.text.indexOf(@robot.alias) == startOfText
if (isDM and not robotIsNamed)
message.text = "#{ @robot.name } #{ message.text }"
@robot.receive message
@robot.logger.info "Message sent to hubot brain."
else # enter room message
if newmsg.u._id isnt userid
user = @robot.brain.userForId newmsg.u._id, name: newmsg.u.username, room: newmsg.rid
@robot.receive new EnterMessage user, null, newmsg._id
.then(() =>
@robot.logger.info "Successfully subscribed to messages"

@chatdriver.setupReactiveMessageList (newmsg, messageOptions) =>
if newmsg.u._id is userid
return

isDM = messageOptions.roomType is 'd'

if isDM and not RespondToDirectMessage
return

if not isDM and not messageOptions.roomParticipant and not ListenOnAllPublicRooms
return

curts = new Date(newmsg.ts.$date)
if RespondToEditedMessage and newmsg.editedAt?.$date?
edited = new Date(newmsg.editedAt.$date)
curts = if edited > curts then edited else curts
@robot.logger.info "Message receive callback id " + newmsg._id + " ts " + curts
@robot.logger.info "[Incoming] #{newmsg.u.username}: #{newmsg.msg}"

if curts > @lastts
@lastts = curts
if newmsg.t is 'uj'
user = @robot.brain.userForId newmsg.u._id, name: newmsg.u.username, room: newmsg.rid
@robot.receive new EnterMessage user, null, newmsg._id
else
user = @robot.brain.userForId newmsg.u._id, name: newmsg.u.username, room: newmsg.rid
message = new TextMessage user, newmsg.msg, newmsg._id
startOfText = if message.text.indexOf('@') == 0 then 1 else 0
robotIsNamed = message.text.indexOf(@robot.name) == startOfText || message.text.indexOf(@robot.alias) == startOfText
if isDM and not robotIsNamed
message.text = "#{ @robot.name } #{ message.text }"
@robot.receive message
@robot.logger.info "Message sent to hubot brain."
)
.then(() =>
@emit 'connected'
Expand Down Expand Up @@ -206,7 +194,9 @@ class RocketChatBotAdapter extends Adapter

reply: (envelope, strings...) =>
@robot.logger.info "reply"
strings = strings.map (s) -> "@#{envelope.user.name} #{s}"
isDM = envelope.room.indexOf(envelope.user.id) > -1
unless isDM
strings = strings.map (s) -> "@#{envelope.user.name} #{s}"
@send envelope, strings...

callMethod: (method, args...) =>
Expand Down
8 changes: 4 additions & 4 deletions src/rocketchat_driver.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ LRU = require('lru-cache')

# TODO: need to grab these values from process.env[]

_msgsubtopic = 'stream-messages' # 'messages'
_msgsubtopic = 'stream-room-messages' # 'messages'
_msgsublimit = 10 # this is not actually used right now
_messageCollection = 'stream-messages'
_messageCollection = 'stream-room-messages'

# room id cache
_roomCacheSize = parseInt(process.env.ROOM_ID_CACHE_SIZE) || 10
Expand Down Expand Up @@ -107,7 +107,7 @@ class RocketChatDriver
# data.roomid
# return promise
@logger.info "Preparing Meteor Subscriptions.."
msgsub = @asteroid.subscribe _msgsubtopic, data.roomid, _msgsublimit
msgsub = @asteroid.subscribe _msgsubtopic, data.roomid, true
@logger.info "Subscribing to Room: #{data.roomid}"
return msgsub.ready

Expand All @@ -127,7 +127,7 @@ class RocketChatDriver
# console.log('changed:', JSON.stringify(changedMsg, null, 2));
if changedMsg.args?
@logger.info "Message received with ID " + id
receiveMessageCallback changedMsg.args[1]
receiveMessageCallback changedMsg.args[0], changedMsg.args[1]

callMethod: (name, args = []) =>
@logger.info "Calling: #{name}, #{args.join(', ')}"
Expand Down

0 comments on commit 9245376

Please sign in to comment.