index.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { customerServicePayloadType, IMessageModel } from '../interface';
  2. import { CUSTOM_MESSAGE_SRC, TYPES } from '../constant';
  3. // Determine if it is a JSON string
  4. export function isJSON(str: string): boolean {
  5. // eslint-disable-next-line no-useless-escape
  6. if (typeof str === 'string') {
  7. try {
  8. const data = JSON.parse(str);
  9. if (data) {
  10. return true;
  11. }
  12. return false;
  13. } catch (error: any) {
  14. return false;
  15. }
  16. }
  17. return false;
  18. }
  19. // Determine if it is a JSON string
  20. export function JSONToObject(str: string) {
  21. if (!isJSON(str)) {
  22. return str;
  23. }
  24. return JSON.parse(str);
  25. }
  26. export function isCustomerServiceMessage(message: IMessageModel): boolean {
  27. const customerServicePayload: customerServicePayloadType = JSONToObject(message?.payload?.data);
  28. return Number(customerServicePayload?.customerServicePlugin) === 0 || Number(customerServicePayload?.chatbotPlugin) === 1;
  29. }
  30. export const isMessageRating = (message: IMessageModel): boolean => {
  31. const customerServicePayload: customerServicePayloadType = JSONToObject(message?.payload?.data);
  32. return isCustomerServiceMessage(message) && customerServicePayload.src === CUSTOM_MESSAGE_SRC.MENU;
  33. };
  34. export const isMessageInvisible = (message: IMessageModel): boolean => {
  35. const customerServicePayload: customerServicePayloadType = JSONToObject(message?.payload?.data);
  36. const robotCommandArray = ['feedback', 'updateBotStatus'];
  37. const whiteList = [
  38. CUSTOM_MESSAGE_SRC.MENU,
  39. CUSTOM_MESSAGE_SRC.BRANCH,
  40. CUSTOM_MESSAGE_SRC.BRANCH_NUMBER,
  41. CUSTOM_MESSAGE_SRC.FROM_INPUT,
  42. CUSTOM_MESSAGE_SRC.PRODUCT_CARD,
  43. ];
  44. const isCustomerMessage = message?.type === TYPES.MSG_CUSTOM;
  45. const isExistWhiteList = whiteList.includes(customerServicePayload?.src);
  46. const isRobot = customerServicePayload?.src === CUSTOM_MESSAGE_SRC.ROBOT && robotCommandArray.indexOf(customerServicePayload?.content?.command) !== -1;
  47. return isCustomerMessage && (!isExistWhiteList || isRobot);
  48. };