Skip to content

Commit

Permalink
Merge pull request #10 from dingdinglz/dev
Browse files Browse the repository at this point in the history
docs done
  • Loading branch information
dingdinglz authored Nov 22, 2024
2 parents 691211e + 77a8ff3 commit 923e883
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 34 deletions.
127 changes: 126 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,126 @@
# dingbot
## 简述

本项目实现了一个qq机器人框架,本体支持了部分功能如关键词回复功能,并开放了一个由**blockly**驱动的插件系统,这意味着你可以采用拖拼图的方式进行插件的开发,对新手和想开发自己机器人插件的小白friendly。例如

![](index.png)

详细介绍加图片请移步:[dingbot v2](https://dingdinglz.github.io/p/dingbot-v2/)

## 实现功能

- [x] 扫码登录及后续自动登录

- [x] 关键词回复

- [x] 违禁词系统

- [x] 插件系统(blockly + lua)

## 插件系统

### 支持事件

- [x] 群消息事件

- [x] 私聊消息事件

- [x] 插件信息事件

- [ ] 好友申请事件

### 支持接口

#### bot相关

- [x] 发送群消息

- [x] 发送私聊消息

- [ ] 群禁言

- [ ] 获取群成员信息

- [ ] 获取群列表

- [ ] 获取好友列表

#### 功能相关

- [ ] 键值对存储

- [ ] 插件设置界面支持

## 实现方法

golang + html

golang下对接[Lagrange](https://github.com/LagrangeDev/LagrangeGo),以[fiber](https://github.com/gofiber/fiber)框架作为web框架,html以[layui](https://layui.dev)构建前端界面

## 编译方法

### 前置条件

安装golang

### 编译方法

```bash
go build
```

### 产物

将所得的dingbot.exe(其他系统后缀可能不同)与web文件夹复制到新的文件夹中,此时的dingbot可以直接运行。

此时你的文件夹结构应该为

```
- dingbot[.exe]
- web
```

## 运行方法

打开dingbot可执行文件,第一次使用会为你打开初始化界面,完成设置后重启dingbot即可。

首先在账号设置栏中保存QQ账号与密码,点击保存。

点击下方登录进行登录,初次登录需要扫描二维码。后续登录如sig未失效可直接登录。

提示登录成功后机器人便进入正常运行状态。

### 关键词回复

点击创建可以创建新的关键词。

精确识别为消息必须完美符合关键词才会发送回复内容。

非精确识别则如果消息包含关键词即发送回复内容。

### 群接收开关 && 好友接收开关

开启后机器人才会对对应群和好友的消息进行处理。否则dingbot自带功能与插件功能无法在对应的对话中生效。

### 插件

新建插件后即会为你创建一个新的插件源码,点击编辑后会进入插件编辑器中进行编辑。

一个完整的插件**至少**拥有插件信息提供事件,即事件中第一个,将其拖至编辑器中,用文本填充相关信息即可。

#### 事件

所有的事件的实现方法都拖至事件内部处理。

在对应的事件中可以使用对应的参数。

#### 保存

编辑完插件后记得保存,否则下次将丢失上次编辑。

#### 生成

插件编辑完后无法立即进入工作状态,点击生成插件后才会生成正式的插件文件,若你可以在插件列表中看到该插件,即为生成成功,并进入工作状态。

#### 插件的获取

当然,除了自己编写插件以外,可以从社区下载他人制作完成的插件,复制到对应的文件夹中,若你可以在插件列表中看到该插件,即为添加成功。
41 changes: 41 additions & 0 deletions blockly_file/library.xml
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,45 @@
<field name="HUE">180</field>
</block>
</value>
</block><block type="factory_base" id="RZFR267,_6y6|}9#t;tk" deletable="false" movable="false" x="10" y="10">
<mutation connections="BOTH"></mutation>
<field name="NAME">dingbotPrint</field>
<field name="INLINE">AUTO</field>
<field name="CONNECTIONS">BOTH</field>
<statement name="INPUTS">
<block type="input_value" id="0MXXU/,T1$vupJ9Yg:^Q">
<field name="INPUTNAME">text</field>
<field name="ALIGN">LEFT</field>
<statement name="FIELDS">
<block type="field_static" id="QE]h@Z!*mtdE,]g7YB_v">
<field name="TEXT">Dingbot输出</field>
</block>
</statement>
<value name="TYPE">
<shadow type="type_null" id="F8nD64],zo.7{m{HO^g0"></shadow>
</value>
</block>
</statement>
<value name="TOOLTIP">
<block type="text" id=",gq?jYox}Mm0jLLPzj$C" deletable="false" movable="false">
<field name="TEXT">控制台输出</field>
</block>
</value>
<value name="HELPURL">
<block type="text" id="s:k~d_0FeJtW@jaEfk(I" deletable="false" movable="false">
<field name="TEXT"></field>
</block>
</value>
<value name="TOPTYPE">
<shadow type="type_null" id="]L,GtUv`EnI8q94hC-2="></shadow>
</value>
<value name="BOTTOMTYPE">
<shadow type="type_null" id="Opcd)qtics2t@cUm,qC}"></shadow>
</value>
<value name="COLOUR">
<block type="colour_hue" id="4:H]bF#3(J3Fv)axT6Q$">
<mutation colour="#5b67a5"></mutation>
<field name="HUE">230</field>
</block>
</value>
</block></xml>
17 changes: 1 addition & 16 deletions blockly_file/toolbox.xml
Original file line number Diff line number Diff line change
Expand Up @@ -227,22 +227,6 @@
</shadow>
</value>
</block>
<block type="text_print">
<value name="TEXT">
<shadow type="text">
<field name="TEXT">abc</field>
</shadow>
</value>
</block>
<block type="text_prompt_ext">
<mutation type="TEXT"></mutation>
<field name="TYPE">TEXT</field>
<value name="TEXT">
<shadow type="text">
<field name="TEXT">abc</field>
</shadow>
</value>
</block>
</category>
<category name="列表" colour="#745ba5">
<block type="lists_create_with">
Expand Down Expand Up @@ -327,6 +311,7 @@
<block type="useruin"></block>
</category>
<category name="功能函数" colour="#9fa55b">
<block type="dingbotprint"></block>
<block type="sendgroupmessage"></block>
<block type="sendprivatemessage"></block>
</category>
Expand Down
39 changes: 39 additions & 0 deletions bot/groupmessage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bot

import (
"fmt"
"io/fs"
"os"
"path/filepath"
Expand All @@ -24,6 +25,10 @@ func AddVMFuncs(Vm *otto.Otto) {
DingQQBot.SendPrivateMessage(tool.StringToUint32(call.Argument(0).String()), []message.IMessageElement{message.NewText(call.Argument(1).String())})
return otto.Value{}
})
Vm.Set("DingbotPrint", func(call otto.FunctionCall) otto.Value {
fmt.Println("[plugin] " + call.Argument(0).String())
return otto.Value{}
})
}

func BotGroupMessageEvent(client *client.QQClient, event *message.GroupMessage) {
Expand Down Expand Up @@ -69,7 +74,41 @@ func BotGroupMessageEvent(client *client.QQClient, event *message.GroupMessage)
if strings.Contains(messageText, i.Key) {
client.SendGroupMessage(event.GroupUin, []message.IMessageElement{message.NewGroupReply(event), message.NewText(i.Text)})
}
}
}
}

{
groupMembers, _ := DingQQBot.GetGroupMembersData(event.GroupUin)
if groupMembers[event.Sender.Uin].Permission == 0 {
// 发言者是普通成员
if groupMembers[client.Uin].Permission != 0 {
// 机器人是管理
}
} else {
// 发言者是管理
var ReplyFlag = false
var ReplySeq uint32
var ReplySender uint32
for _, i := range event.GetElements() {
if i.Type() == message.Reply {
ReplyFlag = true
ReplySeq = i.(*message.ReplyElement).ReplySeq
ReplySender = i.(*message.ReplyElement).SenderUin
}
if i.Type() == message.Text {
if strings.ReplaceAll(i.(*message.TextElement).Content, " ", "") == "撤回" {
if ReplyFlag {
if ReplySender == client.Uin {
client.RecallGroupMessage(event.GroupUin, ReplySeq)
} else {
if groupMembers[client.Uin].Permission > 0 && groupMembers[ReplySender].Permission == 0 {
client.RecallGroupMessage(event.GroupUin, ReplySeq)
}
}
}
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion database/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ func DatabaseInit() {
if err != nil {
tool.ErrorOut("database", "open error", err)
}
DB.AutoMigrate(&KeyWordTable{}, &OpenTable{})
DB.AutoMigrate(&KeyWordTable{}, &OpenTable{}, &StopTable{})
}
34 changes: 34 additions & 0 deletions database/stop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package database

import "strings"

func StopContain(sentence string) (bool, string) {
sentence = strings.ReplaceAll(sentence, " ", "")
sentence = strings.ReplaceAll(sentence, "\n", "")
sentence = strings.ReplaceAll(sentence, "*", "")
sentence = strings.ReplaceAll(sentence, "\\", "")
sentence = strings.ReplaceAll(sentence, "/", "")
sentence = strings.ReplaceAll(sentence, ".", "")
sentence = strings.ReplaceAll(sentence, ",", "")
sentence = strings.ReplaceAll(sentence, "。", "")
sentence = strings.ReplaceAll(sentence, ",", "")
var stops []StopTable
DB.Model(&StopTable{}).Find(&stops)
for _, i := range stops {
if strings.Contains(sentence, i.Text) {
return true, i.Text
}
}
return false, ""
}

func StopAdd(text string) bool {
b, _ := StopContain(text)
if b {
return false
}
var i StopTable
i.Text = text
DB.Create(&i)
return true
}
8 changes: 8 additions & 0 deletions database/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ type OpenTable struct {
func (OpenTable) TableName() string {
return "open"
}

type StopTable struct {
Text string
}

func (StopTable) TableName() string {
return "stop"
}
Binary file added index.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions route/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,12 @@ func DeletePluginRoute(c *fiber.Ctx) error {
os.Remove(filepath.Join(rootPath, "data", "plugin", c.Query("name")+".js"))
return JsonMessage(c, 0, "ok")
}

func DeleteSigRoute(c *fiber.Ctx) error {
rootPath, _ := os.Getwd()
os.Remove(filepath.Join(rootPath, "data", "sig.bin"))
if tool.FileIsExsits(filepath.Join(rootPath, "data", "qrcode.png")) {
os.Remove(filepath.Join(rootPath, "data", "qrcode.png"))
}
return JsonMessage(c, 0, "ok")
}
1 change: 1 addition & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func ServerCommonRun() {
apiDeleteRoute.Post("/open", route.DeleteOpenRoute)
apiDeleteRoute.Get("/plugin_source", route.DeletePluginSourceRoute)
apiDeleteRoute.Get("/plugin", route.DeletePluginRoute)
apiDeleteRoute.Get("/sig", route.DeleteSigRoute)

err := appconfig.MainServer.Listen("0.0.0.0:" + strconv.Itoa(appconfig.AppConfigVar.Port))
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions web/index/bot.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
}
});
}
function DeleteSig()
{
layui.$.get("/api/delete/sig",function(){
layui.layer.alert("清除成功!");
});
}
</script>
<div class="layui-card">
<div class="layui-card-header">QQ Bot Basical Setting</div>
Expand Down Expand Up @@ -77,6 +83,7 @@
<div class="layui-form-item">
<label class="layui-form-label"></label>
<button class="layui-btn layui-bg-blue" onclick="BotLogin()" id="login_btn">登录</button>
<button class="layui-btn layui-btn-danger" onclick="DeleteSig()">删除sig点击登录无反应时点我</button>
</div>
</div>
</div>
Loading

0 comments on commit 923e883

Please sign in to comment.