Skip to content

Commit a72fba7

Browse files
authored
Merge pull request #496 from Kommunicate-io/CM-1628
[CM-1628] 256 char limit for CX and ES not working allowing for 2000 char | iOS SDK
2 parents e48ebdc + 2a63536 commit a72fba7

File tree

6 files changed

+64
-11
lines changed

6 files changed

+64
-11
lines changed

Sources/Kommunicate/Classes/BotDetailResponse.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct BotDetailResponse: Decodable, BotDetailResponseProtocol {
2525

2626
public struct BotDetail: Decodable {
2727
let aiPlatform: String?
28+
let dialogflowCXBot: Bool?
2829
}
2930

3031
extension BotDetailResponse {
@@ -35,6 +36,8 @@ extension BotDetailResponse {
3536
case RASA = "rasa"
3637
case SMARTREPLY = "smartreply"
3738
case CUSTOM = "custom"
39+
case DIALOGFLOWCX = "dialogflowcx"
40+
case DIALOGFLOWES = "dialogflowes"
3841
}
3942

4043
init(data: Data) throws {

Sources/Kommunicate/Classes/Controllers/KMConversationController+MessageCharacterLimit.swift

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,30 @@ extension KMConversationViewController: MessageCharacterLimitDelegate {
1818
}
1919

2020
func characterLimit(manager _: MessageCharacterLimitManager, reachedTheLimit _: Int, textCount: Int) {
21-
let messageLimit = CharacterLimit.charlimit
22-
let botLimit = CharacterLimit.botCharLimit
23-
if isConversationAssignedToDialogflowBot, textCount >= botLimit.soft, textCount <= messageLimit.soft {
24-
botCharLimitManager.messageToShow = characterLimitMessage(textCount: textCount, limit: botLimit, isMessageforBot: true)
25-
botCharLimitManager.showLimitView(true, disableButton: textCount > botLimit.hard)
26-
} else if textCount >= messageLimit.soft {
27-
messageCharLimitManager.messageToShow = characterLimitMessage(textCount: textCount, limit: messageLimit, isMessageforBot: false)
28-
messageCharLimitManager.showLimitView(true, disableButton: textCount > messageLimit.hard)
21+
let limitConfig = determineCharacterLimit()
22+
handleCharacterLimit(textCount: textCount, limit: limitConfig.limit, manager: limitConfig.manager, isBotMessage: limitConfig.isBotMessage)
23+
}
24+
25+
private func determineCharacterLimit() -> (limit: CharacterLimit.Limit, manager: MessageCharacterLimitManager, isBotMessage: Bool) {
26+
switch (isConversationAssignedToDialogflowBot, isConversationAssignedToDialogflowCXBot) {
27+
case (true, true):
28+
return (CharacterLimit.cxBotCharLimit, botCharLimitManager, true) /// Conversation is assigned to Dialogflow CX bot
29+
case (true, false):
30+
return (CharacterLimit.botCharLimit, botCharLimitManager, true) /// Conversation is assigned to Dialogflow ES bot
31+
default:
32+
return (CharacterLimit.charlimit, messageCharLimitManager, false) /// Conversation is assigned to Non - Dialogflow bot
33+
}
34+
}
35+
36+
private func handleCharacterLimit(textCount: Int, limit: CharacterLimit.Limit, manager: MessageCharacterLimitManager, isBotMessage: Bool) {
37+
let hasReachedSoftLimit = textCount >= limit.soft
38+
let hasExceededHardLimit = textCount > limit.hard
39+
40+
if hasReachedSoftLimit {
41+
manager.messageToShow = characterLimitMessage(textCount: textCount, limit: limit, isMessageforBot: isBotMessage)
42+
manager.showLimitView(true, disableButton: hasExceededHardLimit)
2943
} else {
30-
messageCharLimitManager.showLimitView(false)
44+
manager.showLimitView(false)
3145
}
3246
}
3347
}

Sources/Kommunicate/Classes/KMAppUserDefaultHandler.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ class KMAppUserDefaultHandler: NSObject {
6969
func getBotType(botId: String) -> String? {
7070
return userDefaultSuite.value(forKey: botId) as? String
7171
}
72+
73+
func setDialogFlowBotType(_ botType: String, botId: String) {
74+
guard !botType.isEmpty, !botId.isEmpty else { return }
75+
let key = "dialogflow_\(botId)"
76+
userDefaultSuite.setValue(botType, forKey: key)
77+
}
78+
79+
func getDialogFlowBotType(botId: String) -> String? {
80+
guard !botId.isEmpty else { return nil }
81+
let key = "dialogflow_\(botId)"
82+
return userDefaultSuite.value(forKey: key) as? String
83+
}
7284

7385
func clear() {
7486
userDefaultSuite.removePersistentDomain(forName: KMAppUserDefaultHandler.defaultSuiteName)

Sources/Kommunicate/Classes/KMBotService.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ public struct KMBotService {
5050
print("Bot detail fetched successfully for botId:\(botId) And Bot platform:", botDetail.aiPlatform as Any)
5151
/// Add the botType and botId in user defaults
5252
DispatchQueue.main.async {
53+
if botType == BotDetailResponse.BotType.DIALOGFLOW.rawValue {
54+
if let isCXBot = botDetail.dialogflowCXBot, isCXBot {
55+
KMAppUserDefaultHandler.shared.setDialogFlowBotType(BotDetailResponse.BotType.DIALOGFLOWCX.rawValue, botId: botId)
56+
} else {
57+
KMAppUserDefaultHandler.shared.setDialogFlowBotType(BotDetailResponse.BotType.DIALOGFLOWES.rawValue, botId: botId)
58+
}
59+
}
5360
KMAppUserDefaultHandler.shared.setBotType(botType, botId: botId)
5461
completion(.success(botDetail))
5562
}
@@ -123,4 +130,14 @@ public struct KMBotService {
123130
}
124131
}
125132
}
133+
134+
func isCXDialogFlowBot(type: String, groupId: NSNumber) -> Bool? {
135+
guard let assigneeId = assigneeUserIdFor(groupId: groupId),
136+
let channelUserX = channelDBService.loadChannelUserX(byUserId: groupId, andUserId: assigneeId),
137+
channelUserX.role == 2
138+
else {
139+
return nil
140+
}
141+
return KMAppUserDefaultHandler.shared.getDialogFlowBotType(botId: assigneeId) == type
142+
}
126143
}

Sources/Kommunicate/Classes/KMConversationViewController.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ open class KMConversationViewController: ALKConversationViewController, KMUpdate
8181
}
8282
}
8383
}
84+
var isConversationAssignedToDialogflowCXBot = false
8485

8586
let awayMessageheight = 80.0
8687

@@ -1123,7 +1124,12 @@ extension KMConversationViewController {
11231124

11241125
func conversationAssignedToDialogflowBot() {
11251126
guard let channelKey = viewModel.channelKey else { return }
1126-
kmBotService.conversationAssignedToBotForBotType(type: BotDetailResponse.BotType.DIALOGFLOW.rawValue, groupId: channelKey) { [weak self] isDialogflowBot in
1127+
kmBotService.conversationAssignedToBotForBotType(type: BotDetailResponse.BotType.DIALOGFLOW.rawValue, groupId: channelKey) {
1128+
[weak self] isDialogflowBot in
1129+
if isDialogflowBot,
1130+
let isCXBot = self?.kmBotService.isCXDialogFlowBot(type: BotDetailResponse.BotType.DIALOGFLOWCX.rawValue, groupId: channelKey) {
1131+
self?.isConversationAssignedToDialogflowCXBot = isCXBot
1132+
}
11271133

11281134
self?.isConversationAssignedToDialogflowBot = isDialogflowBot
11291135
guard let weakSelf = self,

Sources/Kommunicate/Classes/Utilities/CharacterLimit.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public enum CharacterLimit: Localizable {
1919
}
2020

2121
public static var charlimit = Limit(soft: 1800, hard: 2000)
22-
public static var botCharLimit = Limit(soft: 55, hard: 256)
22+
public static var botCharLimit = Limit(soft: 200, hard: 256)
23+
public static var cxBotCharLimit = Limit(soft: 450, hard: 500)
2324

2425
enum LocalizedText {
2526
private static let filename = Kommunicate.defaultConfiguration.localizedStringFileName

0 commit comments

Comments
 (0)