Skip to content

Commit

Permalink
Merge pull request #71 from KjellBerlin/Update-slack-message
Browse files Browse the repository at this point in the history
Update slack message based on user interaction
  • Loading branch information
KjellBerlin authored Sep 23, 2024
2 parents e2d52e0 + aa34325 commit ada964e
Show file tree
Hide file tree
Showing 4 changed files with 296 additions and 19 deletions.
269 changes: 262 additions & 7 deletions src/main/kotlin/com/carbonara/core/slack/SlackMessageService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.carbonara.core.slack
import com.carbonara.core.order.OrderStatus
import com.slack.api.Slack
import com.slack.api.methods.kotlin_extension.request.chat.blocks
import com.slack.api.methods.response.chat.ChatUpdateResponse
import mu.KotlinLogging
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
Expand Down Expand Up @@ -72,13 +73,33 @@ class SlackMessageService {
}
}

// TODO: Update depending on actual status
fun updateOrderMessageToAccepted(
fun updateOrderMessage(
params: SlackMessageParams
) {
val slackResponse = when(params.orderStatus) {
OrderStatus.RIDER_ASSIGNED -> updateOrderMessageToAccepted(params)
OrderStatus.DELIVERY_IN_PROGRESS -> updateOrderMessageToDeliveryInProgress(params)
OrderStatus.DELIVERED -> updateOrderMessageToDelivered(params)
OrderStatus.CANCELLED -> updateOrderMessageToCancelled(params)
OrderStatus.FINDING_AVAILABLE_RIDER -> updateOrderMessageToUnassigned(params)
else -> throw IllegalArgumentException("Cannot update slack message based on order status ${params.orderStatus}")
}

if (slackResponse == null) {
log.error("Slack API error: Slack response null")
throw SlackException("Failed to update slack message for orderId: ${params.orderId}. Error: Slack response null")
} else if (!slackResponse.isOk) {
log.error("Slack API error: ${slackResponse.error}")
throw SlackException("Failed to update slack message for orderId: ${params.orderId}. Error: ${slackResponse.error}")
}
}

private fun updateOrderMessageToAccepted(
params: SlackMessageParams
): ChatUpdateResponse? {

val slack = Slack.getInstance()
val response = slack.methods(slackToken).chatUpdate { req -> req
return slack.methods(slackToken).chatUpdate { req -> req
.channel(slackChannel)
.ts(params.timeStamp)
.blocks {
Expand All @@ -94,6 +115,11 @@ class SlackMessageService {
markdownText("*Products:*\n${params.productNames.joinToString(", ")}")
}
}
section {
fields {
markdownText("*Rider:*\n<@${params.slackUserId}>")
}
}
actions {
button {
text("ACCEPT", emoji = true)
Expand All @@ -116,17 +142,245 @@ class SlackMessageService {
value(params.orderId)
actionId("cancelled")
}
button {
text("UNASSIGN", emoji = true)
value(params.orderId)
actionId("unassign")
}
}
divider()
}
}
}

if (!response.isOk) {
log.error("Slack API error: ${response.error}")
throw SlackException("Failed to update slack message for orderId: ${params.orderId}. Error: ${response.error}")
private fun updateOrderMessageToDeliveryInProgress(
params: SlackMessageParams
): ChatUpdateResponse? {

val slack = Slack.getInstance()
return slack.methods(slackToken).chatUpdate { req -> req
.channel(slackChannel)
.ts(params.timeStamp)
.blocks {
section {
fields {
markdownText("*Customer Name:*\n${params.customerName}")
markdownText("*OrderId:*\n${params.orderId}")
}
}
section {
fields {
markdownText("*Address:*\n${params.address}\n${params.googleMapsLink}")
markdownText("*Products:*\n${params.productNames.joinToString(", ")}")
}
}
section {
fields {
markdownText("*Rider:*\n<@${params.slackUserId}>")
}
}
actions {
button {
text("ACCEPT", emoji = true)
value(params.orderId)
actionId("accept")
}
button {
text("DELIVERY IN PROGRESS", emoji = true)
style("primary")
value(params.orderId)
actionId("delivery_in_progress")
}
button {
text("DELIVERED", emoji = true)
value(params.orderId)
actionId("delivered")
}
button {
text("CANCELLED", emoji = true)
value(params.orderId)
actionId("cancelled")
}
button {
text("UNASSIGN", emoji = true)
value(params.orderId)
actionId("unassign")
}
}
divider()
}
}
}

private fun updateOrderMessageToDelivered(
params: SlackMessageParams
): ChatUpdateResponse? {

val slack = Slack.getInstance()
return slack.methods(slackToken).chatUpdate { req -> req
.channel(slackChannel)
.ts(params.timeStamp)
.blocks {
section {
fields {
markdownText("*Customer Name:*\n${params.customerName}")
markdownText("*OrderId:*\n${params.orderId}")
}
}
section {
fields {
markdownText("*Address:*\n${params.address}\n${params.googleMapsLink}")
markdownText("*Products:*\n${params.productNames.joinToString(", ")}")
}
}
section {
fields {
markdownText("*Rider:*\n<@${params.slackUserId}>")
}
}
actions {
button {
text("ACCEPT", emoji = true)
value(params.orderId)
actionId("accept")
}
button {
text("DELIVERY IN PROGRESS", emoji = true)
value(params.orderId)
actionId("delivery_in_progress")
}
button {
text("DELIVERED", emoji = true)
style("primary")
value(params.orderId)
actionId("delivered")
}
button {
text("CANCELLED", emoji = true)
value(params.orderId)
actionId("cancelled")
}
button {
text("UNASSIGN", emoji = true)
value(params.orderId)
actionId("unassign")
}
}
divider()
}
}
}

private fun updateOrderMessageToCancelled(
params: SlackMessageParams
): ChatUpdateResponse? {

val slack = Slack.getInstance()
return slack.methods(slackToken).chatUpdate { req -> req
.channel(slackChannel)
.ts(params.timeStamp)
.blocks {
section {
fields {
markdownText("*Customer Name:*\n${params.customerName}")
markdownText("*OrderId:*\n${params.orderId}")
}
}
section {
fields {
markdownText("*Address:*\n${params.address}\n${params.googleMapsLink}")
markdownText("*Products:*\n${params.productNames.joinToString(", ")}")
}
}
section {
fields {
markdownText("*Rider:*\n<@${params.slackUserId}>")
}
}
actions {
button {
text("ACCEPT", emoji = true)
value(params.orderId)
actionId("accept")
}
button {
text("DELIVERY IN PROGRESS", emoji = true)
value(params.orderId)
actionId("delivery_in_progress")
}
button {
text("DELIVERED", emoji = true)
value(params.orderId)
actionId("delivered")
}
button {
text("CANCELLED", emoji = true)
style("danger")
value(params.orderId)
actionId("cancelled")
}
button {
text("UNASSIGN", emoji = true)
value(params.orderId)
actionId("unassign")
}
}
divider()
}
}
}

fun updateOrderMessageToUnassigned(
params: SlackMessageParams
): ChatUpdateResponse? {

val slack = Slack.getInstance()
return slack.methods(slackToken).chatUpdate { req -> req
.channel(slackChannel)
.ts(params.timeStamp)
.blocks {
section {
fields {
markdownText("*Customer Name:*\n${params.customerName}")
markdownText("*OrderId:*\n${params.orderId}")
}
}
section {
fields {
markdownText("*Address:*\n${params.address}\n${params.googleMapsLink}")
markdownText("*Products:*\n${params.productNames.joinToString(", ")}")
}
}
actions {
button {
text("ACCEPT", emoji = true)
style("primary")
value(params.orderId)
actionId("accept")
}
button {
text("DELIVERY IN PROGRESS", emoji = true)
style("primary")
value(params.orderId)
actionId("delivery_in_progress")
}
button {
text("DELIVERED", emoji = true)
style("primary")
value(params.orderId)
actionId("delivered")
}
button {
text("CANCELLED", emoji = true)
style("danger")
value(params.orderId)
actionId("cancelled")
}
}
divider()
}
}
}

companion object {
private val log = KotlinLogging.logger {}
Expand All @@ -140,5 +394,6 @@ data class SlackMessageParams(
val googleMapsLink: String,
val productNames: List<String>,
val timeStamp: String? = null,
val orderStatus: OrderStatus? = null
val orderStatus: OrderStatus? = null,
val slackUserId: String? = null
)
9 changes: 6 additions & 3 deletions src/main/kotlin/com/carbonara/core/slack/SlackService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,24 @@ class SlackService(
suspend fun handleOrderStatusUpdate(
orderId: String,
slackOrderStatus: String,
messageTimestamp: String
messageTimestamp: String,
slackUserId: String
) {
val orderStatus = mapSlackOrderStatusToOrderStatus(slackOrderStatus)
val order = orderService.updateOrderStatus(
orderId = orderId,
orderStatus = orderStatus
)
slackMessageService.updateOrderMessageToAccepted(
slackMessageService.updateOrderMessage(
SlackMessageParams(
customerName = order.userName,
orderId = orderId,
address = order.deliveryAddress.toString(),
googleMapsLink = order.deliveryAddress.createGoogleMapsLink(),
productNames = order.products.map { it.productName },
timeStamp = messageTimestamp,
orderStatus = orderStatus
orderStatus = orderStatus,
slackUserId = slackUserId
)
)
}
Expand All @@ -41,6 +43,7 @@ class SlackService(
"delivery_in_progress" -> OrderStatus.DELIVERY_IN_PROGRESS
"delivered" -> OrderStatus.DELIVERED
"cancelled" -> OrderStatus.CANCELLED
"unassign" -> OrderStatus.FINDING_AVAILABLE_RIDER
else -> throw IllegalArgumentException("Invalid slack order status: $slackOrderStatus")
}
}
Expand Down
Loading

0 comments on commit ada964e

Please sign in to comment.