Skip to content

Commit

Permalink
Merge pull request #8 from WickrInc/add-file-handler
Browse files Browse the repository at this point in the history
feat: add support for files
  • Loading branch information
dwickr committed Oct 25, 2023
2 parents 5b90e73 + e3b6e6b commit 510e7f9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
49 changes: 39 additions & 10 deletions lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class WickrBot extends EventEmitter {
'help': {fn: this.sendHelp.bind(this) },
}
this.defaultHandler = undefined
this.fileHandler = undefined

this.username = username || this._getUsername()
this.on('start', () => this.wickr.cmdStartAsyncRecvMessages(this.handleMessage()))
Expand Down Expand Up @@ -98,22 +99,26 @@ class WickrBot extends EventEmitter {
handleMessage() {
// return as an arrow function to maintain `this` binding
return (message) => {
//console.log("Message received:", message);
let data = JSON.parse(message)
if (data.msgtype !== MESSAGE_TYPE.TEXT) {
return
}
let handler, handlerName, args

if (data.msgtype === MESSAGE_TYPE.FILE) {
handler = this.fileHandler
handlerName = 'file'
} else if (data.msgtype === MESSAGE_TYPE.TEXT) {
const msg = this._parseMessage(data.message)

let msg = this._parseMessage(data.message)
args = msg.args
handler = this.handlers[msg.command]?.fn || this.defaultHandler
handlerName = msg.command || 'default'
}

if (msg.command in this.handlers) {
if (handler) {
try {
this.handlers[msg.command].fn(data, msg.args)
handler(data, args)
} catch (error) {
console.error(`Error executing '${msg.command}' handler:`, error)
console.warn(`Error executing ${handlerName} handler:`, error)
}
} else if (typeof msg.command === 'undefined' && this.defaultHandler) {
this.defaultHandler(data, msg.args)
}
}
}
Expand Down Expand Up @@ -296,10 +301,34 @@ class WickrBot extends EventEmitter {
}
}

/**
* setDefaultHandler defines the function which will be called when a text message is received
* which does not match any slash command
*
* @param {function (msg, args)} fn The function to call
*/
setDefaultHandler(fn) {
this.defaultHandler = fn
}

/**
* setFileHandler defines the function which will be called when a file is received
*
* See https://wickrinc.github.io/wickrio-docs/#definitions-wickr-message-formats-file-transfer-messages
* for the complete format of a file transfer message
*
* @param {function (msg)} fn The function to call when a file is received
*/
setFileHandler(fn) {
this.fileHandler = fn
}

/**
* setAvatar sets the user icon for the bot in Wickr
*
* @param {string} imgPath path to an image file containing the avatar
* @returns {boolean} `true` when the operation succeeded, `false` otherwise
*/
setAvatar(imgPath) {
if (!fs.existsSync(imgPath)) throw new Error(`Unable to set avatar. File ${imgPath} not found.`)

Expand Down
15 changes: 13 additions & 2 deletions test/test-wickr-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const sinon = require('sinon')
const FakeWickr = require('./fakes/wickr')
const WickrBot = require('../lib/bot')

describe('wickr-bot', function() {
describe('WickrBot', function() {
beforeEach(function() {
this.wickr = new FakeWickr()
})
Expand Down Expand Up @@ -33,7 +33,7 @@ describe('wickr-bot', function() {
expect(spyFn.calledWith(JSON.parse(fakeMsg), ['bar', 'baz'])).to.be.true
})

it('creates a default listener', function() {
it('sets a default listener', function() {
let fakeMsg = '{"msgtype": 1000, "message": "hey what\'s up?"}'
let spyFn = sinon.spy()
let bot = new WickrBot(this.wickr, 'foo')
Expand All @@ -44,6 +44,17 @@ describe('wickr-bot', function() {
expect(spyFn.calledWith(JSON.parse(fakeMsg), ['hey', 'what\'s', 'up?'])).to.be.true
})

it('sets a file listener', function() {
let fakeMsg = '{"msgtype": 6000}'
let spyFn = sinon.spy()
let bot = new WickrBot(this.wickr, 'foo')

bot.setFileHandler(spyFn)
bot.handleMessage()(fakeMsg)

expect(spyFn.calledWith(JSON.parse(fakeMsg))).to.be.true
})

describe('#handleMessage', function() {
it('catches errors thrown in handlers', function() {
let fakeMsg = '{"msgtype": 1000, "message": "/foo@fake-bot"}'
Expand Down

0 comments on commit 510e7f9

Please sign in to comment.