Skip to content
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
5 changes: 4 additions & 1 deletion services/bot/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class Handler {
public async handle (request: Request): Promise<void> {
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.
Expand Down Expand Up @@ -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);
}

}
Expand Down
8 changes: 8 additions & 0 deletions services/bot/tests/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down