forked from leochen-g/wechatBot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
103 lines (90 loc) · 3.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* WechatBot
* - https://github.com/gengchen528/wechatBot
*/
const { Wechaty, Friendship } = require('wechaty')
const schedule = require('./schedule/index')
const config = require('./config/index')
const untils = require('./untils/index')
const superagent = require('./superagent/index')
// 延时函数,防止检测出类似机器人行为操作
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
// 二维码生成
function onScan(qrcode, status) {
require('qrcode-terminal').generate(qrcode) // 在console端显示二维码
const qrcodeImageUrl = [
'https://api.qrserver.com/v1/create-qr-code/?data=',
encodeURIComponent(qrcode),
].join('')
console.log(qrcodeImageUrl)
}
// 登录
async function onLogin(user) {
console.log(`贴心小助理${user}登录了`)
if (config.AUTOREPLY) {
console.log(`已开启机器人自动聊天模式`)
}
// 登陆后创建定时任务
await initDay()
}
//登出
function onLogout(user) {
console.log(`小助手${user} 已经登出`)
}
// 监听对话
async function onMessage(msg) {
const contact = msg.from() // 发消息人
const content = msg.text() //消息内容
const room = msg.room() //是否是群消息
if (msg.self()) {
return
}
if (room) { // 如果是群消息
const topic = await room.topic()
console.log(`群名: ${topic} 发消息人: ${contact.name()} 内容: ${content}`)
} else { // 如果非群消息
console.log(`发消息人: ${contact.name()} 消息内容: ${content}`)
if (config.AUTOREPLY && contact.name() == config.AUTOREPLYPERSON) { // 如果开启自动聊天且已经指定了智能聊天的对象才开启机器人聊天
let reply = await superagent.getReply(content)
console.log('天行机器人回复:', reply)
try {
await delay(2000)
await contact.say(reply)
} catch (e) {
console.error(e)
}
}
}
}
// 创建微信每日说定时任务
async function initDay() {
console.log(`已经设定每日说任务`)
schedule.setSchedule(config.SENDDATE, async() => {
console.log('你的贴心小助理开始工作啦!')
let logMsg
let contact = await bot.Contact.find({ name: config.NICKNAME }) || await bot.Contact.find({ alias: config.NAME }) // 获取你要发送的联系人
let one = await superagent.getOne() //获取每日一句
let weather = await superagent.getTXweather() //获取天气信息
let today = await untils.formatDate(new Date()) //获取今天的日期
let memorialDay = untils.getDay(config.MEMORIAL_DAY) //获取纪念日天数
let sweetWord = await superagent.getSweetWord()
let str = today + '<br>我们在一起的第' + memorialDay + '天<br>' + '<br>元气满满的一天开始啦,要开心噢^_^<br>' +
'<br>今日天气<br>' + weather.weatherTips + '<br>' + weather.todayWeather + '<br>每日一句:<br>' + one + '<br>' + '<br>每日土味情话:<br>' + sweetWord + '<br><br>' + '————————最爱你的我'
try {
logMsg = str
await delay(2000)
await contact.say(str) // 发送消息
} catch (e) {
logMsg = e.message
}
console.log(logMsg)
})
}
const bot = new Wechaty({ name: 'WechatEveryDay' })
bot.on('scan', onScan)
bot.on('login', onLogin)
bot.on('logout', onLogout)
bot.on('message', onMessage)
bot.start()
.then(() => console.log('开始登陆微信'))
.catch(e => console.error(e))