diff --git a/services/bot/src/handler.ts b/services/bot/src/handler.ts index 4dd2d2a..2c1eedc 100644 --- a/services/bot/src/handler.ts +++ b/services/bot/src/handler.ts @@ -39,7 +39,7 @@ export class Handler { public async handle (request: Request): Promise { const me = await BOT.people.get('me'); // Don't do anything if the bot is receiving a hook from its own message. - if (me.id !== request.body.data.personId) { + if (request.body.data.personId && me.id !== request.body.data.personId) { // This is the earliest we can put this try catch, any earlier // and an error will cause a loop which continuously sends messages // since the bot will respond to its own messages. @@ -99,6 +99,9 @@ export class Handler { LOGGER.error(innerError); } } + } else if (!request.body.data.personId) { + // Sometimes this happens in production where a personId is not coming in from the webex server + console.error(request.body.data); } } diff --git a/services/bot/tests/handler.test.ts b/services/bot/tests/handler.test.ts index bf16b0f..26a3273 100644 --- a/services/bot/tests/handler.test.ts +++ b/services/bot/tests/handler.test.ts @@ -60,6 +60,14 @@ describe('Handler is working', () => { expect(BOT.messages.create).toHaveBeenCalledTimes(0); }); + test('handler appropriately errors and logs when message originator can\'t be determined', async () => { + BOT.people.get.mockReturnValueOnce({ id: MOCK_REQUEST.body.data.personId }); + expect(await new Handler().handle({ body: { ...MOCK_REQUEST.body, data: { ...MOCK_REQUEST.body.data, personId: undefined } } } as Request)).toEqual(undefined); + expect(BOT.people.get).toHaveBeenCalledWith('me'); + expect(BOT.messages.get).toHaveBeenCalledTimes(0); + expect(BOT.messages.create).toHaveBeenCalledTimes(0); + }); + test('handler appropriately returns response if command is invalid', async () => { BOT.messages.get.mockReturnValueOnce({ text: 'invalid foo command', personId: 'mockPersonId' }); BOT.people.get.mockReturnValue({ id: MOCK_REQUEST.body.data.personId, displayName: 'mockDisplayName' });