Skip to content

Commit 51a697c

Browse files
authored
Merge pull request #1982 from CreditMutuelArkea/dercbot-1777
[DERCBOT-1777] Make thread usage configurable and unify message sending
2 parents ced5978 + 5b465ec commit 51a697c

File tree

5 files changed

+174
-1589
lines changed

5 files changed

+174
-1589
lines changed

bot/admin/server/src/main/kotlin/BotAdminVerticle.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,11 +540,9 @@ open class BotAdminVerticle : AdminVerticle() {
540540
connectorProvider
541541
.configuration()
542542
.fields
543-
.filter { it.mandatory && !bot.parameters.containsKey(it.key) }
544-
.map {
543+
.filter { it.mandatory && !bot.parameters.containsKey(it.key) }.associate {
545544
it.key to "Please fill a value"
546545
}
547-
.toMap()
548546
conf.copy(parameters = conf.parameters + additionalProperties)
549547
} else {
550548
conf

bot/connector-google-chat/src/main/kotlin/GoogleChatConnector.kt

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import com.github.salomonbrys.kodein.instance
3636
import com.google.api.client.json.jackson2.JacksonFactory
3737
import com.google.api.services.chat.v1.HangoutsChat
3838
import com.google.api.services.chat.v1.model.DeprecatedEvent
39-
import com.google.api.services.chat.v1.model.Thread
4039
import mu.KotlinLogging
4140
import java.time.Duration
4241

@@ -47,6 +46,7 @@ class GoogleChatConnector(
4746
private val authorisationHandler: GoogleChatAuthorisationHandler,
4847
private val useCondensedFootnotes: Boolean,
4948
private val introMessage: String? = null,
49+
private val useThread: Boolean = false,
5050
) : ConnectorBase(GoogleChatConnectorProvider.connectorType) {
5151
private val logger = KotlinLogging.logger {}
5252
private val executor: Executor by injector.instance()
@@ -79,6 +79,7 @@ class GoogleChatConnector(
7979
threadName,
8080
chatService,
8181
introMessage,
82+
useThread,
8283
),
8384
),
8485
)
@@ -99,37 +100,18 @@ class GoogleChatConnector(
99100
delayInMs: Long,
100101
) {
101102
logger.debug { "event: $event" }
102-
if (event is Action) {
103-
val message = GoogleChatMessageConverter.toMessageOut(event, useCondensedFootnotes)
104-
if (message != null) {
105-
callback as GoogleChatConnectorCallback
106-
executor.executeBlocking(Duration.ofMillis(delayInMs)) {
107-
try {
108-
logger.info {
109-
"Sending to Google Chat: space=${callback.spaceName}, thread=${callback.threadName}"
110-
}
111-
logger.debug {
112-
"Message content: ${message.toGoogleMessage()}"
113-
}
103+
if (event !is Action) return
114104

115-
val response =
116-
chatService
117-
.spaces()
118-
.messages()
119-
.create(
120-
callback.spaceName,
121-
message.toGoogleMessage().setThread(Thread().setName(callback.threadName)),
122-
).setMessageReplyOption("REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD")
123-
.execute()
124-
125-
logger.info { "Google Chat API response: ${response?.name}" }
126-
} catch (e: Exception) {
127-
logger.error(e) {
128-
"Failed to send message to Google Chat (space=${callback.spaceName}, thread=${callback.threadName})"
129-
}
130-
}
131-
}
132-
}
105+
val message =
106+
GoogleChatMessageConverter.toMessageOut(event, useCondensedFootnotes)
107+
?: return
108+
109+
callback as GoogleChatConnectorCallback
110+
111+
executor.executeBlocking(Duration.ofMillis(delayInMs)) {
112+
callback.sendGoogleMessage(
113+
message,
114+
)
133115
}
134116
}
135117

bot/connector-google-chat/src/main/kotlin/GoogleChatConnectorCallback.kt

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ data class GoogleChatConnectorCallback(
2929
val threadName: String,
3030
private val chatService: HangoutsChat,
3131
private val introMessage: String?,
32+
val useThread: Boolean,
3233
) : ConnectorCallbackBase(applicationId, googleChatConnectorType) {
3334
private val logger = KotlinLogging.logger {}
3435

@@ -54,29 +55,50 @@ data class GoogleChatConnectorCallback(
5455
* Sends the intro message to the Google Chat space/thread.
5556
*/
5657
private fun sendIntroMessage() {
57-
if (introMessage == null) return
58+
val message = introMessage ?: return
59+
logger.info {
60+
"Sending Google Chat intro message: space=$spaceName" +
61+
if (useThread) ", thread=$threadName" else ""
62+
}
63+
sendGoogleMessage(
64+
GoogleChatConnectorTextMessageOut(message),
65+
)
66+
}
5867

68+
/**
69+
* Sends message to the Google Chat space/thread.
70+
*/
71+
fun sendGoogleMessage(message: GoogleChatConnectorMessage) {
5972
try {
73+
val googleMessage = message.toGoogleMessage()
74+
logger.debug { "Google Message content: $googleMessage" }
75+
76+
if (useThread) {
77+
googleMessage.thread = Thread().setName(threadName)
78+
}
79+
6080
logger.info {
61-
"Sending intro message to Google Chat: space=$spaceName, thread=$threadName"
81+
"Sending Google Chat message: space=$spaceName" +
82+
if (useThread) ", thread=$threadName" else ""
6283
}
6384

64-
val response =
85+
val request =
6586
chatService
6687
.spaces()
6788
.messages()
68-
.create(
69-
spaceName,
70-
GoogleChatConnectorTextMessageOut(introMessage)
71-
.toGoogleMessage()
72-
.setThread(Thread().setName(threadName)),
73-
).setMessageReplyOption("REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD")
74-
.execute()
89+
.create(spaceName, googleMessage)
90+
91+
if (useThread) {
92+
request.messageReplyOption = "REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD"
93+
}
7594

76-
logger.info { "Google Chat API intro response: ${response?.name}" }
95+
val response = request.execute()
96+
logger.info { "Google Chat API response: ${response?.name}" }
7797
} catch (e: Exception) {
7898
logger.error(e) {
79-
"Failed to send intro message to Google Chat (space=$spaceName, thread=$threadName)"
99+
"Failed to send Google Chat message: " +
100+
"space=$spaceName" +
101+
if (useThread) ", thread=$threadName" else ""
80102
}
81103
}
82104
}

bot/connector-google-chat/src/main/kotlin/GoogleChatConnectorProvider.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ private const val BOT_PROJECT_NUMBER_PARAMETER = "botProjectNumber"
4545
private const val CONDENSED_FOOTNOTES_PARAMETER = "useCondensedFootnotes"
4646
private const val GSA_TO_IMPERSONATE_PARAMETER = "gsaToImpersonate"
4747
private const val INTRO_MESSAGE_PARAMETER = "introMessage"
48+
private const val USE_THREAD_PARAMETER = "useThread"
4849

4950
// Lifetime (in seconds) of each impersonated access token.
5051
// This is the TTL of a single token, not a hard limit on the connector:
@@ -101,13 +102,17 @@ internal object GoogleChatConnectorProvider : ConnectorProvider {
101102
val introMessage =
102103
connectorConfiguration.parameters[INTRO_MESSAGE_PARAMETER]?.takeIf { it.isNotBlank() }
103104

105+
val useThread =
106+
connectorConfiguration.parameters[USE_THREAD_PARAMETER] == "1"
107+
104108
return GoogleChatConnector(
105109
connectorId,
106110
path,
107111
chatService,
108112
authorisationHandler,
109113
useCondensedFootnotes,
110114
introMessage,
115+
useThread,
111116
)
112117
}
113118
}
@@ -199,6 +204,11 @@ internal object GoogleChatConnectorProvider : ConnectorProvider {
199204
INTRO_MESSAGE_PARAMETER,
200205
false,
201206
),
207+
ConnectorTypeConfigurationField(
208+
"Use thread (true = 1, false = 0)",
209+
USE_THREAD_PARAMETER,
210+
false,
211+
),
202212
),
203213
svgIcon = resourceAsString("/google_chat.svg"),
204214
)

0 commit comments

Comments
 (0)