Skip to content

Commit

Permalink
Feature: get message from id
Browse files Browse the repository at this point in the history
  • Loading branch information
ryoii committed Mar 1, 2020
1 parent c6d4513 commit 288d077
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,44 @@ Content-Type:multipart/form-data
```


### 通过messageId获取一条被缓存的消息

```
[GET] /messageFromId?sessionKey=YourSessionKey&id=1234567890
```

使用此方法获取bot接收到的消息和各类事件

#### 请求:

| 名字 | 可选 | 举例 | 说明 |
| ---------- | ----- | -------------- | -------------------- |
| sessionKey | false | YourSessionKey | 你的session key |
| id | false | 1234567890 | 获取消息的messageId |

#### 响应: 返回JSON对象

**当该messageId没有被缓存或缓存失效时,返回code 5(指定对象不存在)**

```json5
{
"type": "FriendMessage", // 消息类型:GroupMessage或FriendMessage或各类Event
"messageChain": [{ // 消息链,是一个消息对象构成的数组
"type": "Source",
"id": 123456
},{
"type": "Plain",
"text": "Miral牛逼"
}],
"sender": { // 发送者信息
"id": 1234567890, // 发送者的QQ号码
"nickName": "", // 发送者的昵称
"remark": "" // 发送者的备注
}
}
```



### 事件类型一览
[事件类型一览](EventType.md)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,17 @@ package net.mamoe.mirai.api.http.route

import io.ktor.application.Application
import io.ktor.application.call
import io.ktor.http.HttpStatusCode
import io.ktor.http.content.readAllParts
import io.ktor.http.content.streamProvider
import io.ktor.request.receiveMultipart
import io.ktor.response.respond
import io.ktor.response.respondText
import io.ktor.routing.post
import io.ktor.routing.routing
import kotlinx.serialization.Serializable
import net.mamoe.mirai.api.http.AuthedSession
import net.mamoe.mirai.api.http.SessionManager
import net.mamoe.mirai.api.http.data.*
import net.mamoe.mirai.api.http.data.common.DTO
import net.mamoe.mirai.api.http.data.common.MessageChainDTO
import net.mamoe.mirai.api.http.data.common.VerifyDTO
import net.mamoe.mirai.api.http.data.common.toMessageChain
import net.mamoe.mirai.api.http.data.common.*
import net.mamoe.mirai.api.http.util.toJson
import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.message.FriendMessage
Expand All @@ -46,6 +41,13 @@ fun Application.messageModule() {
call.respondJson(fetch.toJson())
}

miraiGet("/messageFromId") {
val id: Long = paramOrNull("id")
it.messageQueue.cache[id]?.apply {
call.respondDTO(this.toDTO())
} ?: throw NoSuchElementException()
}

suspend fun <C : Contact> sendMessage(
quote: QuoteReplyToSend?,
messageChain: MessageChain,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ object MiraiJson {
@UnstableDefault
val json = Json(
configuration = JsonConfiguration(
strictMode = false,
useArrayPolymorphism = true
strictMode = false
),
context = SerializersModule {

Expand Down

0 comments on commit 288d077

Please sign in to comment.