Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added character limit for ES and CX bot. TDH-1685 #1281

Merged
merged 2 commits into from
Jan 15, 2025
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
2 changes: 1 addition & 1 deletion webplugin/js/app/labels/default-labels.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class KMLabel {
'search.contacts': 'Contacts',
'search.groups': 'Groups',
'char.limit.warn':
'Keep your message within 256 characters to help the bot understand easily',
'Keep your message within LIMIT characters to help the bot understand easily',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Implementation doesn't fully address the PR objectives.

While replacing "256" with "LIMIT" makes the message template dynamic, the implementation is incomplete as it doesn't include:

  1. Logic to differentiate between ES (256) and CX (500) bots
  2. Mechanism to inject the actual character limit value

Consider implementing a configuration mechanism to:

  1. Detect the bot type (ES/CX)
  2. Set the appropriate character limit
  3. Inject the limit value into the message template

Example approach:

// In a configuration file
const BOT_LIMITS = {
    ES: 256,
    CX: 500
};

// In the labels class
'char.limit.warn': (botType) => 
    `Keep your message within ${BOT_LIMITS[botType]} characters to help the bot understand easily`

🛠️ Refactor suggestion

Consider a more maintainable implementation approach.

The current string-based approach with a "LIMIT" placeholder is prone to errors and difficult to maintain. Consider:

  1. Using a template function instead of string interpolation
  2. Adding type safety for the limit value
  3. Including validation for the injected value

Example implementation:

'char.limit.warn': {
    template: (limit) => {
        if (typeof limit !== 'number' || limit <= 0) {
            throw new Error('Invalid character limit');
        }
        return `Keep your message within ${limit} characters to help the bot understand easily`;
    },
    defaultLimit: 256 // Fallback value
}

'limit.remove': 'Remove',
'limit.characters': 'characters',
'limit.remaining': 'remaining',
Expand Down
11 changes: 7 additions & 4 deletions webplugin/js/app/mck-sidebox-1.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ var WAITING_QUEUE = [];
var AVAILABLE_VOICES_FOR_TTS = new Array();
var KM_ATTACHMENT_V2_SUPPORTED_MIME_TYPES = ['application', 'text', 'image'];
const DEFAULT_TEAM_NAME = ['Default Team', 'Default'];
const CHARACTER_LIMIT = {"ES": 256 , "CX": 500};
const WARNING_LENGTH = {"ES": 199 , "CX": 450};
var userOverride = {
voiceOutput: true,
};
Expand Down Expand Up @@ -4610,11 +4612,11 @@ const firstVisibleMsg = {
return;
}
if (CURRENT_GROUP_DATA.CHAR_CHECK) {
var warningLength = 199;
var maxLength = 256;
var warningLength = CURRENT_GROUP_DATA.isDialogflowCXBot ? WARNING_LENGTH["CX"]: WARNING_LENGTH["ES"];
var maxLength = CURRENT_GROUP_DATA.isDialogflowCXBot ? CHARACTER_LIMIT["CX"] : CHARACTER_LIMIT["ES"];
var textBox = $mck_text_box[0]; //using separate selector for vanilla JS functions
if (!document.getElementById('mck-char-count')) {
warningText.innerHTML +=
warningText.innerHTML = MCK_LABELS["char.limit.warn"].replace("LIMIT",maxLength) +
'<span> | </span><span id="mck-char-count"></span>';
}
var remtxt;
Expand Down Expand Up @@ -14464,6 +14466,7 @@ const firstVisibleMsg = {
CURRENT_GROUP_DATA.isConversationAssigneeBot = true;
CURRENT_GROUP_DATA.answerFeedback =
res?.answerFeedback || false;
CURRENT_GROUP_DATA.isDialogflowCXBot = res?.dialogflowCXBot || false;
},
error: function () {
CURRENT_GROUP_DATA.CHAR_CHECK = false;
Expand Down Expand Up @@ -14492,7 +14495,7 @@ const firstVisibleMsg = {
_this.disableSendButton = function (value) {
var textBox = document.getElementById('mck-text-box');
var str = mckUtils.textVal(textBox);
var maxLength = 256;
var maxLength = CURRENT_GROUP_DATA.isDialogflowCXBot ? CHARACTER_LIMIT['CX'] : CHARACTER_LIMIT['ES'];
var sendButton = document.getElementById('mck-msg-sbmt');
var trimmedStr = str.trim();
var textLength = trimmedStr.length;
Expand Down
Loading