提交 903ac79a 编写于 作者: X xuelang532777032

Wed Nov 15 09:48:00 CST 2023 inscode

上级 82206c3f
import { WechatyBuilder, log } from 'wechaty'
// npm install wechaty-puppet-wechat
// set WECHATY_PUPPET=wechaty-puppet-wechat
process.on('uncaughtException', function(err) {
console.log('Caught exception: ' + err);
});
import {FileBox} from 'file-box'
import QrcodeTerminal from 'qrcode-terminal'
import QRcode from "qrcode"; //二维码
import axios from "axios"
const bot = WechatyBuilder.build()
//这是一个微信群聊机器人项目,也就是说你在群里发1,机器人会回复2,
//完整教程合集地址 https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzI1NjAxODkzMg==&action=getalbum&album_id=3102070220246171651&scene=173&from_msgid=2572037584&from_itemidx=1&count=3&nolastread=1&uin=&key=&devicetype=Windows+10+x64&version=6309071d&lang=zh_CN&ascene=0
//其实完整教程合集你学前10章就已经够用了!
//1、微信扫码(17年注册的微信可以直接扫,17年后注册的微信自己要绑定手机号和个人认证下,我记得还要开通收付款。)
//2、把自己的的机器人拉个群聊
//3、你发一个1,机器人会回复2
import {WechatyBuilder,} from 'wechaty'
import {PuppetMock,mock} from 'wechaty-puppet-mock';
const ajax = async (url, param, type) => {
return new Promise((resolve, reject) => {
var axiosOption = {
......@@ -26,42 +28,396 @@ const ajax = async (url, param, type) => {
})
})
}
bot.on('scan', (qrcode, status) => {
//这一步是 生成二维码
console.log("开发教程地址:https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzI1NjAxODkzMg==&action=getalbum&album_id=3102070220246171651&scene=173&from_msgid=2572037584&from_itemidx=1&count=3&nolastread=1&uin=&key=&devicetype=Windows+10+x64&version=6309071d&lang=zh_CN&ascene=0")
console.log("1、微信扫码(17年注册的微信可以直接扫,17年后注册的微信自己要绑定手机号和个人认证下,我记得还要开通收付款。)\n2、把自己的的机器人拉个群聊\n3、你发一个1,机器人会回复2")
QrcodeTerminal.generate(qrcode, {
small: true
})
})
.on('login', (user) => {
console.log("login")
console.log(user)
})
.on('logout', (msg) => {
console.log("logout:")
console.log(msg)
})
.on('message', (msg) => {
// 接收消息事件
let room = msg.room() // 获取这条消息是不是来自群聊
if (room) {
let topic = room.topic() // 获取群聊名称
}else{
return ;
}
const contact = msg.talker() // 获取这条消息发送人的信息
const contactName = contact.name() // 发送人的昵称
const text = msg.text() // 消息内容
console.log(text)
if(text=="1"){
room.say('2') //除了可以发 文字外,可以发图片、链接、语言、文件等具体看头部教程地址
}
debugger
// room.say('...') // 回复群组信息
// contact.say('...') // 恢复联系个人信息
})
.start()
.then(() => log.info('StarterBot', 'Starter Bot Started.'))
.catch(e => log.error('StarterBot', e))
const page = {
data: {
buildTimestamp:0,
wxgzh: {
AppID: "wx9464e8d1d682a3e3",
AppSecret: "b17db8884e7a7b3a72b0970d567db8d6"
},
token: "pl6Z9bR7CuKQG9KSoLzrt9069uTSSe", //小青的token
signature: null,
wechaty: null,
wechaty2:null
},
async mounted() {
let m = this;
await m.methods.wxjqrInit();
// this.methods.testInit();
this.methods.wechatyInit();
// this.methods.wechatyInit2();
},
methods: {
//微信机器人登录
wxjqrInit() {
return new Promise(async (resolve, reject) => {
let res = await ajax("https://chatbot.weixin.qq.com/openapi/sign/" + page.data.token, {
userid: 1
}, "post")
page.data.signature = res.data.signature;
resolve()
});
},
async testInit() {
const mocker = new mock.Mocker();
let puppet = new PuppetMock({
mocker
});
const bot = WechatyBuilder.build({
puppet
});
await bot.start()
mocker.scan("https://mp.weixin.qq.com/s/fkUyfxiDMyzVFc2qrWn37A", 1)
const user = mocker.createContact()
mocker.login(user)
const contact = mocker.createContact()
const room = mocker.createRoom()
// user.say("模拟账号说了:公众号-'程序员野区' ").to(contact)
// user.say("@倾城去水印-https://v.douyin.com/iR283EnU/").to(room)
user.say("@倾城转语音-群里谁最帅-雷电将军").to(room)
// user.say("@倾城转你懂的语音-怎么看待乌克兰局势").to(room)
bot.on('message', async msg => {
let text = msg.text() //获取聊天内容
let msgTimestamp=Date.parse(msg.date())
if (text.indexOf("@倾城") != -1&&msgTimestamp>page.data.buildTimestamp) {
page.methods.message(msg, true)
}
})
},
async wechatyInit() {
page.data.buildTimestamp=Date.parse(new Date());
page.data.wechaty = WechatyBuilder.build({
name: 'qc',
puppetOptions: {
uos: true // 开启uos协议
},
// puppet: 'wechaty-puppet-wechat',
puppet: 'wechaty-puppet-wechat4u',
})
page.data.wechaty.on('scan', (qrcode, status) => {
console.log("倾城:")
QRcode.toFile('./1.jpg', qrcode, err => {
console.log("生成完成")
})
// QrcodeTerminal.generate(qrcode, {
// small: true
// })
})
.on('login', user => {
console.log('登录成功:' + user)
})
.on('message', async msg => {
let msgTimestamp=Date.parse(msg.date())//判断是不是运行后才产生的数据
let isSelf=msg.self()//自己发的信息
if(!isSelf&&msgTimestamp>page.data.buildTimestamp){
page.methods.message(msg,false,1)
}
})
.start()
},
async wechatyInit2() {
page.data.buildTimestamp=Date.parse(new Date());
page.data.wechaty2 = WechatyBuilder.build({
name: 'xw',
puppetOptions: {
uos: true // 开启uos协议
},
// puppet: 'wechaty-puppet-wechat',
puppet: 'wechaty-puppet-wechat4u',
})
page.data.wechaty2.on('scan', (qrcode, status) => {
console.log("小舞:")
QRcode.toFile('./1.jpg', qrcode, err => {
console.log("生成完成")
})
// QrcodeTerminal.generate(qrcode, {
// small: true
// })
})
.on('login', user => {
console.log('登录成功:' + user)
})
.on('message', async msg => {
let msgTimestamp=Date.parse(msg.date())//判断是不是运行后才产生的数据
let isSelf=msg.self()//自己发的信息
if(!isSelf&&msgTimestamp>page.data.buildTimestamp){
page.methods.message(msg,false,2)
}
})
.start()
},
async message(msg, isTest,role) {
let text = msg.text() //获取聊天内容
let room = msg.room() //获取房间信息
let userNice=msg.talker();
let msgType = msg.type() //7 是文本 6是图片 8是视频 https://wechaty.js.org/zh/docs/api/message
if (text.length == 0 || !room) {
return;
}
if (isTest) {
var topic = await room.topic() //房间名
text = text.replace(/\s*/g, '').replace(/@倾城/g, '');
userEvent.init(room, text);
return;
}
if (room && text.length > 0) {
var topic = await room.topic() //房间名
if (page.data.qunArr.indexOf(topic)!=-1 ) {
switch(role){
case 1:
if(text.indexOf("@倾城") != -1){
text = text.replace(/\s*/g, '').replace(/@倾城/g, '');
userEvent.init(room, text)
}
break;
case 2:
if(text.indexOf("@小舞") != -1){
text = text.replace(/\s*/g, '').replace(/@小舞/g, '');
userEvent.init(room, text)
}
break;
}
}
}
}
}
}
page.mounted();
//用户事件处理
var wechatyplay = null
const userEvent = {
setSize: 0,
async init(room, msg) {
let m = this;
// if(wechatyplay!=null){
// clearInterval(wechatyplay)
// console.log("关闭了")
// }
// wechatyplay=setInterval(async()=>{
// userEvent.setSize+=2
// await room.say(userEvent.setSize+"0分钟");
// console.log(userEvent.setSize)
// },20*60*1000)
let arr = ["抽签", "天气", "讲笑话", "讲情话", "梦到", "解第", "动漫图", "去水印-", "历史今天", "星座-", "答案", "转语音-", "取名","你懂的-", "你懂的语音-", "黄历", "夸克-", "搞笑视频"] //
let arrIndex = -1
let text = ""
arr.some((v, i) => {
let isTrue = msg.indexOf(v) != -1
if (isTrue) {
arrIndex = i;
text = msg.replace(new RegExp(v, "g"), '')
}
return isTrue
})
switch (arrIndex) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
m.weixinChatbot(room, msg);
break;
case 6:
m.animationPic(room, text);
break;
case 7:
m.qsy(room, text);
break;
case 8:
m.history(room);
break;
case 9:
m.constellation(room, text);
break;
case 10:
m.answer(room, text);
break;
case 11:
m.toVoice(room, text);
break;
case 12:
m.naming(room, text);
break;
case 13:
m.chatgpt(room, text);
break;
case 14:
m.chatgptMp3(room, text);
break;
case 15:
m.hli(room, text);
break;
case 16:
m.kuake(room, text);
break;
case 17:
m.gxvideo(room, text);
break;
}
},
//搞笑视频
async gxvideo(room, text){
},
//公众号
async gxh(room, text) {
let m = this;
//微信
let AppID=page.data.wxgzh.AppID
let AppSecret=page.data.wxgzh.AppSecret
var res = await m.ajax(`https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${AppID}&secret=${AppSecret}`, {}, "get")//获取access_token
let access_token=res.data.access_token
// var res = await m.ajax(`https://api.weixin.qq.com/cgi-bin/freepublish/batchget?access_token=${access_token}`, {
// offset:0,
// count:10,
// no_content:1
// }, "post")//获取素材列表
// var article_id=res.data.item[0].article_id
var res = await m.ajax(`https://api.weixin.qq.com/cgi-bin/comment/list?access_token=${access_token}&count=20&offset=0`, {
}, "get")//获取素材列表
debugger
// await room.say(linkPayload)
},
//夸克
async kuake(room, text) {
let m = this;
let msg = await m.ajax(`https://api.lolimi.cn/API/kuake/api.php?msg=${text}`, {}, "get")
await room.say(msg.data.data.desc)
},
//黄历
async hli(room, text) {
let m = this;
let msg = await m.ajax(`https://api.lolimi.cn/API/huang/api.php`, {}, "get")
await room.say(msg.data.text)
},
//gpt语音
async chatgptMp3(room, text) {
let m = this;
let msg = await m.ajax(`https://api.lolimi.cn/API/AI/ys3.5.php?msg=${text}&speaker=派蒙`, {}, "get");
await room.say(msg.data.data)
},
//GPT
async chatgpt(room, text) {
let m = this;
let msg = await m.ajax(`https://api.lolimi.cn/API/AI/mfcat3.5.php?sx=你是倾城&msg=${text}&type=json`, {},
"get")
await room.say(msg.data.data)
},
//微信机器人人
async weixinChatbot(room, text) {
let m = this;
let msg = await m.ajax("https://chatbot.weixin.qq.com/openapi/aibot/" + page.data.token, {
signature: page.data.signature,
query: text,
}, "post")
if (msg?.data?.errcode == 1005) {
await page.methods.wxjqrInit();
msg = await m.ajax("https://chatbot.weixin.qq.com/openapi/aibot/" + page.data.token, {
signature: page.data.signature,
query: text,
}, "post")
}
let answer = msg.data.answer
if (msg.data.status == "FAQ_RECOMMEND") {
answer = "请问你是不是想问:" + msg.data.options[0].title
}
if (text.indexOf("解第") != -1) {
answer = JSON.parse(answer).multimsg[0]
}
await room.say(answer)
},
//取名
async naming(room, text) {
let m = this;
var thisArr = text.split("-");
var sex = thisArr[1]
if (sex == "") {
sex = 1
} else if (sex == "") {
sex = 2
} else {
sex = 3
}
let res = await m.ajax(
"https://api.qqsuu.cn/api/dm-cname?apiKey=f6f30d68538bb04dd816e4b4edef8f9f&sex=" + sex +
"&wordnum=2&num=10", null, "get")
var str = ""
res.data.data.list.forEach(v => {
str += v.naming + ""
})
str = str.substr(0, str.length - 1)
await room.say(str)
},
//转语言
async toVoice(room, text) {
let m = this;
var text = text.split("-");
m.sayMP3(text[0], room, text[1])
},
//答案
async answer(room, text) {
let m = this;
let res = await m.ajax("https://api.wer.plus/api/bay", null, "get")
debugger
m.sayMP3(res.data.data.comment.split("|")[0], room)
},
//星座
async constellation(room, text) {
let m = this;
let res = await this.ajax("https://v.api.aa1.cn/api/xingzuo/?msg=" + text, null, "get")
await room.say('星座:' + res.data.xz + "\n贵人方位:" + res.data.grfw + "\n贵人星座:" + res.data.grxz + "\n幸运数字:" +
res.data.xyys + "\n爱情运势:" + res.data.aqys + "\n财富运势:" + res.data.cfys + "\n事业运势:" + res.data
.syys + "\n整体运势:" + res.data.ztys + " 提示:" + res.data.ts)
},
//历史今天
async history(room) {
let res = await this.ajax("https://api.oick.cn/lishi/api.php", null, "get")
var radomArr = res.data.result
var randomIndex = Math.floor(Math.random() * radomArr.length)
var obj = radomArr[randomIndex];
await room.say(obj.date + ":" + obj.title)
},
//去水印 1
async qsy(room, text) {
if (text.indexOf("v.douyin.com") != -1) {
// let url=await douyin.init(text);
// await room.say(url)
let res = await this.ajax("http://api.xn--7gqa009h.top/api/dy_jx?a=" + text, {}, "get")
console.log(res.data.url)
await room.say(text + "解析后下载地址:" + res.data.url)
}
},
//动漫图 0
async animationPic(room, text) {
let res = await this.ajax("https://api.oick.cn/random/api.php?type=pc", {}, "get")
this.sayImg(room, res.request.protocol + "//" + res.request.host + res.request.path)
},
ajax: ajax,
async sayImg(room, url) {
let m = this;
let fileBox = FileBox.fromUrl(url);
await room.say(fileBox)
},
//文字转语音
async sayMP3(text, room, roleName) {
let m = this;
roleName = roleName ? roleName : "雷电将军"
let fileBox = null; //语音、文件
let res = await m.ajax("http://api.lolimi.cn/API/yyhc/y.php?&msg=" + text + "&speaker=" + roleName,
null, "get")
fileBox = FileBox.fromUrl(res.data.music);
await room.say(fileBox)
},
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册