Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
800b5cba
Commit
800b5cba
authored
Feb 04, 2020
by
ryoii
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
handle no such element exception
parent
878d2ab8
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
6 additions
and
174 deletions
+6
-174
mirai-api-http/src/main/kotlin/net.mamoe.mirai.api.http/MiraiHttpApplication.kt
...n/kotlin/net.mamoe.mirai.api.http/MiraiHttpApplication.kt
+0
-171
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/Session.kt
...-http/src/main/kotlin/net/mamoe/mirai/api/http/Session.kt
+1
-1
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/dto/VerifyDTO.kt
...src/main/kotlin/net/mamoe/mirai/api/http/dto/VerifyDTO.kt
+2
-1
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/BaseRoute.kt
...c/main/kotlin/net/mamoe/mirai/api/http/route/BaseRoute.kt
+2
-0
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt
...tlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt
+1
-1
No files found.
mirai-api-http/src/main/kotlin/net.mamoe.mirai.api.http/MiraiHttpApplication.kt
deleted
100644 → 0
View file @
878d2ab8
package
net.mamoe.mirai.api.http
import
io.ktor.application.Application
import
io.ktor.application.ApplicationCall
import
io.ktor.application.call
import
io.ktor.application.install
import
io.ktor.features.CallLogging
import
io.ktor.features.DefaultHeaders
import
io.ktor.http.HttpMethod
import
io.ktor.http.HttpStatusCode
import
io.ktor.response.respond
import
io.ktor.routing.Route
import
io.ktor.routing.route
import
io.ktor.routing.routing
import
io.ktor.server.engine.applicationEngineEnvironment
import
io.ktor.util.pipeline.ContextDsl
import
io.ktor.util.pipeline.PipelineContext
import
io.ktor.util.pipeline.PipelineInterceptor
import
net.mamoe.mirai.Bot
import
net.mamoe.mirai.contact.sendMessage
import
net.mamoe.mirai.utils.DefaultLogger
import
net.mamoe.mirai.utils.io.hexToBytes
import
net.mamoe.mirai.utils.io.hexToUBytes
fun
main
(
args
:
Array
<
String
>)
{
val
logger
=
DefaultLogger
(
"Mirai HTTP API"
)
//write default first
SessionManager
.
authKey
=
generateSessionKey
()
//用于验证的key, 使用和SessionKey相同的方法生成, 但意义不同
var
port
=
8080
//start port
args
.
forEach
{
if
(
it
.
contains
(
"="
))
{
when
{
it
.
toLowerCase
().
contains
(
"authkey"
)
->
{
SessionManager
.
authKey
=
it
.
split
(
"="
)[
1
].
trim
()
if
(
it
.
length
!
in
8
..
128
){
logger
.
error
(
"Expected authKey length is between 8 to 128"
)
SessionManager
.
authKey
=
generateSessionKey
()
}
logger
.
info
(
"Session Auth Key now is ${SessionManager.authKey}"
)
}
it
.
toLowerCase
().
contains
(
"port"
)
->
{
try
{
port
=
it
.
split
(
"="
)[
1
].
trim
().
toInt
()
}
catch
(
e
:
Exception
){
logger
.
error
(
"Expected -port=xxxxx, xxxxx to be numbers"
)
}
if
(
port
!
in
1025
..
65535
){
logger
.
error
(
"Expected -port=xxxxx, xxxxx > 1024 && <65536"
)
port
=
8080
}
logger
.
info
(
"HTTP API Listening port now is $port"
)
}
}
}
if
(
it
.
contains
(
"help"
)){
logger
.
info
(
"-authkey=XXXXXXXX to use custom Session Auth Key, note that key is case sensitive"
)
logger
.
info
(
"-port=XXXXX to use custom listener port, default using 8080"
)
}
}
Application
(
applicationEngineEnvironment
{}).
apply
{
mirai
()
}
}
@UseExperimental
(
ExperimentalUnsignedTypes
::
class
)
fun
Application
.
mirai
()
{
install
(
DefaultHeaders
)
install
(
CallLogging
)
routing
{
mirai
(
"/sendFriendMessage"
)
{
// TODO: 2019/11/21 解析图片消息等为 Message
Bot
.
instanceWhose
(
qq
=
param
(
"bot"
)).
getFriend
(
param
(
"qq"
)).
sendMessage
(
param
<
String
>(
"message"
))
call
.
ok
()
}
mirai
(
"/sendGroupMessage"
)
{
Bot
.
instanceWhose
(
qq
=
param
(
"bot"
)).
getGroup
(
param
<
Long
>(
"group"
)).
sendMessage
(
param
<
String
>(
"message"
))
call
.
ok
()
}
mirai
(
"/event/message"
)
{
// TODO: 2019/11/21
Bot
.
instanceWhose
(
qq
=
param
(
"bot"
))
}
mirai
(
"/addFriend"
)
{
Bot
.
instanceWhose
(
qq
=
param
(
"bot"
)).
addFriend
(
id
=
param
(
"qq"
),
message
=
paramOrNull
(
"message"
),
remark
=
paramOrNull
(
"remark"
)
)
call
.
ok
()
}
}
}
@ContextDsl
private
fun
Route
.
mirai
(
path
:
String
,
body
:
PipelineInterceptor
<
Unit
,
ApplicationCall
>):
Route
{
return
route
(
path
,
HttpMethod
.
Get
)
{
handle
{
try
{
this
.
body
(
this
.
subject
)
}
catch
(
e
:
IllegalAccessException
)
{
call
.
respond
(
HttpStatusCode
.
BadRequest
,
e
.
message
)
}
}
}
}
suspend
inline
fun
ApplicationCall
.
ok
()
=
this
.
respond
(
HttpStatusCode
.
OK
,
"OK"
)
/**
* 错误请求. 抛出这个异常后将会返回错误给一个请求
*/
@Suppress
(
"unused"
)
open
class
IllegalAccessException
:
Exception
{
override
val
message
:
String
get
()
=
super
.
message
!!
constructor
(
message
:
String
)
:
super
(
message
,
null
)
constructor
(
cause
:
Throwable
)
:
super
(
cause
.
toString
(),
cause
)
constructor
(
message
:
String
,
cause
:
Throwable
?)
:
super
(
message
,
cause
)
}
/**
* 错误参数
*/
class
IllegalParamException
(
message
:
String
)
:
IllegalAccessException
(
message
)
fun
PipelineContext
<
Unit
,
ApplicationCall
>.
illegalParam
(
expectingType
:
String
?,
paramName
:
String
,
actualValue
:
String
?
=
call
.
parameters
[
paramName
]
):
Nothing
=
throw
IllegalParamException
(
"Illegal param. A $expectingType is required for `$paramName` while `$actualValue` is given"
)
@Suppress
(
"IMPLICIT_CAST_TO_ANY"
)
@UseExperimental
(
ExperimentalUnsignedTypes
::
class
)
private
inline
fun
<
reified
R
>
PipelineContext
<
Unit
,
ApplicationCall
>.
param
(
name
:
String
):
R
=
this
.
paramOrNull
(
name
)
?:
illegalParam
(
R
::
class
.
simpleName
,
name
)
@Suppress
(
"IMPLICIT_CAST_TO_ANY"
)
@UseExperimental
(
ExperimentalUnsignedTypes
::
class
)
private
inline
fun
<
reified
R
>
PipelineContext
<
Unit
,
ApplicationCall
>.
paramOrNull
(
name
:
String
):
R
?
=
when
(
R
::
class
)
{
Byte
::
class
-> call.parameters[name]?.to
Byte
()
Int
::
class
-> call.parameters[name]?.to
Int
()
Short
::
class
-> call.parameters[name]?.to
Short
()
Float
::
class
-> call.parameters[name]?.to
Float
()
Long
::
class
-> call.parameters[name]?.to
Long
()
Double
::
class
-> call.parameters[name]?.to
Double
()
Boolean
::
class
-> when (call.parameters[name]) {
"true" -> true
"false" -> false
"0" -> false
"1" -> true
null -> null
else -> illegal
Param
(
"boolean"
,
name
)
}
String
::
class
-> call.parameters[name]
UByte
::
class
-> call.parameters[name]?.to
UByte
()
UInt
::
class
-> call.parameters[name]?.to
UInt
()
UShort
::
class
-> call.parameters[name]?.to
UShort
()
UByteArray
::
class
-> call.parameters[name]?.hex
ToUBytes
()
ByteArray
::
class
-> call.parameters[name]?.hex
ToBytes
()
else
->
error
(
name
::
class
.
simpleName
+
" is not supported"
)
}
as
R
?
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/Session.kt
View file @
800b5cba
...
@@ -102,7 +102,7 @@ class AuthedSession internal constructor(val bot: Bot, coroutineContext: Corouti
...
@@ -102,7 +102,7 @@ class AuthedSession internal constructor(val bot: Bot, coroutineContext: Corouti
init
{
init
{
bot
.
subscribeMessages
{
bot
.
subscribeMessages
{
_listener
=
always
{
this
.
run
(
messageQueue
::
add
)
}
_listener
=
always
{
this
.
run
(
messageQueue
::
add
)
}
// this aka messagePacket
}
}
}
}
...
...
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/dto/VerifyDTO.kt
View file @
800b5cba
...
@@ -22,7 +22,8 @@ sealed class StateCodeDTO(val code: Int, var msg: String) : DTO {
...
@@ -22,7 +22,8 @@ sealed class StateCodeDTO(val code: Int, var msg: String) : DTO {
// object AUTH_WRONG : CodeDTO(1) // AuthKey错误, @see AuthResDTO
// object AUTH_WRONG : CodeDTO(1) // AuthKey错误, @see AuthResDTO
object
NoBot
:
StateCodeDTO
(
2
,
"指定Bot不存在"
)
object
NoBot
:
StateCodeDTO
(
2
,
"指定Bot不存在"
)
object
IllegalSession
:
StateCodeDTO
(
3
,
"Session失效或不存在"
)
object
IllegalSession
:
StateCodeDTO
(
3
,
"Session失效或不存在"
)
object
NotVerifySession
:
StateCodeDTO
(
3
,
"Session未认证"
)
object
NotVerifySession
:
StateCodeDTO
(
4
,
"Session未认证"
)
object
NoElement
:
StateCodeDTO
(
5
,
"指定对象不存在"
)
// KS bug: 主构造器中不能有非字段参数 https://github.com/Kotlin/kotlinx.serialization/issues/575
// KS bug: 主构造器中不能有非字段参数 https://github.com/Kotlin/kotlinx.serialization/issues/575
@Serializable
@Serializable
...
...
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/BaseRoute.kt
View file @
800b5cba
...
@@ -108,6 +108,8 @@ internal inline fun Route.intercept(crossinline blk: suspend PipelineContext<Uni
...
@@ -108,6 +108,8 @@ internal inline fun Route.intercept(crossinline blk: suspend PipelineContext<Uni
call
.
respondDTO
(
StateCodeDTO
.
IllegalSession
)
call
.
respondDTO
(
StateCodeDTO
.
IllegalSession
)
}
catch
(
e
:
NotVerifiedSessionException
)
{
}
catch
(
e
:
NotVerifiedSessionException
)
{
call
.
respondDTO
(
StateCodeDTO
.
NotVerifySession
)
call
.
respondDTO
(
StateCodeDTO
.
NotVerifySession
)
}
catch
(
e
:
NoSuchElementException
)
{
call
.
respondDTO
(
StateCodeDTO
.
NoElement
)
}
catch
(
e
:
IllegalAccessException
)
{
}
catch
(
e
:
IllegalAccessException
)
{
call
.
respondDTO
(
StateCodeDTO
.
IllegalAccess
(
e
.
message
),
HttpStatusCode
.
BadRequest
)
call
.
respondDTO
(
StateCodeDTO
.
IllegalAccess
(
e
.
message
),
HttpStatusCode
.
BadRequest
)
}
}
...
...
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt
View file @
800b5cba
...
@@ -17,7 +17,7 @@ fun Application.messageModule() {
...
@@ -17,7 +17,7 @@ fun Application.messageModule() {
}
}
miraiVerify
<
SendDTO
>(
"/sendFriendMessage"
)
{
miraiVerify
<
SendDTO
>(
"/sendFriendMessage"
)
{
it
.
session
.
bot
.
get
QQ
(
it
.
target
).
sendMessage
(
it
.
messageChain
.
toMessageChain
())
it
.
session
.
bot
.
get
Friend
(
it
.
target
).
sendMessage
(
it
.
messageChain
.
toMessageChain
())
call
.
respondDTO
(
StateCodeDTO
.
Success
)
call
.
respondDTO
(
StateCodeDTO
.
Success
)
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment