All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.misk.slack.webapi.SlackApi.kt Maven / Gradle / Ivy

There is a newer version: 2024.09.25.163146-1b49d90
Show newest version
package misk.slack.webapi

import misk.slack.webapi.helpers.PostMessageRequest
import misk.slack.webapi.helpers.PostMessageResponse
import misk.slack.webapi.helpers.GetUserResponse
import retrofit2.Call
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.Headers
import retrofit2.http.POST
import retrofit2.http.Query
import retrofit2.http.Url

interface SlackApi {
  /**
   * Calls Slack and asks it to post message.
   * https://api.slack.com/methods/chat.postMessage
   */
  @POST("/api/chat.postMessage")
  @Headers(value = ["accept: application/json"])
  fun postMessage(
    @Body postMessageJson: PostMessageRequest,
  ): Call

  /**
   * Calls Slack and asks it to post a confirmation message to the dynamic URL
   * sent from Slack.
   * https://api.slack.com/interactivity/handling#message_responses
   */
  @POST
  @Headers(value = ["accept: application/json"])
  fun postConfirmation(
    @Url url: String,
    @Body confirmationMessageJson: PostMessageRequest,
  ): Call

  /**
   * Calls Slack to fetch user for given email.
   *
   * https://api.slack.com/methods/users.lookupByEmail
   */
  @GET("/api/users.lookupByEmail")
  @Headers(value = ["accept: application/json"])
  fun getUserByEmail(@Query("email") email: String): Call
}

fun Response.checkSuccessful() {
  check(isSuccessful) {
    "Slack HTTP call failed: ${errorBody()!!.string()}"
  }

  val postMessageResponseJson = body()!!
  check(postMessageResponseJson.ok) {
    "Slack call failed: $postMessageResponseJson"
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy