-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a783e68
Showing
7 changed files
with
676 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
config.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# node-tuling-bot | ||
|
||
一个由 Node.js 编写的小程序,通过调用 [图灵机器人](http://www.turingapi.com/) API 来让其谈笑风生 | ||
|
||
本项目在 [go-cqhttp](https://github.com/Mrs4s/go-cqhttp) 环境下调试,两者搭配食用更佳~ | ||
|
||
## 使用方法 | ||
|
||
1. 克隆本项目 `git clone https://github.com/Talaxy009/node-tuling-bot.git` | ||
|
||
2. 移动到本地仓库 `cd ./node-tuling-bot` | ||
|
||
3. 安装依赖 `npm install` | ||
|
||
4. 复制一份 `config.default.json` 修改名字为 `config.json` 后对其进行编辑,设置文件相关说明在此->[点我](https://github.com/Talaxy009/node-tuling-bot/docs/config.md) | ||
|
||
5. 启动 `npm run start` | ||
|
||
## TODO 🕊 | ||
|
||
- 实装单次QQ限制调用次数功能 | ||
|
||
- 支持更多的图灵机器人功能 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"cqws": { | ||
"host": "127.0.0.1", | ||
"port": 6700, | ||
"enableAPI": true, | ||
"enableEvent": true, | ||
"accessToken": "", | ||
"reconnection": true, | ||
"reconnectionAttempts": 10, | ||
"reconnectionDelay": 5000 | ||
}, | ||
"bot": { | ||
"admin": -1, | ||
"Apikey": "", | ||
"PerQQLimit": 250, | ||
"needAt": true, | ||
"greet": "哈喽,我准备聊天了!" | ||
}, | ||
"message": { | ||
"reqType": 0, | ||
"perception": { | ||
"inputText": { | ||
"text": "" | ||
} | ||
}, | ||
"userInfo": { | ||
"apiKey": "", | ||
"groupId":"", | ||
"userId": "" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# 设置文件介绍 | ||
|
||
一个默认的设置文件应该如下 | ||
|
||
``` json | ||
{ | ||
"cqws": { | ||
"host": "127.0.0.1", | ||
"port": 6700, | ||
"enableAPI": true, | ||
"enableEvent": true, | ||
"accessToken": "", | ||
"reconnection": true, | ||
"reconnectionAttempts": 10, | ||
"reconnectionDelay": 5000 | ||
}, | ||
"bot": { | ||
"admin": -1, | ||
"Apikey": "", | ||
"PerQQLimit": 250, | ||
"needAt": true, | ||
"greet": "哈喽,我准备聊天了!" | ||
}, | ||
"message": { | ||
"reqType": 0, | ||
"perception": { | ||
"inputText": { | ||
"text": "" | ||
} | ||
}, | ||
"userInfo": { | ||
"apiKey": "", | ||
"groupId":"", | ||
"userId": "" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## cqws 分支(连接相关,一般不需要修改) | ||
|
||
- host——websocket 服务器地址 | ||
|
||
- port——websocket 服务器端口 | ||
|
||
- enableAPI——启用 API | ||
|
||
- enableEvent——启用 EVENT | ||
|
||
- accessToken——连接令牌 | ||
|
||
- reconnection——是否重连 | ||
|
||
- reconnectionAttempts——尝试重连次数 | ||
|
||
- reconnectionDelay——每次重连尝试间隔 | ||
|
||
## bot 分支(机器人设置相关) | ||
|
||
- admin——管理员 QQ(一般为你的 QQ,机器人连接成功后会发送消息到此 QQ 上) | ||
|
||
- ApiKey——你在图灵机器人官网申请的 API | ||
|
||
- PerQQLimit——每个 QQ 每天最高聊天次数限制(待实装) | ||
|
||
- needAt——触发机器人聊天是否需要 @ | ||
|
||
- greet——机器人连接成功后向管理员发送的消息 | ||
|
||
## message 分支 | ||
|
||
不要修改,这是机器人发送请求所用的格式,我只是省力保存在这了 ( = v =)b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
const { default: Axios } = require('axios'); | ||
const { CQWebSocket } = require('cq-websocket'); | ||
const config = require('./config.json'); | ||
const bot = new CQWebSocket(config.cqws); | ||
var url = "http://openapi.tuling123.com/openapi/api/v2"; | ||
var Msg = config.message; | ||
Msg.userInfo.apiKey = config.bot.Apikey; | ||
|
||
|
||
//连接 | ||
bot.connect(); | ||
|
||
bot.on('socket.connecting', function (wsType, attempts) { | ||
console.log(new Date().toLocaleString() + ' 尝试第%d次连接[%s]中···', attempts, wsType,) | ||
}).on('socket.connect', function (wsType, sock, attempts) { | ||
console.log(new Date().toLocaleString() + ' 第%d次连接[%s]成功! ( = v =)b', attempts, wsType); | ||
if (config.bot.admin > 0 && wsType === '/api') { | ||
setTimeout(() => { | ||
bot('send_private_msg', { | ||
user_id: config.bot.admin, | ||
message: `${config.bot.greet}` | ||
}); | ||
}, 1000) | ||
} | ||
}).on('socket.failed', function (wsType, attempts) { | ||
console.log(new Date().toLocaleString() + ' 第%d次连接失败[%s]', attempts, wsType) | ||
}) | ||
|
||
//设置监听 | ||
if (config.bot.needAt) { | ||
bot.on("message.group.@.me", GroupMsg); | ||
bot.on("message.discuss.@.me", DiscussMsg); | ||
bot.on("message.private", PrivateMsg); | ||
} else { | ||
bot.on("message.group", GroupMsg); | ||
bot.on("message.discuss", DiscussMsg); | ||
bot.on("message.private", PrivateMsg); | ||
}; | ||
|
||
/** | ||
* 接受群消息 | ||
*/ | ||
function GroupMsg(e, context) { | ||
let group = context.group_id; | ||
let message = context.message; | ||
let user = context.user_id; | ||
|
||
e.stopPropagation(); | ||
reply(group, user, message, 0); | ||
}; | ||
|
||
/** | ||
* 接受讨论组消息 | ||
*/ | ||
function DiscussMsg(e, context) { | ||
let discuss = context.discuss_id; | ||
let message = context.message; | ||
let user = context.user_id; | ||
|
||
e.stopPropagation(); | ||
reply(discuss, user, message, 1); | ||
}; | ||
|
||
/** | ||
* 接受私聊消息 | ||
*/ | ||
function PrivateMsg(e, context) { | ||
let user = context.user_id; | ||
let message = context.message; | ||
|
||
e.stopPropagation(); | ||
reply(null, user, message, 2); | ||
}; | ||
|
||
/** | ||
* 发送消息&获取回复&发送回复 | ||
* @param gd 群/讨论组ID | ||
* @param id 消息发送者ID | ||
* @param msg 发送消息 | ||
* @param type 消息类型 | ||
*/ | ||
function reply(gd, id, msg, type) { | ||
Msg.perception.inputText.text = msg; | ||
Msg.userInfo.userId = id; | ||
Msg.userInfo.groupId = gd; | ||
Axios.post(url, Msg | ||
).then(function Send(response) { | ||
var GotMsg = response.data.results[0].values.text; | ||
var SendMsg = null; | ||
switch (type) { | ||
case 0: | ||
SendMsg = `[CQ:at,qq=${id}]${GotMsg}`; | ||
bot('send_group_msg', { | ||
group_id: gd, | ||
message: SendMsg, | ||
}); | ||
console.log(`${new Date().toLocaleString()} 回复${gd}群, ${id}者:`); | ||
break; | ||
|
||
case 1: | ||
SendMsg = `[CQ:at,qq=${id}]${GotMsg}`; | ||
bot('send_discuss_msg', { | ||
discuss_id: gd, | ||
message: SendMsg, | ||
}); | ||
console.log(`${new Date().toLocaleString()} 回复${gd}讨论组, ${id}者:`); | ||
break; | ||
|
||
default: | ||
SendMsg = `${GotMsg}`; | ||
bot('send_private_msg', { | ||
user_id: id, | ||
message: SendMsg, | ||
}); | ||
console.log(`${new Date().toLocaleString()} 回复私聊${id}者:`); | ||
break; | ||
}; | ||
console.log(GotMsg); | ||
}).catch(function (error) { | ||
console.log(error); | ||
}); | ||
}; |
Oops, something went wrong.