Skip to content

Commit

Permalink
🎉First commit: Shigure was born!🥳
Browse files Browse the repository at this point in the history
In this release, we've released the first version v0.0.1 of the Shigure-Bot project as a preview. To be honest, we're still at a very early stage now. But I believe that my passion can last at least for a while...

Change log:

- Introduce basic bot framework
- Add partial support for OneBot-V11 API
- Add a simple bot that compatible with OneBot-V11 backend
- Add an example for using OneBot-V11 bot
  • Loading branch information
arttnba3 committed Feb 20, 2025
0 parents commit 4bcaf55
Show file tree
Hide file tree
Showing 19 changed files with 1,140 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# JetBrains IDE
.idea/

# Vscode
.vscode/
339 changes: 339 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 時雨 (Shigure)

Yet another chatbot SDK that satisfies multiple chatbot backend specifications.

## Usage

> TBD
## Supported bot specifications

### [OneBot V11](https://github.com/botuniverse/onebot-11/)

Currently, we support part of OneBot V11 API, which can be known by examining the source code.

For the connection, we support following:

- [HTTP](https://github.com/botuniverse/onebot-11/blob/master/communication/http.md)
- [HTTP-Post](https://github.com/botuniverse/onebot-11/blob/master/communication/http-post.md)

To configure a Shigure-Bot for an OneBot backend, we need to provide the configuration in following format(if one of which was not configured, it won't be invoked):

```json
{
"http_post": {
"host": "example.com",
"port": 11451
},
"http_server": {
"port": 19198
}
}
```

You can refer to [example/onebot-v11.go](example/onebot-v11.go) for an example usage.

## Author

arttnba3 <[email protected]>

## License

GPL V2
83 changes: 83 additions & 0 deletions api/onebot/v11/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// OneBot V11 API
//
// Refer to following link for detailed docs:
//
// https://github.com/botuniverse/onebot-11/blob/master/api/public.md
// https://github.com/botuniverse/onebot-11/blob/master/api/hidden.md
//

package onebot_v11_api

type V11BotAPI interface {
SendPrivateMsg(userId int64, message interface{}, autoEscape bool) (int32, error)
SendGroupMsg(groupId int64, message interface{}, autoEscape bool) (int32, error)
SendMsg(messageType string, userId int64, groupId int64, message interface{}, autoEscape bool) (int32, error)
DeleteMsg(messageID int32) error
GetMsg(messageID int32) (interface{}, error)
// TODO: implement more API
}

type BotAction struct {
Action string `json:"action"`
Params interface{} `json:"params"`
UUID string `json:"uuid"`
}

// send_private_msg

type SendPrivateMsgReq struct {
UserID int64 `json:"user_id"`
Message interface{} `json:"message"`
AutoEscape bool `json:"auto_escape"`
}

type SendPrivateMsgResp struct {
MessageID int32 `json:"message_id"`
}

// send_group_msg

type SendGroupMsgReq struct {
GroupID int64 `json:"group_id"`
Message interface{} `json:"message"`
AutoEscape bool `json:"auto_escape"`
}

type SendGroupMsgResp struct {
MessageID int32 `json:"message_id"`
}

type SendMsgReq struct {
MessageType string `json:"message_type"`
UserID int64 `json:"user_id"`
GroupID int64 `json:"group_id"`
Message interface{} `json:"message"`
AutoEscape bool `json:"auto_escape"`
}

type SendMsgResp struct {
MessageID int32 `json:"message_id"`
}

type DeleteMsgReq struct {
MessageID int32 `json:"message_id"`
}

// DeleteMsgResp : Only a placeholder here
type DeleteMsgResp struct {
}

type GetMsgReq struct {
MessageID int32 `json:"message_id"`
}
type GetMsgResp struct {
Time int32 `json:"time"`
MessageType string `json:"message_type"`
MessageID int32 `json:"message_id"`
RealID int32 `json:"real_id"`
Sender interface{} `json:"sender"`
Message interface{} `json:"message"`
}

// TODO: implement more APIs
35 changes: 35 additions & 0 deletions api/onebot/v11/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Event base and sub-events
//
// Refer to following link for detailed docs:
//
// https://github.com/botuniverse/onebot-11/blob/master/event/README.md
// https://github.com/botuniverse/onebot-11/blob/master/event/meta.md
//

package onebot_v11_api

// EventBase
// This struct ONLY indicates what an event should have
type EventBase struct {
Time int64 `json:"time"`
SelfId int64 `json:"self_id"`
PostType string `json:"post_type"`
}

type LifeCycle struct {
Time int64 `json:"time"`
SelfId int64 `json:"self_id"`
PostType string `json:"post_type"`
MetaEventType string `json:"meta_event_type"`
SubType string `json:"sub_type"`
}

type HeartBeat struct {
Time int64 `json:"time"`
SelfId int64 `json:"self_id"`
PostType string `json:"post_type"`
MetaEventType string `json:"meta_event_type"`
Status interface{} `json:"status"`
Interval int64 `json:"interval"`
}
15 changes: 15 additions & 0 deletions api/onebot/v11/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// HTTP API
//
// Refer to following link for detailed docs:
//
// https://github.com/botuniverse/onebot-11/blob/master/communication/http.md
//

package onebot_v11_api

type HTTPResponseBody struct {
Status string `json:"status"`
RetCode int `json:"retcode"`
Data interface{} `json:"data"`
}
63 changes: 63 additions & 0 deletions api/onebot/v11/message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// Message Event
//
// Refer to following link for detailed docs:
//
// https://github.com/botuniverse/onebot-11/blob/master/event/message.md
//

package onebot_v11_api

type PrivateMessageSender struct {
UserId int64 `json:"user_id"`
Nickname string `json:"nickname"`
Sex string `json:"sex"`
Age int32 `json:"age"`
}

type GroupMessageSender struct {
UserId int64 `json:"user_id"`
Nickname string `json:"nickname"`
Card string `json:"card"`
Sex string `json:"sex"`
Age int32 `json:"age"`
Area string `json:"area"`
Level string `json:"level"`
Role string `json:"role"`
Title string `json:"title"`
}

type PrivateMessage struct {
Time int64 `json:"time"`
SelfId int64 `json:"self_id"`
PostType string `json:"post_type"`
MessageType string `json:"message_type"`
SubType string `json:"sub_type"`
MessageId int32 `json:"message_id"`
UserId int64 `json:"user_id"`
Message interface{} `json:"message"`
RawMessage string `json:"raw_message"`
Font int32 `json:"font"`
Sender PrivateMessageSender `json:"sender"`
}

type GroupMessage struct {
Time int64 `json:"time"`
SelfId int64 `json:"self_id"`
PostType string `json:"post_type"`
MessageType string `json:"message_type"`
SubType string `json:"sub_type"`
MessageId int32 `json:"message_id"`
GroupId int64 `json:"group_id"`
UserId int64 `json:"user_id"`
Anonymous interface{} `json:"anonymous"`
Message interface{} `json:"message"`
RawMessage string `json:"raw_message"`
Font int32 `json:"font"`
Sender GroupMessageSender `json:"sender"`
}

type MessageSegment struct {
Type string `json:"type"`
Data interface{} `json:"data"`
}
11 changes: 11 additions & 0 deletions api/onebot/v11/notice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// Notice Event
//
// Refer to following link for detailed docs:
//
// https://github.com/botuniverse/onebot-11/blob/master/event/notice.md
//

package onebot_v11_api

// TODO: complete this
4 changes: 4 additions & 0 deletions api/onebot/v11/receiver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package onebot_v11_api

type V11ReceiverAPI interface {
}
31 changes: 31 additions & 0 deletions api/onebot/v11/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Request Event
//
// Refer to following link for detailed docs:
//
// https://github.com/botuniverse/onebot-11/blob/master/event/request.md
//

package onebot_v11_api

type FriendAddRequest struct {
Time int64 `json:"time"`
SelfId int64 `json:"self_id"`
PostType string `json:"post_type"`
RequestType string `json:"request_type"`
UserId int64 `json:"user_id"`
Comment string `json:"comment"`
Flag string `json:"flag"`
}

type GroupAddRequest struct {
Time int64 `json:"time"`
SelfId int64 `json:"self_id"`
PostType string `json:"post_type"`
RequestType string `json:"request_type"`
SubType string `json:"sub_type"`
GroupId int64 `json:"group_id"`
UserId int64 `json:"user_id"`
Comment string `json:"comment"`
Flag string `json:"flag"`
}
7 changes: 7 additions & 0 deletions api/onebot/v11/sender.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package onebot_v11_api

type V11SenderAPI interface {
SendRequest(request BotAction) error
GetRequestResult(uuid string) ([]byte, error)
SendRequestAndGetResult(action string, params interface{}) ([]byte, error)
}
28 changes: 28 additions & 0 deletions bot/bot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package shigure

import (
"errors"
"github.com/arttnba3/Shigure-Bot/bot/onebot/v11"
)

type ShigureBot struct {
Bot interface{}
handlers map[string]func(...any)
}

func NewShigureBot(botType string, configJson []byte, logger func(...any), handler func(rawData []byte)) (*ShigureBot, error) {
switch botType {
case "OneBot-V11":
bot, err := onebot_v11_impl.NewV11Bot(configJson, logger, handler) // TODO: switch from handler to handlers table
if err != nil {
return nil, err
}

return &ShigureBot{
Bot: bot,
handlers: nil,
}, nil
default:
return nil, errors.New("unknown bot type [" + botType + "]")
}
}
Loading

0 comments on commit 4bcaf55

Please sign in to comment.