Skip to content

Commit

Permalink
Guess what? Logging
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenhand committed Sep 27, 2024
1 parent ce859b3 commit 87abffc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
11 changes: 8 additions & 3 deletions functions/helpers/sendErrorMessageForUnsupportedMedia.private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export type ConversationSid = `CH${string}`;
type SendErrorMessageForUnsupportedMediaEvent = {
Body?: string;
ConversationSid: ConversationSid;
EventType?: string;
Media?: Record<string, any>;
DateCreated: Date;
};
Expand Down Expand Up @@ -56,28 +55,33 @@ export const sendConversationMessage = async (
});

export const sendErrorMessageForUnsupportedMedia = async (context: Context, event: Event) => {
const { EventType, Body, Media, ConversationSid } = event;
const { Body, Media, ConversationSid } = event;

/* Valid message will have either a body/media. A message with no
body or media implies that there was an error sending such message
*/
if (EventType === 'onMessageAdded' && !Body && !Media) {
if (!Body && !Media) {
console.debug('Message has no text body or media, sending error.', ConversationSid);
let messageText = FALLBACK_ERROR_MESSAGE;

const serviceConfig = await context.getTwilioClient().flexApi.configuration.get().fetch();
const helplineLanguage = serviceConfig.attributes.helplineLanguage ?? 'en-US';

console.debug('Helpline language to send error message: ', helplineLanguage, ConversationSid);
if (helplineLanguage) {
try {
const response = await fetch(
`https://${context.DOMAIN_NAME}/translations/${helplineLanguage}/messages.json`,
);
const translation = await response.json();
const { [ERROR_MESSAGE_TRANSLATION_KEY]: translatedMessage } = translation;

console.debug('Translated error message: ', translatedMessage, ConversationSid);
messageText = translatedMessage || messageText;
} catch {
console.warn(
`Couldn't retrieve ${ERROR_MESSAGE_TRANSLATION_KEY} message translation for ${helplineLanguage}`,
ConversationSid,
);
}
}
Expand All @@ -87,6 +91,7 @@ export const sendErrorMessageForUnsupportedMedia = async (context: Context, even
author: 'Bot',
messageText,
});
console.info('Sent error message: ', messageText, ConversationSid);
}
};

Expand Down
22 changes: 13 additions & 9 deletions functions/webhooks/serviceConversationListener.protected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@ import {
} from '../helpers/sendErrorMessageForUnsupportedMedia.private';

export const handler = async (context: Context, event: Event, callback: ServerlessCallback) => {
console.info(`===== Service Conversation Listener (event: ${event.EventType})=====`);
const response = responseWithCors();
const resolve = bindResolve(callback)(response);
// eslint-disable-next-line global-require,import/no-dynamic-require
const sendErrorMessageForUnsupportedMedia = require(Runtime.getFunctions()[
'helpers/sendErrorMessageForUnsupportedMedia'
].path).sendErrorMessageForUnsupportedMedia as SendErrorMessageForUnsupportedMedia;
if (event.EventType === 'onMessageAdded') {
// eslint-disable-next-line global-require,import/no-dynamic-require
const sendErrorMessageForUnsupportedMedia = require(Runtime.getFunctions()[
'helpers/sendErrorMessageForUnsupportedMedia'
].path).sendErrorMessageForUnsupportedMedia as SendErrorMessageForUnsupportedMedia;

try {
await sendErrorMessageForUnsupportedMedia(context, event);
} catch (err) {
if (err instanceof Error) resolve(error500(err));
else resolve(error500(new Error(String(err))));
try {
console.debug('New message, checking if we need to send error.');
await sendErrorMessageForUnsupportedMedia(context, event);
} catch (err) {
if (err instanceof Error) resolve(error500(err));
else resolve(error500(new Error(String(err))));
}
}
};

0 comments on commit 87abffc

Please sign in to comment.