src/telegram: reset state of global regexp (#326)

References:

  * BR: https://github.com/yagop/node-telegram-bot-api/issues/325
  * BR-by: @Sirius-A
  * PR: https://github.com/yagop/node-telegram-bot-api/pull/326
  * PR-by: @Sirius-A
experimental
Fabio Zuber 9 years ago committed by Gocho Mugo
parent 3d4d10f9b3
commit 9f3107b5ab
  1. 2
      src/telegram.js
  2. 10
      test/telegram.js

@ -473,6 +473,8 @@ class TelegramBot extends EventEmitter {
if (!result) {
return false;
}
// reset index so we start at the beginning of the regex each time
reg.regexp.lastIndex = 0;
debug('Matches %s', reg.regexp);
reg.callback(message, result);
// returning truthy value exits .some

@ -975,6 +975,16 @@ describe('TelegramBot', function telegramSuite() {
message: { text: '/onText ECHO ALOHA' },
});
});
it('should reset the global regex state with each message', function test(done) {
const regexp = /\/onText (.+)/g;
botWebHook.onText(regexp, () => {
assert.equal(regexp.lastIndex, 0);
return done();
});
utils.sendWebHookMessage(webHookPort2, TOKEN, {
message: { text: '/onText ECHO ALOHA' },
});
});
});
describe.skip('#onReplyToMessage', function onReplyToMessageSuite() {});

Loading…
Cancel
Save