Amazebot / bbot-rocketchat-boilerplate

bRocket is a bBot boilerplate for building Rocket.Chat bots
MIT License
13 stars 8 forks source link

How can I write any message in any channel programatically instead of b.respond()? #5

Open online0227 opened 5 years ago

online0227 commented 5 years ago

How can I write any message in any channel programatically instead of b.respond()?

There are lots of examples about using b.respond but nothing about writing programatically instead of responding to something.

timkinnane commented 5 years ago

@online0227 - You can use the bot's dispatch method, to send an envelope, which can contain strings and/or attachment/s. The easiest way to compose the attributes of the envelope (other than passing them all into the constructor) is using its helpers...

const envelope = new bot.Envelope()
  .toRoomId('111')
  .write('hello')
bot.dispatch(envelope)

There aren't any examples I could find easily, but the envelope.ts code is well commented so you can see the methods there. See here. I haven't versioned the docs generation, so the exact usage for your version might involve trial and error.

The rocketchat boilerplate also includes an example of using envelope methods to add quick reply buttons as attachments. See here.

anicoa commented 5 years ago

@timkinnane thanks a lot for the information. I try hard to post a dm to a user but right now im stuck. can you shed some light how username has to be specified? tried it

new bot.Envelope( {user: {name: 'username'}, method: 'dm' }) and new bot.Envelope( {user: {id: 'userid'}, method: 'dm' })

but I only get the follwing errors:

info:   [thought] dispatch envelope f6d0b5763928a2baabfbbb37642c1d04
debug:  [thought] global respond processing incoming message ID 3aba1b126cf5e9d6e04822d57c424d36
debug:  [createDirectMessage] Calling (cached): undefined
error:  [createDirectMessage] Error:Match failed [400]

would be very happy about any help! thanks a lot for the great work!

Update1: ok, seems that we can send dm messages to the room-id as the bot does in reply to the ping back command: info: [sendMessage] Calling (async): [{"msg":"Ping :ping_pong:","bot":{"i":"js.SDK"},"rid":"YyPCGWjMvP4ZmPgkkqNpPXZ7moo3QKudmS"}]

But how do we get the room-id especially for direct messages?

anicoa commented 5 years ago

to answer the question by my self:

const user = { username: 'da' }
bot.dispatch(new bot.Envelope({ user, method:'dm' }).compose('hello'))
anicoa commented 5 years ago

ok, nevertheless: i can't get sending to channel by channel-name wo work.

The example from http://bbot.chat/docs/hubot

const room = { name: 'general' }
const user = { name: 'bilbo' }
bot.dispatch(new bot.Envelope({ room }).compose('hello #' + room.name))

throws an error complaining that no roomID has been provided in node_modules/bbot/dist/adapters/rocketchat.js line 172 and the send-case only calls this.driver.sendToRoomId and does not call this.driver.sendToRoom if only room.name but not room.id is provided.

If I implement the call to this.driver.sendToRoom accordingly its working fine.

@timkinnane can we discuss if this is correct? if it is i can do a pr...

timkinnane commented 5 years ago

@anicoa You're totally right. Seems the docs do not match the implementation. I'd be happy to accept a PR to fix it.

Seems like the condition below should be updated to throw if no room and no ID, otherwise if room name given without ID, use driver.sendToRoom.

https://github.com/Amazebot/bbot/blob/67c0088a807d3b57c2094f04f50e37314b034539/packages/bbot-message-rocketchat/src/rocketchat.ts#L112

I've been working on the next major release for a long time (without much time to progress it lately) and it's moved far away from master, so I would apply any changes as a patch to the 1.4.0 release and carry across your changes manually into my v2 dev branch.

If it helps, you could also chain the envelope utility methods to set the params you need, which might be more semantic. e.g. bot.dispatch(new bot.Envelope().toUser(user).via('dm').compose('hello'))

Thanks for your efforts.