Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for files #8

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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