Skip to content

Commit bc5c4c7

Browse files
author
chengeng
committed
更新说明文档,添加常见问题解决方案
1 parent 07be218 commit bc5c4c7

File tree

2 files changed

+389
-243
lines changed

2 files changed

+389
-243
lines changed

README.md

+81-243
Original file line numberDiff line numberDiff line change
@@ -1,230 +1,26 @@
1-
## wechatBot
2-
微信每日说,每日自动发送微信消息给爱人
1+
## 微信每日说
32

4-
## 项目介绍
3+
### 详细介绍
4+
参见[微信每日说,每日自动发暖心话给男(女)朋友](detail.md)
55

6-
### 灵感来源
7-
在掘金看到了一篇[《用Node + EJS写一个爬虫脚本每天定时女朋友发一封暖心邮件》](https://juejin.im/post/5c75fa4af265da2d84109219)后,
8-
在评论区偶然看到一位读者说可不可以用微信实现一下。然后最近刚好在做微信机器人的小项目,那就把这个定时任务放到微信上去做吧,说干就干,撸了一下午终于撸出来了。
9-
## 项目地址
10-
github:[https://github.com/gengchen528/wechatBot](https://github.com/gengchen528/wechatBot)
11-
### 使用库
12-
* [wechaty](https://github.com/Chatie/wechaty) - 微信操作
13-
* [node-schedule](https://github.com/node-schedule/node-schedule) - 定时任务
14-
* [superagent](https://github.com/visionmedia/superagent) - 爬取页面信息
15-
* [cheerio](https://github.com/cheeriojs/cheerio#readme) - 抓取页面
16-
* [qrcode-terminal](https://github.com/gtanner/qrcode-terminal) - 终端显示二维码
17-
### 功能
18-
* 定时给朋友发送每日天气提醒以及每日一句
19-
* 根据关键词自动加好友和自动拉群功能
20-
* 后续继续扩展吧...(你有好的想法也可以提pr)
21-
22-
### 数据来源
23-
* 每日一句和上面的大佬一样也是来自[one](http://wufazhuce.com/)
24-
* 天气信息来自[墨迹天气](https://tianqi.moji.com/weather)
25-
26-
### 定时任务
27-
[node-schedule](https://github.com/node-schedule/node-schedule)非你莫属了,
28-
可以定时每个月、每个礼拜、每天具体什么时候执行什么任务
29-
30-
### 实现效果
31-
由于是微信定时发送消息,较邮件来说,微信无法把图片和文字放在同一消息框中,所以美观度来说可能没有邮件好,不过文字进行排版后还是可以的,由于时间仓促,所以文字比较少,后续会继续增加内容;
32-
33-
![](https://user-gold-cdn.xitu.io/2019/2/28/16933e1817ce89f7?w=1136&h=702&f=png&s=157566)
34-
35-
![](https://user-gold-cdn.xitu.io/2019/2/28/16933e04e55a70c3?w=982&h=432&f=png&s=79574)
36-
37-
## 代码说明
38-
39-
### 目录结构
40-
41-
![](https://user-gold-cdn.xitu.io/2019/2/28/16933e8244c7d39a?w=628&h=650&f=png&s=77229)
42-
43-
* config: 存放公共变量和superagent的配置
44-
* schedule: 任务调度的配置
45-
* superagent: 获取每日一句和天气信息
46-
* untils: 抽取的共用方法
6+
### 主要功能
477

48-
### 核心代码
49-
50-
index.js
51-
52-
关于微信的登录,定时任务的创建,发送信息的获取都在这个文件里
53-
54-
/**
55-
* WechatBot
56-
* - https://github.com/gengchen528/wechatBot
57-
*/
58-
const {Wechaty,Friendship} = require('wechaty')
59-
const schedule = require('./schedule/index')
60-
const config = require('./config/index')
61-
const untils = require('./untils/index')
62-
const superagent = require('./superagent/index')
63-
const {FileBox} = require('file-box') //文件读取模块
64-
// 二维码生成
65-
function onScan (qrcode, status) {
66-
require('qrcode-terminal').generate(qrcode) // 在console端显示二维码
67-
const qrcodeImageUrl = [
68-
'https://api.qrserver.com/v1/create-qr-code/?data=',
69-
encodeURIComponent(qrcode),
70-
].join('')
71-
console.log(qrcodeImageUrl)
72-
}
73-
74-
// 登录
75-
async function onLogin (user) {
76-
console.log(`贴心小助理${user}登录了`)
77-
// 登陆后创建定时任务
78-
schedule.setSchedule(config.SENDDATE,()=>{
79-
console.log('你的贴心小助理开始工作啦!')
80-
main()
81-
})
82-
}
83-
84-
//登出
85-
function onLogout(user) {
86-
console.log(`${user} 登出`)
87-
}
88-
// 监听对话 根据关键词自动加群
89-
async function onMessage (msg) {
90-
const contact = msg.from() // 发消息人
91-
const content = msg.text() //消息内容
92-
const room = msg.room() //是否是群消息
93-
const roomCodeUrl = FileBox.fromUrl(config.ROOMCODEURL) //来自url的文件
94-
const roomCodeLocal = FileBox.fromFile(config.ROOMLOCALPATH) //添加本地文件
95-
if (msg.self()) {
96-
return
97-
}
98-
if(room){ // 如果是群消息
99-
const topic = await room.topic()
100-
console.log(`群名: ${topic} 发消息人: ${contact.name()} 内容: ${content}`)
101-
}else { // 如果非群消息
102-
console.log(`发消息人: ${contact.name()} 消息内容: ${content}`)
103-
let addRoomReg = eval(config.ADDROOMWORD)
104-
let roomReg = eval(config.ROOMNAME)
105-
if(addRoomReg.test(content)&&!room){
106-
let keyRoom = await this.Room.find({topic: roomReg})
107-
if(keyRoom){
108-
try{
109-
await contact.say(roomCodeLocal||roomCodeUrl)
110-
await keyRoom.say('微信每日说:欢迎新朋友', contact)
111-
}catch (e) {
112-
console.error(e)
113-
}
114-
}
115-
}else {
116-
await contact.say('你好,不要轻易调戏我,我只会发群二维码,不会聊天的!')
117-
await contact.say('请回复暗号:加群 获取群二维码图片')
118-
}
119-
}
120-
}
121-
// 自动加好友功能
122-
async function onFriendShip(friendship) {
123-
let logMsg
124-
try {
125-
logMsg = '添加好友' + friendship.contact().name()
126-
console.log(logMsg)
127-
128-
switch (friendship.type()) {
129-
/**
130-
*
131-
* 1. New Friend Request
132-
*
133-
* when request is set, we can get verify message from `request.hello`,
134-
* and accept this request by `request.accept()`
135-
*/
136-
case Friendship.Type.Receive:
137-
let addFriendReg = eval(config.ADDFRIENDWORD)
138-
if (addFriendReg.test(friendship.hello())) {
139-
logMsg = '自动添加好友,因为验证信息中带关键字‘每日说’'
140-
await friendship.accept()
141-
} else {
142-
logMsg = '没有通过验证 ' + friendship.hello()
143-
}
144-
break
145-
/**
146-
*
147-
* 2. Friend Ship Confirmed
148-
*
149-
*/
150-
case Friendship.Type.Confirm:
151-
logMsg = 'friend ship confirmed with ' + friendship.contact().name()
152-
break
153-
}
154-
} catch (e) {
155-
logMsg = e.message
156-
}
157-
console.log(logMsg)
158-
}
159-
// 自动发消息功能
160-
async function main() {
161-
let contact = await bot.Contact.find({name:config.NICKNAME}) || await bot.Contact.find({alias:config.NAME}) // 获取你要发送的联系人
162-
let one = await superagent.getOne() //获取每日一句
163-
let weather = await superagent.getWeather() //获取天气信息
164-
let today = await untils.formatDate(new Date())//获取今天的日期
165-
let memorialDay = untils.getDay(config.MEMORIAL_DAY)//获取纪念日天数
166-
let str = today + '<br>' + '今天是我们在一起的第' + memorialDay + '天'
167-
+ '<br><br>今日天气早知道<br><br>' + weather.weatherTips +'<br><br>' +weather.todayWeather+ '每日一句:<br><br>'+one+'<br><br>'+'------来自最爱你的我'
168-
await contact.say(str)//发送消息
169-
}
170-
171-
const bot = new Wechaty()
172-
173-
bot.on('scan', onScan)
174-
bot.on('login', onLogin)
175-
bot.on('logout', onLogout)
176-
bot.on('message', onMessage)
177-
bot.on('friendship', onFriendShip)
178-
179-
bot.start()
180-
.then(() => console.log('开始登陆微信'))
181-
.catch(e => console.error(e))
8+
* 定时给朋友发送每日天气提醒以及每日一句
9+
* 根据关键词自动加好友
10+
* 根据关键词自动拉群功能(需要群二维码图片,群人数超过100人,只能手动邀请)
11+
* 图灵机器人自动聊天(需要自己申请图灵api)
12+
* 还在不断增加中..
18213

14+
## 环境
18315

16+
* node.js (version >= 10 作者使用node 10.15.1)
17+
* Mac/Linux/Windows
18418

185-
superagent/index.js
19+
## 安装配置
18620

187-
const superagent = require('../config/superagent')
188-
const config = require('../config/index')
189-
const cheerio = require('cheerio')
190-
191-
async function getOne() { // 获取每日一句
192-
let res = await superagent.req(config.ONE,'GET')
193-
let $ = cheerio.load(res.text)
194-
let todayOneList = $('#carousel-one .carousel-inner .item')
195-
let todayOne = $(todayOneList[0]).find('.fp-one-cita').text().replace(/(^\s*)|(\s*$)/g, "")
196-
return todayOne;
197-
}
198-
199-
async function getWeather() { //获取墨迹天气
200-
let url = config.MOJI_HOST+config.CITY+'/'+config.LOCATION
201-
let res = await superagent.req(url,'GET')
202-
let $ = cheerio.load(res.text)
203-
let weatherTips = $('.wea_tips em').text()
204-
const today = $('.forecast .days').first().find('li');
205-
let todayInfo = {
206-
Day:$(today[0]).text().replace(/(^\s*)|(\s*$)/g, ""),
207-
WeatherText:$(today[1]).text().replace(/(^\s*)|(\s*$)/g, ""),
208-
Temp:$(today[2]).text().replace(/(^\s*)|(\s*$)/g, ""),
209-
Wind:$(today[3]).find('em').text().replace(/(^\s*)|(\s*$)/g, ""),
210-
WindLevel:$(today[3]).find('b').text().replace(/(^\s*)|(\s*$)/g, ""),
211-
PollutionLevel:$(today[4]).find('strong').text().replace(/(^\s*)|(\s*$)/g, "")
212-
}
213-
let obj = {
214-
weatherTips:weatherTips,
215-
todayWeather:todayInfo.Day + ':' + todayInfo.WeatherText + '<br>' + '温度:' + todayInfo.Temp + '<br>'
216-
+ todayInfo.Wind + todayInfo.WindLevel + '<br>' + '空气:' + todayInfo.PollutionLevel + '<br>'
217-
}
218-
return obj
219-
}
220-
module.exports ={
221-
getOne,getWeather
222-
}
223-
224-
225-
## 项目运行
21+
### 配置源
22622

227-
由于需要安装chromium, 所以要先配置一下镜像
23+
npm或yarn配置淘宝源(重要,因为需要安装chromium,不配置的话下载会失败或者速度很慢,因为这个玩意140M左右)
22824

22925
npm
23026

@@ -236,49 +32,91 @@ yarn
23632
yarn config set registry https://registry.npm.taobao.org
23733
yarn config set disturl https://npm.taobao.org/dist
23834
yarn config set puppeteer_download_host https://npm.taobao.org/mirrors
239-
然后进行项目安装
240-
35+
36+
### 安装
24137
git clone [email protected]:gengchen528/wechatBot.git
24238
cd wechatBot
243-
npm install 或 cnpm install
244-
245-
参数配置
246-
247-
wechatBot/config/index.js
248-
39+
npm install
40+
npm run start
41+
42+
## 项目相关配置
43+
config/index.js
44+
24945
// 配置文件
25046
module.exports ={
47+
AUTOADDFRIEND:false,//自动加好友功能 默认关闭
48+
AUTOADDROOM:false,//自动拉群功能 默认关闭
49+
AUTOREPLY:false,//自动聊天功能 默认关闭
25150
ONE:'http://wufazhuce.com/',////ONE的web版网站
25251
MOJI_HOST:'https://tianqi.moji.com/weather/china/', //中国墨迹天气url
25352
CITY:'shanghai',//收信者所在城市
25453
LOCATION:'pudong-new-district',//收信者所在区 (可以访问墨迹天气网站后,查询区的英文拼写)
54+
AIBOTAPI:'http://www.tuling123.com/openapi/api',//图灵机器人API 注册地址http://www.turingapi.com/
55+
APIKEY:'你的图灵机器人apikey',//图灵机器人apikey
25556
MEMORIAL_DAY:'2015/04/18', //你和收信者的纪念日
256-
NAME:'Leo_chen',//备注姓名
257-
NICKNAME:'Leo_chen', //昵称
258-
SENDDATE:'30 15 8 * * *',//定时发送时间 每天8点15分30秒发送,规则见 /schedule/index.js
259-
ROOMNAME:'/^微信每日说/i', //群名(请只修改中文,不要删除符号,这是正则)
260-
ADDFRIENDWORD:'/微信每日说/i',//自动加好友触发的关键词(请只修改中文,不要删除符号,这是正则)
57+
NAME:'A兔子',//备注姓名
58+
NICKNAME:'嗯哼', //昵称
59+
SENDDATE:'0 0 8 * * *',//定时发送时间 每天8点15分30秒发送,规则见 /schedule/index.js
60+
ROOMNAME:'/^你的群名/i', //群名(请只修改中文,不要删除符号,这是正则)
61+
ADDFRIENDWORD:'/你要触发的关键词/i',//自动加好友触发的关键词(请只修改中文,不要删除符号,这是正则)
26162
ADDROOMWORD:'/加群/',//自动发送群图片触发关键词(请只修改中文,不要删除符号,这是正则)
26263
ROOMCODEURL:'http://image.bloggeng.com/qun.png',//群二维码url链接(与本地群二维码路径选填一个)
26364
ROOMLOCALPATH:'./static/qun.png',//本地群二维码图片路径(与群url选填一个)
26465
}
26566

266-
开始运行
67+
68+
## 常见问题处理
69+
70+
1. 我的微信号无法登陆
26771

268-
npm run start
269-
然后掏出你的手机,最好使用小号,扫描控制台的二维码即可
72+
从2017年6月下旬开始,使用基于web版微信接入方案存在大概率的被限制登陆的可能性。 主要表现为:无法登陆Web 微信,但不影响手机等其他平台。 验证是否被限制登陆: https://wx.qq.com 上扫码查看是否能登陆。 更多内容详见:
73+
74+
[Can not login with error message: 当前登录环境异常。为了你的帐号安全,暂时不能登录web微信。](https://github.com/Chatie/wechaty/issues/603)
75+
76+
[[谣言] 微信将会关闭网页版本](https://github.com/Chatie/wechaty/issues/990)
77+
78+
[新注册的微信号无法登陆](https://github.com/Chatie/wechaty/issues/872)
79+
80+
2. 执行npm run start时无法安装puppet-puppeteer&&Chromium
81+
82+
* Centos7下部署出现以下问题
83+
![](http://image.bloggeng.com/14481551970095_.pic_hd.jpg)
84+
85+
问题原因:[https://segmentfault.com/a/1190000011382062](https://segmentfault.com/a/1190000011382062)
86+
87+
解决方案:
88+
89+
#依赖库
90+
yum install pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXtst.x86_64 cups-libs.x86_64 libXScrnSaver.x86_64 libXrandr.x86_64 GConf2.x86_64 alsa-lib.x86_64 atk.x86_64 gtk3.x86_64 -y
91+
92+
#字体
93+
yum install ipa-gothic-fonts xorg-x11-fonts-100dpi xorg-x11-fonts-75dpi xorg-x11-utils xorg-x11-fonts-cyrillic xorg-x11-fonts-Type1 xorg-x11-fonts-misc -y
94+
* windows下,下载puppeteer失败
95+
96+
链接:https://pan.baidu.com/s/1YF09nELpO-4KZh3D2nAOhA
97+
提取码:0mrz
98+
99+
把下载的文件放到如下图路径,并解压到当前文件夹中即可
100+
![](http://image.bloggeng.com/14241551970542_.pic_hd.jpg)
101+
3. 支持 红包、转账、朋友圈… 吗
102+
103+
支付相关 - 红包、转账、收款 等都不支持
104+
105+
4. 更多关于wechaty功能相关接口
106+
107+
[参考wechaty官网文档](https://docs.chatie.io/v/zh/)
270108

271-
## 待解决问题
109+
5. 其他问题解决方案
272110

273-
* 由于微信登录和邮件登录不同,所以无法使用进程守护工具,目前没有测试是否能够长时间登录
274-
* 因为node的原因,如果发生错误,可能会导致任务无法进行,需要手动重启并登录
275-
* 最好能够使用小号登录,如果是常用微信登录,在电脑客户端登陆后可能会wechaty挤掉
276-
* 墨迹天气页面在获取的时候可能会存在延时,有时可能获取不到
111+
* 先检查node版本是否大于10
112+
* 确认npm或yarn已经配置好淘宝源
113+
* 存在package-lock.json文件先删除
114+
* 删除`node_modules`后重新执行`npm install``cnpm install`
277115

278-
## 后续功能
116+
## 注意
279117

280-
* 为了防止占用你的微信号,你和你的爱人添加我的微信后。你发送指定内容,我将会每天帮你发送消息
281-
* 还有在思考中...(你有好的想法也可以提出来)
118+
本项目属于个人兴趣开发,开源出来是为了技术交流,请勿使用此项目做违反微信规定或者其他违法事情。
119+
建议使用小号进行测试,有被微信封禁网页端登录权限的风险(客户端不受影响),请确保自愿使用。因为个人使用不当导致网页端登录权限被封禁,均与作者无关,谢谢理解
282120

283121
## 最后
284122

0 commit comments

Comments
 (0)