Skip to content

Commit

Permalink
feat(payload): Support rich message actions in adapters
Browse files Browse the repository at this point in the history
- Payload helpers return payload for chaining
  • Loading branch information
timkinnane committed Aug 31, 2018
1 parent 0b6e785 commit fba4de6
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -80,7 +80,7 @@
"typescript": "^2.9.0"
},
"dependencies": {
"@rocket.chat/sdk": "^0.2.6",
"@rocket.chat/sdk": "^0.2.7",
"@types/inquirer": "^0.0.42",
"@types/mongoose": "^5.2.0",
"@types/node": "^10.0.0",
Expand Down
37 changes: 34 additions & 3 deletions src/adapters/rocketchat.ts
Expand Up @@ -96,14 +96,15 @@ export class Rocketchat extends bot.MessageAdapter {

/** Parsing envelope content to an array of Rocket.Chat message schemas */
parseEnvelope (envelope: bot.Envelope, roomId?: string) {
const messages = []
const messages: any[] = []
const attachments: any[] = []
const actions: any[] = []
if (envelope.strings) {
for (let text of envelope.strings) {
messages.push(this.driver.prepareMessage(this.format(text), roomId))
}
}
if (envelope.payload.attachments) {
const attachments = []
for (let attachment of envelope.payload.attachments) {
attachments.push({
fields: attachment.fields,
Expand All @@ -121,7 +122,37 @@ export class Rocketchat extends bot.MessageAdapter {
video_url: attachment.video
})
}
if (attachments.length) {
}
if (envelope.payload.quickReplies) {
for (let qr of envelope.payload.quickReplies) {
const { text, type, content, image } = qr
actions.push({
text,
type,
msg: content,
image_url: image,
is_webview: true,
msg_in_chat_window: true
})
}
}

// Append actions to existing attachment if only one,
// otherwise create new attachment for actions.
if (actions.length) {
if (attachments.length === 1) {
attachments[0].actions = actions
} else {
attachments.push({ actions })
}
}

// Append actions to existing attachment if only one,
// otherwise create new attachment for actions.
if (attachments.length) {
if (messages.length === 1) {
messages[0].attachments = attachments
} else {
messages.push(this.driver.prepareMessage({
rid: roomId || envelope.room.id || null,
attachments
Expand Down
11 changes: 10 additions & 1 deletion src/adapters/shell.ts
Expand Up @@ -125,7 +125,16 @@ export class Shell extends bBot.MessageAdapter {
this.messages.push([this.bot.settings.name, text])
}
for (let attachment of (envelope.payload.attachments || [])) {
this.messages.push([this.bot.settings.name, attachment.fallback])
if (attachment.fallback) {
this.messages.push([this.bot.settings.name, attachment.fallback])
}
}
// @todo Use inquirer prompt as UI to select from quick replies
if (envelope.payload.quickReplies) {
this.messages.push([
this.bot.settings.name,
`[${envelope.payload.quickReplies.map((qr) => qr.text).join('], [')}]`
])
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/lib/payload.spec.ts
Expand Up @@ -25,14 +25,12 @@ describe('[payload]', () => {
text: 'Foo'
})
})
it('populates payload with given quick replies', () => {
const payload = new bot.Payload({ quickReplies: [{
text: 'Foo',
content: 'foo'
}] })
it('populates payload with quick reply defaults', () => {
const payload = new bot.Payload({ quickReplies: [{ text: 'Foo' }] })
expect(payload.quickReplies![0]).to.eql({
text: 'Foo',
content: 'foo'
content: 'Foo',
type: 'button'
})
})
})
Expand Down
9 changes: 7 additions & 2 deletions src/lib/payload.ts
Expand Up @@ -66,9 +66,9 @@ export interface IConfirm {

/** Rich message quick reply button, support dependent on messaging platform */
export interface IQuickReply {
type?: string // Type of content (text, phone, email, location)
text: string // Button display text
content: any // Value to submit if clicked
type?: string // Type of content (text, phone, email, location)
content?: any // Value to submit if clicked
image?: string // Button image URL
}

Expand All @@ -89,18 +89,23 @@ export class Payload implements IPayload {
attachment (attachment: IAttachment) {
if (!this.attachments) this.attachments = []
this.attachments.push(attachment)
return this
}

/** Add an action button to the payload */
action (action: IAction) {
if (!this.actions) this.actions = []
this.actions.push(action)
return this
}

/** Add a quick reply button to the payload */
quickReply (quickReply: IQuickReply) {
if (!quickReply.type) quickReply.type = 'button'
if (!quickReply.content) quickReply.content = quickReply.text
if (!this.quickReplies) this.quickReplies = []
this.quickReplies.push(quickReply)
return this
}

/** Get the payload as a plain object */
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Expand Up @@ -82,9 +82,9 @@
lodash "^4.17.5"
to-fast-properties "^2.0.0"

"@rocket.chat/sdk@^0.2.6":
version "0.2.6"
resolved "https://registry.yarnpkg.com/@rocket.chat/sdk/-/sdk-0.2.6.tgz#45c2965b66bdaf5440765c3ecbf0f8e46359ebdc"
"@rocket.chat/sdk@^0.2.7":
version "0.2.7"
resolved "https://registry.yarnpkg.com/@rocket.chat/sdk/-/sdk-0.2.7.tgz#0133dd393838dda18db623124c005fa2c1e06626"
dependencies:
"@types/lru-cache" "^4.1.0"
"@types/node" "^9.4.6"
Expand Down

0 comments on commit fba4de6

Please sign in to comment.