Skip to content

Commit

Permalink
PR fixes: naming, javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
Denire committed Mar 4, 2021
1 parent cfa0512 commit 30aac0e
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class FacebookChannel private constructor(
internal const val REQUEST_TEMPLATE_PATH = "/FacebookRequestTemplate.json"
}

override fun processExternalInvocation(request: InvocationRequest, requestContext: RequestContext) {
override fun processInvocation(request: InvocationRequest, requestContext: RequestContext) {
val template = getRequestTemplateFromResources(request, REQUEST_TEMPLATE_PATH)
messenger.onReceiveEvents(template, Optional.empty()) { event ->
FacebookInvocationRequest.create(request, event.asTextMessageEvent())?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package com.justai.jaicf.channel.jaicp
/**
* Events which can be send with JaicpBotRequest
*
* @property fileEvent - is sent when user sends file or image. File will be uploaded to s3 storage,
* @property FILE_EVENT - is sent when user sends file or image. File will be uploaded to s3 storage,
* link will be provided in request.telephony?.jaicp?.data
* @property liveChatFinished - is sent when livechat is finished and request execution is returned to bot.
* @property noLivechatOperatorsOnline - is sent when scenario attempted switchToLiveChat, but no operators were online.
* @property LIVE_CHAT_FINISHED - is sent when livechat is finished and request execution is returned to bot.
* @property NO_LIVE_CHAT_OPERATORS_ONLINE - is sent when scenario attempted switchToLiveChat, but no operators were online.
*
* @see com.justai.jaicf.channel.jaicp.channels.TelephonyEvents
* @see com.justai.jaicf.channel.jaicp.reactions.switchToLiveChat
* */
@Suppress("MemberVisibilityCanBePrivate")
object JaicpEvents {
const val fileEvent = "fileEvent"
const val liveChatFinished = "livechatFinished"
const val noLivechatOperatorsOnline = "noLivechatOperatorsOnline"
const val FILE_EVENT = "fileEvent"
const val LIVE_CHAT_FINISHED = "livechatFinished"
const val NO_LIVE_CHAT_OPERATORS_ONLINE = "noLivechatOperatorsOnline"
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private fun tryProcessAsExternalInvocation(
} catch (e: Exception) {
return false
}
channel.processExternalInvocation(
channel.processInvocation(
request = InvocationEventRequest(data.chatId, event, request.raw),
requestContext = RequestContext.fromHttp(request.asHttpBotRequest())
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,20 @@ internal class ChatAdapterConnector private constructor(

fun initLiveChat(liveChatInitRequest: LiveChatInitRequest) {
try {
initLiveChat0(liveChatInitRequest)
} catch (e: ClientRequestException){
when(e.response?.status){
runBlocking {
httpClient.post<String>("$baseUrl/initLiveChatSwitch") {
body = liveChatInitRequest
contentType(ContentType.Application.Json)
}
}
} catch (e: ClientRequestException) {
when (e.response?.status) {
HttpStatusCode.NotFound -> throw NoOperatorsOnlineException(liveChatInitRequest.request)
HttpStatusCode.BadRequest -> throw NoOperatorChannelConfiguredException(liveChatInitRequest.request)
}
}
}

private fun initLiveChat0(liveChatInitRequest: LiveChatInitRequest) = runBlocking {
httpClient.post<String>("$baseUrl/initLiveChatSwitch") {
body = liveChatInitRequest
contentType(ContentType.Application.Json)
}
}

companion object {
private var INSTANCE: ChatAdapterConnector? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import com.justai.jaicf.channel.jaicp.http.ChatAdapterConnector
import com.justai.jaicf.channel.jaicp.livechat.LiveChatInitRequest
import com.justai.jaicf.channel.jaicp.livechat.exceptions.NoOperatorChannelConfiguredException
import com.justai.jaicf.channel.jaicp.livechat.exceptions.NoOperatorsOnlineException
import com.justai.jaicf.logging.Reaction
import com.justai.jaicf.channel.jaicp.reactions.reaction.SwitchReaction
import com.justai.jaicf.reactions.jaicp.JaicpCompatibleAsyncReactions
import kotlinx.serialization.json.JsonObject

/**
* Switches to livechat operator if channel is connected to livechat in JAICP App Console.
Expand Down Expand Up @@ -38,35 +37,3 @@ fun JaicpCompatibleAsyncReactions.switchToLiveChat(reply: LiveChatSwitchReply):
return SwitchReaction.fromReply(switchRequest.switchData, loggingContext.botContext.dialogContext.currentState)
.also { loggingContext.reactions.add(it) }
}

data class SwitchReaction(
val firstMessage: String? = null,
val closeChatPhrases: List<String> = emptyList(),
val appendCloseChatButton: Boolean = false,
val ignoreOffline: Boolean = false,
val oneTimeMessage: Boolean = false,
val destination: String? = null,
val lastMessage: String? = null,
val attributes: JsonObject? = null,
val hiddenAttributes: JsonObject? = null,
val sendMessagesToOperator: Boolean = false,
val sendMessageHistoryAmount: Int? = null,
override val fromState: String
) : Reaction(fromState) {
companion object {
fun fromReply(switchReply: LiveChatSwitchReply, state: String) = SwitchReaction(
firstMessage = switchReply.firstMessage,
closeChatPhrases = switchReply.closeChatPhrases,
appendCloseChatButton = switchReply.appendCloseChatButton,
ignoreOffline = switchReply.ignoreOffline,
oneTimeMessage = switchReply.oneTimeMessage,
destination = switchReply.destination,
lastMessage = switchReply.lastMessage,
attributes = switchReply.attributes,
hiddenAttributes = switchReply.hiddenAttributes,
sendMessageHistoryAmount = switchReply.sendMessageHistoryAmount,
sendMessagesToOperator = switchReply.sendMessagesToOperator,
fromState = state
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.justai.jaicf.channel.jaicp.reactions.reaction

import com.justai.jaicf.channel.jaicp.dto.LiveChatSwitchReply
import com.justai.jaicf.logging.Reaction
import kotlinx.serialization.json.JsonObject

data class SwitchReaction(
val firstMessage: String? = null,
val closeChatPhrases: List<String> = emptyList(),
val appendCloseChatButton: Boolean = false,
val ignoreOffline: Boolean = false,
val oneTimeMessage: Boolean = false,
val destination: String? = null,
val lastMessage: String? = null,
val attributes: JsonObject? = null,
val hiddenAttributes: JsonObject? = null,
val sendMessagesToOperator: Boolean = false,
val sendMessageHistoryAmount: Int? = null,
override val fromState: String
) : Reaction(fromState) {
companion object {
fun fromReply(switchReply: LiveChatSwitchReply, state: String) = SwitchReaction(
firstMessage = switchReply.firstMessage,
closeChatPhrases = switchReply.closeChatPhrases,
appendCloseChatButton = switchReply.appendCloseChatButton,
ignoreOffline = switchReply.ignoreOffline,
oneTimeMessage = switchReply.oneTimeMessage,
destination = switchReply.destination,
lastMessage = switchReply.lastMessage,
attributes = switchReply.attributes,
hiddenAttributes = switchReply.hiddenAttributes,
sendMessageHistoryAmount = switchReply.sendMessageHistoryAmount,
sendMessagesToOperator = switchReply.sendMessagesToOperator,
fromState = state
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ class SlackChannel private constructor(

override fun provideTimestamp(): String = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()).toString()

override fun processExternalInvocation(request: InvocationRequest, requestContext: RequestContext) {
override fun processInvocation(request: InvocationRequest, requestContext: RequestContext) {
val invocationRequest = SlackInvocationRequest.create(request) ?: return
val slackRequest = buildSlackRequest(getRequestTemplateFromResources(request, REQUEST_TEMPLATE_PATH).asHttpBotRequest())
val slackRequest =
buildSlackRequest(getRequestTemplateFromResources(request, REQUEST_TEMPLATE_PATH).asHttpBotRequest())
botApi.process(invocationRequest, SlackReactions(slackRequest.context), requestContext)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.justai.jaicf.context.RequestContext
import com.justai.jaicf.helpers.kotlin.PropertyWithBackingField
import com.justai.jaicf.channel.invocationapi.InvocableBotChannel
import com.justai.jaicf.channel.invocationapi.InvocationRequest
import java.util.*
import java.util.concurrent.TimeUnit

class TelegramChannel(
Expand Down Expand Up @@ -100,9 +101,12 @@ class TelegramChannel(
return null
}

override fun provideTimestamp(): String = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()).toString()
override fun getRequestTemplateFromResources(request: InvocationRequest, resourceName: String): String =
super.getRequestTemplateFromResources(request, resourceName)
.replace("\"{{ timestamp }}\"", TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()).toString())
.replace("{{ messageId }}", UUID.randomUUID().toString())

override fun processExternalInvocation(request: InvocationRequest, requestContext: RequestContext) {
override fun processInvocation(request: InvocationRequest, requestContext: RequestContext) {
val template = getRequestTemplateFromResources(request, REQUEST_TEMPLATE_PATH)
val message = gson.fromJson(template, Update::class.java).message ?: return
val telegramRequest = TelegramInvocationRequest.create(request, message) ?: return
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.justai.jaicf.channel.invocationapi

import com.justai.jaicf.channel.http.HttpBotRequest
import com.justai.jaicf.channel.http.asHttpBotRequest
import com.justai.jaicf.context.RequestContext
import com.justai.jaicf.helpers.logging.WithLogger
Expand All @@ -9,10 +8,11 @@ import kotlin.random.Random

/**
* Base class for all channels able to process requests from external service.
* Allows to send [InvocationEventRequest] or [InvocationQueryRequest] with client identifier to implementations at any time.
* Any class implementing [InvocableBotChannel] receives method [processInvocation] allowing to process query or event requests with client identifier from any external source.
*
* @see InvocationRequest
*
* @see InvocationQueryRequest
* @see InvocationEventRequest
* */
interface InvocableBotChannel : WithLogger {
/**
Expand All @@ -23,7 +23,7 @@ interface InvocableBotChannel : WithLogger {
*
* @see InvocableBotChannel
*/
fun processExternalInvocation(request: InvocationRequest, requestContext: RequestContext)
fun processInvocation(request: InvocationRequest, requestContext: RequestContext)

/**
* Provides a messageId for substitution in request template
Expand Down Expand Up @@ -64,10 +64,10 @@ interface InvocableBotChannel : WithLogger {
* @see InvocationServlet
* @see botInvocationRouting
* */
internal fun InvocableBotChannel.processExternalInvocation(
internal fun InvocableBotChannel.processInvocation(
queryParams: InvocationQueryParams,
requestData: String
) = processExternalInvocation(
) = processInvocation(
request = when (queryParams.type) {
InvocationRequestType.EVENT -> InvocationEventRequest(queryParams.clientId, queryParams.input, requestData)
InvocationRequestType.QUERY -> InvocationQueryRequest(queryParams.clientId, queryParams.input, requestData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal class InvocationQueryParams(queryParamsMap: Map<String, List<String>>)
}

val clientId: String = requireNotNull(queryParamsMap["clientId"]?.firstOrNull()) {
"clientId path variable must be specified for gateway call"
"clientId path variable must be specified for invocation api call"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.justai.jaicf.api.BotRequest
import com.justai.jaicf.api.EventBotRequest
import com.justai.jaicf.api.QueryBotRequest

val BotRequest.invocationApi get() = this as? InvocationRequest
val BotRequest.invocationRequest get() = this as? InvocationRequest

/**
* A request processed by [InvocableBotChannel] implementation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ typealias RouteToInvocableChannel = Pair<String, InvocableBotChannel>
fun Routing.botInvocationRouting(vararg routes: RouteToInvocableChannel) {
routes.forEach { channel ->
post(channel.first) {
channel.second.processExternalInvocation(call)
channel.second.processInvocation(call)
}

get(channel.first) {
channel.second.processExternalInvocation(call)
channel.second.processInvocation(call)
}
}
}

private suspend fun InvocableBotChannel.processExternalInvocation(call: ApplicationCall) =
processExternalInvocation(queryParams = InvocationQueryParams(call.request), requestData = call.receiveText())
private suspend fun InvocableBotChannel.processInvocation(call: ApplicationCall) =
processInvocation(queryParams = InvocationQueryParams(call.request), requestData = call.receiveText())


Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ open class InvocationServlet(
) : HttpServlet(), WithLogger {

override fun doPost(req: HttpServletRequest?, resp: HttpServletResponse?) {
req?.run { channel.processExternalInvocation(req) }
req?.run { channel.processInvocation(req) }
}

override fun doGet(req: HttpServletRequest?, resp: HttpServletResponse?) {
req?.run { channel.processExternalInvocation(req) }
req?.run { channel.processInvocation(req) }
}
}

/**
* Processes invocation request from [InvocationServlet]
* */
private fun InvocableBotChannel.processExternalInvocation(req: HttpServletRequest) =
processExternalInvocation(InvocationQueryParams(req), req.inputStream.bufferedReader().readText())
private fun InvocableBotChannel.processInvocation(req: HttpServletRequest) =
processInvocation(InvocationQueryParams(req), req.inputStream.bufferedReader().readText())

0 comments on commit 30aac0e

Please sign in to comment.