Kotlin
Kotlin

Install

Add the dependency to the project configuration:

Maven

1
2
3
4
5
<dependency>
    <groupId>com.botbye</groupId>
    <artifactId>kotlin-module</artifactId>
    <version>0.0.9</version>
</dependency>

or

Gradle

1
implementation("com.botbye:kotlin-module:0.0.9")

Configuration

Create BotbyeConfig with your server-key (available inside your Project):

1
2
3
4
// Use your project server-key
val config = BotbyeConfig(serverKey = "00000000-0000-0000-0000-000000000000")

val botbye = Botbye(config)

Usage

Add validateRequest to your handler

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
suspend fun doGet(
    req: HttpServletRequest,
    resp: HttpServletResponse
) {
    val connectionDetails = ConnectionDetails(
        serverPort = req.serverPort,
        remoteAddr = req.remoteAddr,
        serverName = req.serverName,
        requestMethod = req.method,
        requestUri = req.requestURI
    )

    val headers = extractHeaders(req)

    // Get token from header or any place you store it.
    // For example in "x-botbye-token" header
    val token = req.getHeader("x-botbye-token");

    val response = botbye.validateRequest(token, connectionDetails, headers)

    if (response.result?.isAllowed == false) {
        // your logic
        // for example
        // throw RuntimeException(response.error?.message);
    }
}

private fun extractHeaders(request: HttpServletRequest): Headers {
    val headers = mutableMapOf<String, List<String>>()
    val headerNames = request.headerNames

    while (headerNames.hasMoreElements()) {
        val headerName = headerNames.nextElement()

        val valuesList = Collections.list(request.getHeaders(headerName))

        headers[headerName] = valuesList
    }

    return Headers(headers)
}

Settings

BotbyeConfig contains next configurable parameters:

Setting Description Required Default Value
botbyeEndpoint Host of the API Server no https://verify.botbye.com
serverKey Your BotBye server-key yes -
contentType Content type for API requests no application/json
readTimeout Read timeout for HTTP client no Duration.ofSeconds(2)
writeTimeout Write timeout for HTTP client no Duration.ofSeconds(2)
connectionTimeout Connection timeout for HTTP client no Duration.ofSeconds(2)
callTimeout Total call timeout no Duration.ofSeconds(5)
maxIdleConnections Max idle connections in the pool no 250
keepAliveDuration Keep-alive duration no Duration.ofSeconds(300)
maxRequestsPerHost Max requests per host no 1500
maxRequests Max requests total no 1500

Examples of BotBye API responses

Bot detected:

1
2
3
4
5
6
7
{
  "reqId": "f77b2abd-c5d7-44f0-be4f-174b04876583",
  "result": {
    "isAllowed": false
  },
  "error": "Automation tool used"
}

Bot not detected:

1
2
3
4
5
6
7
{
  "reqId": "f77b2abd-c5d7-44f0-be4f-174b04876583",
  "result": {
    "isAllowed": true
  },
  "error": null
}

Request banned by custom rule:

1
2
3
4
5
6
7
8
9
{
  "reqId": "f77b2abd-c5d7-44f0-be4f-174b04876583",
  "result": {
    "isAllowed": false
  },
  "error": {
    "message": "Banned by rule: MY_CUSTOM_RULE"
  }
}

Invalid server-key:

1
2
3
4
5
6
7
{
  "reqId": "f77b2abd-c5d7-44f0-be4f-174b04876583",
  "result": null,
  "error": {
    "message": "[BotBye] Bad Request: Invalid Server Key"
  }
}