Skip to content

Commit

Permalink
feat(message): Add RichMessage type
Browse files Browse the repository at this point in the history
- Handle payloads on incoming messages
- Update Rocket.Chat adapter and demo
  • Loading branch information
timkinnane committed Aug 12, 2018
1 parent cd974d0 commit b16aa8e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
23 changes: 7 additions & 16 deletions src/adapters/rocketchat.ts
@@ -1,12 +1,11 @@
import * as bot from '..'
import * as sdk from '@rocket.chat/sdk'

console.log(sdk.settings)

export class Rocketchat extends bot.MessageAdapter {
name = 'rocketchat-message-adapter'
constructor (bot: any) {
super(bot)
sdk.settings.integrationId = 'bBot'
this.bot.logger.info('Using Rocket.Chat as message adapter')
}

Expand Down Expand Up @@ -70,21 +69,13 @@ export class Rocketchat extends bot.MessageAdapter {
const robotIsNamed = message.msg.indexOf(this.bot.name) === startOfText || message.msg.indexOf(this.bot.alias) === startOfText
if ((isDM || isLC) && !robotIsNamed) message.msg = `${this.bot.name} ${message.msg}`

// Attachments, format properties for bBot
// Attachments, format properties as payload for bBot rich message type
if (Array.isArray(message.attachments) && message.attachments.length) {
let attachment = message.attachments[0]
if (attachment.image_url) {
attachment.link = `${sdk.settings.host}${attachment.image_url}`
attachment.type = 'image'
} else if (attachment.audio_url) {
attachment.link = `${sdk.settings.host}${attachment.audio_url}`
attachment.type = 'audio'
} else if (attachment.video_url) {
attachment.link = `${sdk.settings.host}${attachment.video_url}`
attachment.type = 'video'
}
this.bot.logger.debug('Message type AttachmentMessage')
return this.bot.receive(new AttachmentMessage(user, attachment, message.msg, message._id))
this.bot.logger.debug('Message type RichMessage')
return this.bot.receive(new bot.RichMessage(user, {
attachments: message.attachments,
text: message.text
}, message._id))
}

// Standard text messages, receive as is
Expand Down
11 changes: 6 additions & 5 deletions src/demo/listen-types.ts
@@ -1,12 +1,13 @@
import * as bot from '..'
/** @todo Disable file logging in demo */

/**
* Setting up basic listeners.
* Run demo using `ts-node src/demo/listen-types`
* Demo :: List Types
* Sets up text listeners with a variety of matching functions.
* Shows different matching methods to trigger the same bit.
* Run demo from project root: `ts-node src/demo/listen-types`
*/

import * as bot from '..'
/** @todo Disable file logging in demo */

// Sends flowers to the console...
bot.setupBit({
id: 'send-flowers',
Expand Down
11 changes: 9 additions & 2 deletions src/demo/rocketchat.ts
@@ -1,7 +1,14 @@
/**
* Demo :: Rocket.Chat
* Starts a bot with the Rocket.Chat adapter.
* Requires config: https://rocket.chat/docs/bots/configure-bot-environment/
* Shows different listener types and adapter respond methods.
* Run demo from project root: `ts-node src/demo/rocketchat`
*/
import * as bot from '..'

bot.config.messageAdapter = 'adapters/rocketchat'
bot.config.storageAdapter = 'adapters/mongo'
bot.config.messageAdapter = '../adapters/rocketchat'
bot.config.storageAdapter = '../adapters/mongo'

const start = async () => {
await bot.start()
Expand Down
24 changes: 21 additions & 3 deletions src/lib/message.ts
Expand Up @@ -12,12 +12,12 @@ export abstract class Message {
this.user = (user instanceof bot.User) ? user : new bot.User(user)
}

/** String representation of the message */
/** String representation of the message. */
abstract toString (): string
}

/**
* NLU attributes interface
* NLU attributes interface.
* @param intent A key characterising what the message was about
* @param entities Additional data inferred from the message or context
* @param sentiment Tone or emotional data provided from NLU parsing of text
Expand Down Expand Up @@ -49,6 +49,24 @@ export class TextMessage extends Message {
}
}

/** A message containing payload attributes from messaging platform. */
export class RichMessage extends Message {

/**
* Create a rich message.
* @param user The user who sent the message
* @param payload The payload to attach
* @param id A unique ID for the message
*/
constructor (user: bot.User, public payload: any, id?: string) {
super(user, id)
}

toString () {
return JSON.stringify(this.payload)
}
}

/** Represent an incoming event notification. */
export abstract class EventMessage extends Message {
abstract event: string
Expand Down Expand Up @@ -83,7 +101,7 @@ export class CatchAllMessage extends Message {
}
}

/** Envelope interface, to create from scratch */
/** Envelope interface, to create from scratch. */
export interface IEnvelope {
user?: bot.User
room?: {
Expand Down

0 comments on commit b16aa8e

Please sign in to comment.