Comparing version 2.2.7 to 2.3.0
@@ -93,3 +93,3 @@ 'use strict'; | ||
* | ||
* Some platforms may not have either of these paramters. If that's the case, | ||
* Some platforms may not have either of these parameters. If that's the case, | ||
* the value assigned will be null or some other suitable value as the | ||
@@ -463,3 +463,2 @@ * equivalent to Messenger's seq in Telegram. | ||
__emitUpdate(update) { | ||
return this.__runIncomingMiddleware(update) | ||
@@ -491,2 +490,27 @@ | ||
/** | ||
* __createBotPatchedWithUpdate is used to create a new bot | ||
* instance that on sendMessage sends the update as an sendOption. | ||
* This is imprtant, because we want to have access to the update object | ||
* even within outgoing middleware. This allows us to always have access | ||
* to it. | ||
* | ||
* @param {object} update - update to be patched to sendMessage | ||
* @returns {object} bot | ||
*/ | ||
__createBotPatchedWithUpdate(update) { | ||
const newBot = Object.create(this); | ||
newBot.sendMessage = (message, sendOptions, cb) => { | ||
if (!sendOptions) { | ||
sendOptions = {}; | ||
} else if (typeof sendOptions === 'function') { | ||
cb = sendOptions; | ||
sendOptions = {}; | ||
} | ||
sendOptions.__update = update; | ||
return this.sendMessage(message, sendOptions, cb); | ||
}; | ||
return newBot; | ||
} | ||
/** | ||
* Add middleware to this bot | ||
@@ -501,3 +525,9 @@ * | ||
} else if (middlewareType === 'outgoing') { | ||
this.outgoingMiddleware.use(middlewareCallback); | ||
this.outgoingMiddleware.use((bot, update, message, next) => { | ||
if (middlewareCallback.length <= 3) { | ||
middlewareCallback(bot, message, next); | ||
} else { | ||
middlewareCallback(bot, update, message, next); | ||
} | ||
}); | ||
} | ||
@@ -509,3 +539,4 @@ // otherwise just don't do anything | ||
return new Promise((resolve, reject) => { | ||
this.incomingMiddleware.run(this, preMiddlewareUpdate, (err, bot, udpate) => { | ||
const patchedBot = this.__createBotPatchedWithUpdate(preMiddlewareUpdate); | ||
const cb = (err, bot, update) => { // the callback | ||
if (err) { | ||
@@ -515,4 +546,6 @@ err.message = `"${err.message}". In incoming middleware`; | ||
} | ||
return resolve(udpate); | ||
}); | ||
return resolve(update); | ||
}; | ||
this.incomingMiddleware.run(patchedBot, preMiddlewareUpdate, cb); | ||
}); | ||
@@ -522,2 +555,5 @@ } | ||
__runOutgoingMiddleware(preMiddlewareMessage, sendOptions) { | ||
if (!sendOptions) { | ||
sendOptions = {}; | ||
} | ||
return new Promise((resolve, reject) => { | ||
@@ -527,3 +563,4 @@ if (sendOptions && sendOptions.ignoreMiddleware) { | ||
} | ||
this.outgoingMiddleware.run(this, preMiddlewareMessage, (err, bot, message) => { | ||
const cb = (err, bot, update, message) => { // the callback | ||
if (err) { | ||
@@ -534,3 +571,6 @@ err.message = `"${err.message}". In outgoing middleware`; | ||
return resolve(message); | ||
}); | ||
}; | ||
this.outgoingMiddleware.run( | ||
this, sendOptions.__update, preMiddlewareMessage, cb); | ||
}); | ||
@@ -537,0 +577,0 @@ } |
@@ -28,2 +28,3 @@ 'use strict'; | ||
attachment: true, | ||
typing: true | ||
}; | ||
@@ -30,0 +31,0 @@ |
@@ -128,3 +128,3 @@ 'use strict'; | ||
this.bots.push(bot); | ||
bot.on('update', update => this.emit('update', bot, update)); | ||
bot.on('update', update => this.emit('update', bot.__createBotPatchedWithUpdate(update), update)); | ||
bot.on('warning', warning => this.emit('warning', bot, warning)); | ||
@@ -131,0 +131,0 @@ bot.on('error', err => this.emit('error', bot, err)); |
{ | ||
"name": "botmaster", | ||
"version": "2.2.7", | ||
"version": "2.3.0", | ||
"description": "Framework allowing developers to write bots that are agnostic with respect to the channel used by their users (messenger, telegram etc...)", | ||
@@ -56,3 +56,2 @@ "main": "./lib/index.js", | ||
"devDependencies": { | ||
"botmaster-fulfill": "^1.1.0", | ||
"chai": "^3.5.0", | ||
@@ -59,0 +58,0 @@ "coveralls": "^2.11.14", |
@@ -411,6 +411,81 @@ 'use strict'; | ||
afterEach(function(done) { | ||
describe('new syntax (bot, update, message, next) in outgoing', function () { | ||
specify('manually setting __update in sendOptions should pass it through to outgoing adopting the new syntax', function (done) { | ||
const mockUpdate = { id: 1 }; | ||
const messageToSend = { id: 2 }; | ||
botmaster.use('outgoing', function (bot, update, message, next) { | ||
assert(message === messageToSend); | ||
assert(update === mockUpdate); | ||
done(); | ||
}); | ||
const bot = botmaster.getBots('messenger')[0]; | ||
bot.sendMessage(messageToSend, { __update: mockUpdate }); | ||
}); | ||
specify('using __createBotPatchedWithUpdate should pass update with sendMessage through to outgoing adopting the new syntax', function (done) { | ||
const mockUpdate = { id: 2 }; | ||
const messageToSend = { id: 3 }; | ||
botmaster.use('outgoing', function (bot, update, message, next) { | ||
assert(message === messageToSend); | ||
assert(update === mockUpdate); | ||
done(); | ||
}); | ||
const bot = botmaster.getBots('messenger')[0].__createBotPatchedWithUpdate(mockUpdate); | ||
bot.sendMessage(messageToSend); | ||
}); | ||
specify('using __createBotPatchedWithUpdate with no options and a callback should pass update with sendMessage through to outgoing adopting the new syntax', function (done) { | ||
const mockUpdate = { id: 2 }; | ||
const messageToSend = { id: 3 }; | ||
botmaster.use('outgoing', function (bot, update, message, next) { | ||
console.log(message); | ||
console.log(update); | ||
expect(message).to.equal(messageToSend); | ||
expect(update).to.equal(mockUpdate); | ||
}); | ||
const bot = botmaster.getBots('messenger')[0].__createBotPatchedWithUpdate(mockUpdate); | ||
bot.sendMessage(messageToSend, (err, body) => { | ||
done(); | ||
}); | ||
}); | ||
specify('from a reply in incoming middleware the update should be sent through to outgoing adopting the new syntax', function (done) { | ||
botmaster.use('incoming', function (bot, update, next) { | ||
update.newProp = 1; | ||
bot.reply(update, 'right back at you!', function(err, body) { | ||
done(); | ||
}); | ||
}); | ||
botmaster.use('outgoing', function (bot, update, message, next) { | ||
assert(message.message.text === 'right back at you!', 'the message should be correct'); | ||
assert(update.newProp === 1, 'new prop should exist in update'); | ||
assert(update === incomingUpdateCopy, 'should still have the same reference to the update'); | ||
next(); | ||
}); | ||
const bot = botmaster.getBots('telegram')[0]; | ||
const incomingUpdateCopy = _.cloneDeep(incomingUpdate); | ||
bot.__emitUpdate(incomingUpdateCopy); | ||
}); | ||
specify('from a reply in an on update handler for botmaster the update should be sent through to outgoing adopting the new syntax', function (done) { | ||
botmaster.once('update', function (bot, update, next) { | ||
update.newProp = 1; | ||
bot.reply(update, 'right back at you!'); | ||
}); | ||
botmaster.use('outgoing', function (bot, update, message, next) { | ||
assert(message.message.text === 'right back at you!', 'the message should be correct'); | ||
assert(update.newProp === 1, 'new prop should exist in update'); | ||
assert(update === incomingUpdateCopy, 'should still have the same reference to the update'); | ||
done(); | ||
}); | ||
const bot = botmaster.getBots('messenger')[0]; | ||
const incomingUpdateCopy = _.cloneDeep(incomingUpdate); | ||
bot.__emitUpdate(incomingUpdateCopy); | ||
}); | ||
}); | ||
afterEach(function (done) { | ||
this.retries(4); | ||
process.nextTick(function() { | ||
botmaster.server.close(function() { done(); }); | ||
process.nextTick(function () { | ||
botmaster.server.close(function () { done(); }); | ||
}); | ||
@@ -417,0 +492,0 @@ }); |
177691
13
4795