Skip to content

Commit

Permalink
v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Summer committed Jul 1, 2021
1 parent 1e3105c commit 4f8d5cf
Show file tree
Hide file tree
Showing 15 changed files with 1,292 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/WhiteDew.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,42 @@
# WhiteDew
Go语言编写的自用QQ机器人,基于 go-cqhttp
自用 QQ 机器人框架,99% 兼容 [OneBot v11](https://github.com/botuniverse/onebot).

## 特点
* 插件化应用管理
* 支持对话状态管理
* 简单易上手
* ...

## 快速开始

```go
package main

import (
"github.com/m1dsummer/whitedew"
"log"
)

type PluginPing struct {}

func (p PluginPing)Init(w *whitedew.WhiteDew) {
w.SetActionHandler("/ping", Callback)
}

func Callback(session *whitedew.Session) {
session.PostPrivateMessage(session.Sender.GetId(), "pong!")
}

func main() {
w := whitedew.New()
w.SetCQServer("http://localhost:60001")
w.AddPlugin(PluginPing{})
w.Run("/event", 60000)
}
```

上面的代码创建了一个叫 PluginPing 的插件,当打开私聊窗口向 Bot 发送 `/ping` 时, Bot 会回复 `pong!`

框架本身并不包含 QQ 消息收发功能的实现,需要配合其他 cqhttp 框架使用,比如[go-cqhttp]()

在这个例子中,WhiteDew 监听地址 `http://localhost:60000`,cq-http 监听在本地 60001 端口并将消息和事件上报给 WhiteDew, 由 PluginPing 插件对包含 `/ping` 字段的消息进行处理。
310 changes: 310 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,310 @@
package whitedew

import (
"encoding/json"
"fmt"
"github.com/parnurzeal/gorequest"
"log"
)

type Agent struct {
URL string
}

func NewAgent(URL string) *Agent {
return &Agent{URL: URL}
}

type MessageTemplate struct {
Action string `json:"action"`
Params map[string]interface{} `json:"params"`
Echo string `json:"echo"`
}

func (a *Agent) NewPostMessage(action string, params map[string]interface{}) *MessageTemplate {
return &MessageTemplate{Action: action, Params: params, Echo: "success"}
}

func (a *Agent) PostMessage(action string, param map[string]interface{}, autoEscape ...bool) []byte {
length := len(autoEscape)
switch length {
case 1:
param["auto_escape"] = autoEscape[0]
case 0:
param["auto_escape"] = false
default:
panic("too many arguments")
}
uri := fmt.Sprintf("%s/%s", a.URL, action)
data, _ := json.Marshal(param)
_, body, errs := gorequest.New().Post(uri).Set("Content-Type", "application/json").Send(string(data)).EndBytes()
if errs != nil {
log.Fatalln(errs)
}
return body
}

func (a *Agent) PostPrivateMessage(receiver int64, msg string) {
param := map[string]interface{}{
"user_id": receiver,
"message": msg,
}
a.PostMessage("send_private_msg", param)
}

func (a *Agent) PostGroupMessage(receiver int64, msg string) {
param := map[string]interface{}{
"group_id": receiver,
"message": msg,
}
a.PostMessage("send_group_msg", param)
}

func (a *Agent) DeleteMsg(mid int64) {
param := map[string]interface{}{
"message_id": mid,
}
a.PostMessage("delete_msg", param)
}

func (a *Agent) GetMessage(mid int64) {
param := map[string]interface{}{
"message_id": mid,
}
a.PostMessage("get_msg", param)
}

func (a *Agent) GetForwardMsg(mid int64) {
param := map[string]interface{}{
"message_id": mid,
}
a.PostMessage("get_forward_msg", param)

}

func (a *Agent) SendLike(uid int64, times int64) {
param := map[string]interface{}{
"user_id": uid,
"times": times,
}
a.PostMessage("get_forward_msg", param)
}

func (a *Agent) SetGroupKick(gid, uid int64, reject bool) {
param := map[string]interface{}{
"group_id": gid,
"user_id": uid,
"reject_add_request": reject,
}
a.PostMessage("set_group_kick", param)
}

func (a *Agent) SetGroupBan(gid, uid, time int64) {
param := map[string]interface{}{
"group_id": gid,
"user_id": uid,
"duration": time,
}
a.PostMessage("set_group_ban", param)
}

func (a *Agent) SetGroupAnonymousBan(gid, duration int64, anonymous AnonymousUser, flag string) {
param := map[string]interface{}{
"group_id": gid,
"anonymous": anonymous,
"duration": duration,
"anonymous_flag": flag,
}
a.PostMessage("set_group_anonymous_ban", param)
}

func (a *Agent) SetGroupWholeBan(gid int64, enable bool) {
param := map[string]interface{}{
"group_id": gid,
"enable": enable,
}
a.PostMessage("set_group_whole_ban", param)
}

func (a *Agent) SetGroupAdmin(gid, uid int64, enable bool) {
param := map[string]interface{}{
"group_id": gid,
"user_id": uid,
"enable": enable,
}
a.PostMessage("set_group_admin", param)
}

func (a *Agent) SetGroupAnonymous(gid int64, enable bool) {
param := map[string]interface{}{
"group_id": gid,
"enable": enable,
}
a.PostMessage("set_group_anonymous", param)
}

func (a *Agent) SetGroupCard(gid, uid int64, card string) {
param := map[string]interface{}{
"group_id": gid,
"user_id": uid,
"card": card,
}
a.PostMessage("set_group_card", param)
}

func (a *Agent) SetGroupName(gid int64, name string) {
param := map[string]interface{}{
"group_id": gid,
"group_name": name,
}
a.PostMessage("set_group_name", param)
}

func (a *Agent) SetGroupLeave(gid int64, dismiss bool) {
param := map[string]interface{}{
"group_id": gid,
"is_dismiss": dismiss,
}
a.PostMessage("set_group_leave", param)
}

func (a *Agent) SetGroupSpecialTitle(gid, uid int64, title string, duration int64) {
param := map[string]interface{}{
"group_id": gid,
"user_id": uid,
"special_title": title,
"duration": duration,
}
a.PostMessage("set_group_special_title", param)
}

func (a *Agent) SetFriendAddRequest(flag string, approve bool, remark string) {
param := map[string]interface{}{
"flag": flag,
"approve": approve,
"remark": remark,
}
a.PostMessage("set_friend_add_request", param)
}

func (a *Agent) SetGroupAddRequest(flag string, _type string, approve bool, reason string) {
param := map[string]interface{}{
"flag": flag,
"type": _type,
"approve": approve,
"reason": reason,
}
a.PostMessage("set_group_add_request", param)
}

func (a *Agent) GetLoginInfo() {
a.PostMessage("get_login_info", nil)
}

func (a *Agent) GetStrangerInfo(uid int64, noCache bool) {
param := map[string]interface{}{
"user_id": uid,
"no_cache": noCache,
}
a.PostMessage("get_stranger_info", param)
}

func (a *Agent) GetFriendList() {
a.PostMessage("get_friend_list", nil)
}

func (a *Agent) GetGroupInfo(gid int64, noCache bool) {
param := map[string]interface{}{
"group_id": gid,
"no_cache": noCache,
}
a.PostMessage("get_group_info", param)
}

func (a *Agent) GetGroupList() {
a.PostMessage("get_group_list", nil)
}

func (a *Agent) GetGroupMemberInfo(gid int64, uid int64, noCache bool) {
param := map[string]interface{}{
"group_id": gid,
"user_id": uid,
"no_cache": noCache,
}
a.PostMessage("get_group_member_info", param)
}

func (a *Agent) GetGroupMemberList(gid int64) {
param := map[string]interface{}{
"group_id": gid,
}
a.PostMessage("get_group_member_list", param)
}

func (a *Agent) GetGroupHonorInfo(gid int64, _type string) {
param := map[string]interface{}{
"group_id": gid,
"type": _type,
}
a.PostMessage("get_group_honor_info", param)
}

func (a *Agent) GetCookies(domain string) {
param := map[string]interface{}{
"domain": domain,
}
a.PostMessage("get_cookies", param)
}

func (a *Agent) GetCsrfToken() {
a.PostMessage("get_csrf_token", nil)
}

func (a *Agent) GetCredentials(domain string) {
param := map[string]interface{}{
"domain": domain,
}
a.PostMessage("get_credentials", param)
}

func (a *Agent) GetRecord(file string, outFormat string) {
param := map[string]interface{}{
"file": file,
"out_format": outFormat,
}
a.PostMessage("get_record", param)
}

func (a *Agent) GetImage(file string) {
param := map[string]interface{}{
"file": file,
}
a.PostMessage("get_image", param)
}

func (a *Agent) CanSendImage() {
a.PostMessage("can_send_image", nil)
}

func (a *Agent) CanSendRecord() {
a.PostMessage("can_send_record", nil)
}

func (a *Agent) GetStatus() {
a.PostMessage("get_status", nil)
}

func (a *Agent) GetVersionInfo() {
a.PostMessage("get_version_info", nil)
}

func (a *Agent) SetRestart() {
param := map[string]interface{}{
"delay": 0,
}
a.PostMessage("set_restart", param)
}

func (a *Agent) CleanCache() {
a.PostMessage("clean_cache", nil)
}
Loading

0 comments on commit 4f8d5cf

Please sign in to comment.