# KOOK(开黑啦)

> For the complete documentation index, see [llms.txt](https://simbot.forte.love/llms.txt).

[https://github.com/simple-robot/simpler-robot/releases](https://github.com/simple-robot/simpler-robot/releases)

## See also

### 相关链接

[KOOK组件源码](https://github.com/simple-robot/simpler-robot/tree/dev/simbot-component-kook) [KOOK开发者平台](https://developer.kookapp.cn/doc/reference) [Ktor首页](https://ktor.io/)

## 概述

KOOK组件 是一个 [Kotlin 多平台](https://kotlinlang.org/docs/multiplatform.html) 的 [KOOK机器人官方API](https://developer.kookapp.cn/doc/reference) SDK实现库， 也是 Simple Robot 标准API下实现的组件库，异步高效、Java友好！

> **Tip:**
> 序列化和网络请求相关分别基于 [Kotlin serialization](https://github.com/Kotlin/kotlinx.serialization) 和 [Ktor](https://ktor.io/).

* 前往KOOK组件的 [源码目录](https://github.com/simple-robot/simpler-robot/tree/dev/simbot-component-kook)

> **Tip:**
> 自 `5.0` 起，KOOK 组件在主仓库中维护，随标准库统一版本发布，模块坐标保持不变。 核心模块提供 JVM、JS、`wasm-js` 和部分 Native 目标。

## 模块

KOOK 组件主要分为下面三层：

simbot-component-kook-api
: KOOK API 的底层封装模块。
:
:
:
: * 定义各类 API 请求
:
: * 定义事件模型、对象模型与消息结构
:
: * 适合仅需要原始 API 能力的场景

simbot-component-kook-stdlib
: 基于 `api` 模块的 Bot 基础实现与事件处理模块。
:
:
:
: 适合想保留更接近原始 KOOK 事件流程、但又不想从零处理鉴权与订阅逻辑的场景。

simbot-component-kook-core
: 完整的 simbot KOOK 组件模块。
:
:
:
: 普通开发者通常直接使用它，并通过 `useKook()`、`KookBotManager`、 `KookBot` 等 API 进行集成。

## 安装

### 安装组件库

Procedure: 安装依赖

1. 安装simbot核心库实现

> **Note:**
> 在安装组件库之前，确保你已经安装了可用的核心库实现，比如 [核心库](start-use-core.html) 或 [Spring Boot starter](spring-boot.html) 。

核心库:

Gradle(Kotlin DSL):

```KOTLIN
implementation("love.forte.simbot:simbot-core:5.0.0-Alpha2")
```

如果使用 Java 而不配合使用 Gradle 的 [Kotlin 插件](https://kotlinlang.org/docs/gradle-configure-project.html#apply-the-plugin), 那么你需要指定依赖的后缀为 `-jvm`。

```KOTLIN
implementation("love.forte.simbot:simbot-core-jvm:5.0.0-Alpha2")
```

Gradle(Groovy):

```GROOVY
implementation 'love.forte.simbot:simbot-core:5.0.0-Alpha2'
```

如果使用 Java 而不配合使用 Gradle 的 [Kotlin 插件](https://kotlinlang.org/docs/gradle-configure-project.html#apply-the-plugin), 那么你需要指定依赖的后缀为 `-jvm`。

```GROOVY
implementation 'love.forte.simbot:simbot-core-jvm:5.0.0-Alpha2'
```

Maven:

```XML
<dependency>
    <groupId>love.forte.simbot</groupId>
    <artifactId>simbot-core-jvm</artifactId>
    <version>5.0.0-Alpha2</version>
</dependency>
```

> **Tip:**
> 前往 [核心库](start-use-core.html) 了解更多内容。

Spring Boot starter:

Spring Boot 3:

Gradle(Kotlin DSL):

```KOTLIN
implementation("love.forte.simbot:simbot-core-spring-boot-starter:5.0.0-Alpha2")
```

Gradle(Groovy):

```GROOVY
implementation 'love.forte.simbot:simbot-core-spring-boot-starter:5.0.0-Alpha2'
```

Maven:

```XML
<dependency>
    <groupId>love.forte.simbot</groupId>
    <artifactId>simbot-core-spring-boot-starter</artifactId>
    <version>5.0.0-Alpha2</version>
</dependency>
```

> **Tip:**
> 前往 [使用 Spring Boot 3](start-use-spring-boot-3.html) 或 [集成Spring Boot](spring-boot.html) 了解更多内容。

Spring Boot 2:

Gradle(Kotlin DSL):

```KOTLIN
implementation("love.forte.simbot:simbot-core-spring-boot-starter-v2:5.0.0-Alpha2")
```

Gradle(Groovy):

```GROOVY
implementation 'love.forte.simbot:simbot-core-spring-boot-starter-v2:5.0.0-Alpha2'
```

Maven:

```XML
<dependency>
    <groupId>love.forte.simbot</groupId>
    <artifactId>simbot-core-spring-boot-starter-v2</artifactId>
    <version>5.0.0-Alpha2</version>
</dependency>
```

> **Tip:**
> 前往 [使用 Spring Boot 2](start-use-spring-boot-2.html) 了解更多内容。

2. 安装组件的核心库依赖

`simbot-component-kook-core` 即为 KOOK 组件的核心库， 也就是作为simbot组件所使用的 组件库 。

Gradle(Kotlin DSL):

```KOTLIN
implementation("love.forte.simbot.component:simbot-component-kook-core:5.0.0-Alpha2")
```

如果使用 Java 而不配合使用 Gradle 的 `kotlin` 插件, 那么你需要指定依赖的后缀为 `-jvm`。

```KOTLIN
implementation("love.forte.simbot.component:simbot-component-kook-core-jvm:5.0.0-Alpha2")
```

Gradle(Groovy):

```GROOVY
implementation 'love.forte.simbot.component:simbot-component-kook-core:5.0.0-Alpha2'
```

如果使用 Java 而不配合使用 Gradle 的 `kotlin` 插件, 那么你需要指定依赖的后缀为 `-jvm`。

```GROOVY
implementation 'love.forte.simbot.component:simbot-component-kook-core-jvm:5.0.0-Alpha2'
```

Maven:

```XML
<dependency>
    <groupId>love.forte.simbot.component</groupId>
    <artifactId>simbot-component-kook-core-jvm</artifactId>
    <version>5.0.0-Alpha2</version>
</dependency>
```

3. 安装Ktor客户端引擎

KOOK组件使用 [Ktor](https://ktor.io) 作为 HTTP 客户端实现， 但是默认不会依赖任何具体的引擎。

因此，你需要选择并使用一个 Ktor Client 引擎实现。

> **Warning:**
> 如果你使用 `stdlib` / `core` 中的 Bot 接收事件， 那么你需要选择一个支持 WebSocket 的引擎。

你可以前往 [Ktor文档](https://ktor.io/docs/http-client-engines.html) 处选择一个对应所用平台下合适的 `Client Engine`。 这里会根据不同平台提供几个示例，你可以选择其他可用目标。

> **Tip:**
> 注意应选择使用 `v2.x` 版本的 Ktor。5.0 后的版本会更新到 Ktor 3。

JVM:

Java:

在 Java11+ 的环境下，可以选择 [Java](https://ktor.io/docs/http-client-engines.html#java) 引擎。

Gradle Kotlin DSL:

```KOTLIN
runtimeOnly("io.ktor:ktor-client-java:$ktor_version")
```

如果不使用Kotlin的[Gradle插件](https://kotlinlang.org/docs/gradle-configure-project.html):

```KOTLIN
runtimeOnly("io.ktor:ktor-client-java-jvm:$ktor_version")
```

Gradle Groovy:

```GROOVY
runtimeOnly 'io.ktor:ktor-client-java:$ktor_version'
```

如果不使用Kotlin的[Gradle插件](https://kotlinlang.org/docs/gradle-configure-project.html):

```GROOVY
runtimeOnly 'io.ktor:ktor-client-java-jvm:$ktor_version'
```

Maven:

```XML
<dependency>
    <groupId>io.ktor</groupId>
    <artifactId>ktor-client-java-jvm</artifactId>
    <version>${ktor_version}</version>
    <scope>runtime</scope>
</dependency>
```

OkHttp:

[OkHttp](https://ktor.io/docs/client-engines.html#okhttp) 引擎基于 OkHttp，是一个不错的库。

Gradle Kotlin DSL:

```KOTLIN
runtimeOnly("io.ktor:ktor-client-okhttp:$ktor_version")
```

如果不使用Kotlin的[Gradle插件](https://kotlinlang.org/docs/gradle-configure-project.html):

```KOTLIN
runtimeOnly("io.ktor:ktor-client-okhttp-jvm:$ktor_version")
```

Gradle Groovy:

```GROOVY
runtimeOnly 'io.ktor:ktor-client-okhttp:$ktor_version'
```

如果不使用Kotlin的[Gradle插件](https://kotlinlang.org/docs/gradle-configure-project.html):

```GROOVY
runtimeOnly 'io.ktor:ktor-client-okhttp-jvm:$ktor_version'
```

Maven:

```XML
<dependency>
    <groupId>io.ktor</groupId>
    <artifactId>ktor-client-okhttp-jvm</artifactId>
    <version>${ktor_version}</version>
    <scope>runtime</scope>
</dependency>
```

CIO:

[CIO](https://ktor.io/docs/http-client-engines.html#cio) 是一个比较通用的引擎。 在不知道选什么的情况下，可以考虑使用它。

Gradle Kotlin DSL:

```KOTLIN
runtimeOnly("io.ktor:ktor-client-cio:$ktor_version")
```

如果不使用Kotlin的[Gradle插件](https://kotlinlang.org/docs/gradle-configure-project.html):

```KOTLIN
runtimeOnly("io.ktor:ktor-client-cio-jvm:$ktor_version")
```

Gradle Groovy:

```GROOVY
runtimeOnly 'io.ktor:ktor-client-cio:$ktor_version'
```

如果不使用Kotlin的[Gradle插件](https://kotlinlang.org/docs/gradle-configure-project.html):

```GROOVY
runtimeOnly 'io.ktor:ktor-client-cio-jvm:$ktor_version'
```

Maven:

```XML
<dependency>
    <groupId>io.ktor</groupId>
    <artifactId>ktor-client-cio-jvm</artifactId>
    <version>${ktor_version}</version>
    <scope>runtime</scope>
</dependency>
```

> **Tip:**
> 引擎大部分情况下可以 runtime only，除非你需要显式地定制化它。

JavaScript:

JavaScript 平台下可以选择 [Js](https://ktor.io/docs/http-client-engines.html#js) 引擎。

Gradle Kotlin DSL:

```KOTLIN
implementation("io.ktor:ktor-client-js:$ktor_version")
```

Gradle Groovy:

```GROOVY
implementation 'io.ktor:ktor-client-js:$ktor_version'
```

Native:

native 平台目标下，可能需要根据不同的平台类型选择不同的引擎。

Mingw:

可以选择 [WinHttp](https://ktor.io/docs/http-client-engines.html#winhttp) 引擎。

Gradle Kotlin DSL:

```KOTLIN
implementation("io.ktor:ktor-client-winhttp:$ktor_version")
```

Gradle Groovy:

```GROOVY
implementation 'io.ktor:ktor-client-winhttp:$ktor_version'
```

Linux:

Linux 下依旧可以选择 [CIO](https://ktor.io/docs/http-client-engines.html#cio) 引擎。

Gradle Kotlin DSL:

```KOTLIN
implementation("io.ktor:ktor-client-cio:$ktor_version")
```

Gradle Groovy:

```GROOVY
implementation 'io.ktor:ktor-client-cio:$ktor_version'
```

MacOS:

可以选择 [Darwin](https://ktor.io/docs/http-client-engines.html#darwin) 引擎。

Gradle Kotlin DSL:

```KOTLIN
implementation("io.ktor:ktor-client-darwin:$ktor_version")
```

Gradle Groovy:

```GROOVY
implementation 'io.ktor:ktor-client-darwin:$ktor_version'
```

