Skip to content

Commit

Permalink
Merge pull request #32 from loddit/message_room
Browse files Browse the repository at this point in the history
add robot.messageRoom support
  • Loading branch information
bcho authored Jun 7, 2017
2 parents 7334dd9 + cbbbecc commit bc3c6d5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ robot.hear /how old are you?/, (res) ->

![art/res_reply.png](art/res_reply.png)

### Send message to other room

If you want to send a message to other channel use `robot.messageRoom` with vchannel_id, and Channel Name support is coming soon:

```
robot.hear /voldemort/i, (res) ->
robot.messageRoom(vchannel_id, "Somebody is talking about you, Voldemort!")
```

### `bearychat.attachment`

If hubot want to response more than text, emit `bearychat.attachment`:
Expand Down
9 changes: 9 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ robot.hear /how old are you?/, (res) ->

![art/res_reply.png](art/res_reply.png)

### 向其他讨论组发消息

如果想让 Hubot 往非当前对话的讨论组发送消息可以使用 `robot.messageRoom`, 参数是 vchannelId,讨论组名称也即将支持。

```
robot.hear /voldemort/i, (res) ->
robot.messageRoom(vchannel_id, "Somebody is talking about you, Voldemort!")
```

### 富文本回复 `bearychat.attachment`

如果 hubot 想要回复富文本消息,可以发送 `bearychat.attachment` 事件:
Expand Down
8 changes: 6 additions & 2 deletions src/bearychat.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ class BearyChatAdapter extends Adapter
.catch((err) => @robot.logger.error 'send message failed', err)

send: (envelope, strings...) ->
message = @client.packMessage false, envelope, strings
@client.sendMessage envelope, message
if envelope.room # robot.messageRoom
message = {text: strings[0]}
@client.sendMessageToRoom envelope, message
else
message = @client.packMessage false, envelope, strings
@client.sendMessage envelope, message

reply: (envelope, strings...) ->
message = @client.packMessage true, envelope, strings
Expand Down
16 changes: 14 additions & 2 deletions src/http_client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,22 @@ class HTTPClient extends EventEmitter
@emit(EventError, err)
@robot.logger.error 'send message failed', err

sendMessageToRoom: (envelope, message) ->
vchannelId = envelope.room
bearychat.message.create({
token: @tokens[0],
vchannel_id: vchannelId,
text: message.text,
attachments: message.attachments or []
})

packMessage: (isReply, envelope, [text, opts]) ->
text = "@#{envelope.user.name}: #{text}" if isReply
Object.assign opts || {},{sender: envelope.user.sender,vchannel_id: envelope.user.vchannel,text: text}

Object.assign opts || {}, {
sender: envelope.user.sender,
vchannel_id: envelope.user.vchannel,
text: text
}

receiveMessageCallback: (req, res) ->
body = req.body
Expand Down
12 changes: 11 additions & 1 deletion src/rtm_client.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
EventEmitter = require 'events'
WebSocket = require 'ws'
{ User, TextMessage } = require 'hubot'
{ rtm } = require 'bearychat'
bearychat = require 'bearychat'
rtm = bearychat.rtm

{
EventConnected,
Expand Down Expand Up @@ -91,6 +92,15 @@ class RTMClient extends EventEmitter
sendMessage: (envelope, message) ->
@writeWebSocket message

sendMessageToRoom: (envelope, message) ->
vchannelId = envelope.room
bearychat.message.create({
token: @token,
vchannel_id: vchannelId,
text: message.text,
attachments: []
})

connectToRTM: (wsHost) ->
@rtmCallId = 0
@rtmConn = new WebSocket wsHost
Expand Down

0 comments on commit bc3c6d5

Please sign in to comment.