微信每日說,每日自動發送微信消息給你心愛的人node
在掘金看到了一篇《用Node + EJS寫一個爬蟲腳本天天定時女友發一封暖心郵件》後,
在評論區偶然看到一位讀者說可不能夠用微信實現一下。而後最近恰好在作微信機器人的小項目,那就把這個定時任務放到微信上去作吧,說幹就幹,擼了一下午終於擼出來了。git
github:https://github.com/gengchen528/wechatBotgithub
node-schedule非你莫屬了,
能夠定時每月、每一個禮拜、天天具體何時執行什麼任務web
因爲是微信定時發送消息,較郵件來講,微信沒法把圖片和文字放在同一消息框中,因此美觀度來講可能沒有郵件好,不過文字進行排版後仍是能夠的,因爲時間倉促,因此文字比較少,後續會繼續增長內容;npm
index.jsapi
關於微信的登陸,定時任務的建立,發送信息的獲取都在這個文件裏微信
/** * 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') // 二維碼生成 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}登陸了`) // 登錄後建立定時任務 schedule.setSchedule(config.SENDDATE,()=>{ console.log('你的貼心小助理開始工做啦!') main() }) } //登出 function onLogout(user) { console.log(`${user} 登出`) } // 自動加羣功能 async function onMessage (msg) { const contact = msg.from() // 發消息人 const content = msg.text() //消息內容 const room = msg.room() //是不是羣消息 if(room){ console.log(`羣名: ${room.topic()} 發消息人: ${contact.name()} 內容: ${content}`) }else { console.log(`發消息人: ${contact.name()} 消息內容: ${content}`) } if (msg.self()) { return } if(/微信每日說|每日說|微信機器人/.test(content)){ let keyRoom = await this.Room.find({topic: /^微信每日說/i}) if(keyRoom){ try{ await keyRoom.add(contact) await keyRoom.say('微信每日說:歡迎新朋友 ', contact) }catch (e) { console.error(e) } } } } // 自動加好友功能 async function onFriendShip(friendship) { let logMsg try { logMsg = '添加好友' + friendship.contact().name() console.log(logMsg) switch (friendship.type()) { /** * * 1. New Friend Request * * when request is set, we can get verify message from `request.hello`, * and accept this request by `request.accept()` */ case Friendship.Type.Receive: if (/微信每日說|微信機器人|微信|每日說/i.test(friendship.hello())) { logMsg = '自動添加好友,由於驗證信息中帶關鍵字‘每日說’' await friendship.accept() } else { logMsg = '沒有經過驗證 ' + friendship.hello() } break /** * * 2. Friend Ship Confirmed * */ case Friendship.Type.Confirm: logMsg = 'friend ship confirmed with ' + friendship.contact().name() break } } catch (e) { logMsg = e.message } console.log(logMsg) } // 自動發消息功能 async function main() { 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.getWeather() //獲取天氣信息 let today = await untils.formatDate(new Date())//獲取今天的日期 let memorialDay = untils.getDay(config.MEMORIAL_DAY)//獲取記念日天數 let str = today + '<br>' + '今天是咱們在一塊兒的第' + memorialDay + '天' + '<br><br>今日天氣早知道<br><br>' + weather.weatherTips +'<br><br>' +weather.todayWeather+ '每日一句:<br><br>'+one+'<br><br>'+'------來自最愛你的我' await contact.say(str)//發送消息 } const bot = new Wechaty() bot.on('scan', onScan) bot.on('login', onLogin) bot.on('logout', onLogout) bot.on('message', onMessage) bot.on('friendship', onFriendShip) bot.start() .then(() => console.log('開始登錄微信')) .catch(e => console.error(e))
superagent/index.jsasync
const superagent = require('../config/superagent') const config = require('../config/index') const cheerio = require('cheerio') async function getOne() { // 獲取每日一句 let res = await superagent.req(config.ONE,'GET') let $ = cheerio.load(res.text) let todayOneList = $('#carousel-one .carousel-inner .item') let todayOne = $(todayOneList[0]).find('.fp-one-cita').text().replace(/(^\s*)|(\s*$)/g, "") return todayOne; } async function getWeather() { //獲取墨跡天氣 let url = config.MOJI_HOST+config.CITY+'/'+config.LOCATION let res = await superagent.req(url,'GET') let $ = cheerio.load(res.text) let weatherTips = $('.wea_tips em').text() const today = $('.forecast .days').first().find('li'); let todayInfo = { Day:$(today[0]).text().replace(/(^\s*)|(\s*$)/g, ""), WeatherText:$(today[1]).text().replace(/(^\s*)|(\s*$)/g, ""), Temp:$(today[2]).text().replace(/(^\s*)|(\s*$)/g, ""), Wind:$(today[3]).find('em').text().replace(/(^\s*)|(\s*$)/g, ""), WindLevel:$(today[3]).find('b').text().replace(/(^\s*)|(\s*$)/g, ""), PollutionLevel:$(today[4]).find('strong').text().replace(/(^\s*)|(\s*$)/g, "") } let obj = { weatherTips:weatherTips, todayWeather:todayInfo.Day + ':' + todayInfo.WeatherText + '<br>' + '溫度:' + todayInfo.Temp + '<br>' + todayInfo.Wind + todayInfo.WindLevel + '<br>' + '空氣:' + todayInfo.PollutionLevel + '<br>' } return obj } module.exports ={ getOne,getWeather }
因爲須要安裝chromium, 因此要先配置一下鏡像,注意因爲wechaty的限制,最好使用node10以上版本工具
npmpost
npm config set registry https://registry.npm.taobao.org npm config set disturl https://npm.taobao.org/dist npm config set puppeteer_download_host https://npm.taobao.org/mirrors
yarn
yarn config set registry https://registry.npm.taobao.org yarn config set disturl https://npm.taobao.org/dist yarn config set puppeteer_download_host https://npm.taobao.org/mirrors
而後進行項目安裝
git clone git@github.com:gengchen528/wechatBot.git cd wechatBot npm install 或 cnpm install
參數配置
wechatBot/config/index.js
// 配置文件 // 配置文件 module.exports ={ ONE:'http://wufazhuce.com/',//ONE的web版網站 MOJI_HOST:'https://tianqi.moji.com/weather/china/', //中國墨跡天氣url CITY:'shanghai',//收信者所在城市 LOCATION:'pudong-new-district',//收信者所在區 MEMORIAL_DAY:'2015/04/18', //你和收信者的記念日 NAME:'Leo_chen',//微信備註姓名 NICKNAME:'Leo_chen', //微信暱稱 SENDDATE:'30 * * * * *',//定時發送時間,規則見 /schedule/index.js }
開始運行
npm run start 而後掏出你的手機,最好使用小號,掃描控制檯的二維碼便可
由於給這個微信加了自動加好友和拉羣功能,因此有興趣的小夥伴能夠加個人微信進行測試,記得在加好友的時候帶上暗號:微信每日說
,加好友後發送加羣
,會自動發送羣的二維碼;
注意 加好友請在驗證中填寫 微信每日說 才能夠自動加好友
趕快親自試一試吧,相信你會挖掘出更多好玩的功能