diff --git a/ReadMe.md b/ReadMe.md new file mode 100644 index 0000000..f347c25 --- /dev/null +++ b/ReadMe.md @@ -0,0 +1,22 @@ +# FCC Panky + +FCC 成都社区的微信群管理机器人,源自[【微信机器人工作坊】][1] + +## 核心功能 + +1. 入群提醒 +2. 自动回复 + - 官网链接 + +## 本地运行 + +```shell +git clone https://github.com/FreeCodeCamp-Chengdu/wechat-robot.git ~/Desktop/FCC-Panky +cd ~/Desktop/FCC-Panky + +npm set puppeteer_download_host https://storage.googleapis.com.cnpmjs.org +npm install +npm start +``` + +[1]: https://fcc-cd.tk/activity/workshop/wechat-robot/ diff --git a/package.json b/package.json index 694119e..fb4403a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fcc-cdc/wechat-robot", - "version": "0.1.0", + "version": "0.2.0", "description": "WeChat group administrator of FCC ChengDu community", "license": "AGPL-3.0", "author": "shiy2008@gmail.com", @@ -12,16 +12,19 @@ "bugs": { "url": "https://github.com/FreeCodeCamp-Chengdu/wechat-robot/issues" }, - "main": "index.js", + "main": "source/index.js", "scripts": { "format": "prettier --write \"{,!(node_modules|.git)/**/}*.{html,md,css,less,js,json,yml,yaml}\"", - "start": "node index" + "start": "node source/" }, "husky": { "hooks": { "pre-commit": "npm run format && git add ." } }, + "engines": { + "node": "^7.6.0" + }, "dependencies": { "puppeteer": "^1.17.0", "qrcode-terminal": "^0.12.0", diff --git a/source/core.js b/source/core.js index 865e4f8..c667812 100644 --- a/source/core.js +++ b/source/core.js @@ -1,9 +1,36 @@ -exports.roomJoin = function(room, list, guy) { - room.say(`欢迎喜爱编程的小伙伴入群: -${list.map((guy, index) => ` ${++index}. ${guy.name()}`)} +const roomList = require('./room.json'), + replyMap = require('./reply.json'); + +exports.roomJoin = async (room, list, guy) => { + const topic = await room.topic(), + name = guy.name(); + + list = list.map(guy => guy.name()); + + console.log(`【加群】${topic} 的 ${name} 邀请了 ${list}`); + + return room.say(`欢迎喜爱编程的小伙伴入群: +${list.map((name, index) => ` ${++index}. ${name}`)} 请新人按照“昵称 - 技术栈”的格式修改自己的群名,并仔细阅读群公告中的【FCC 社区行为准则】: https://fcc-cd.tk/profile/code-of-conduct/ -若以上新人有因违反【行为准则】而被踢,邀请人 ${guy.name()} 也将负连带责任,被一起踢出本群。`); +若以上新人有因违反【行为准则】而被踢,邀请人 ${name} 也将负连带责任,被一起踢出本群。`); +}; + +exports.message = async message => { + const room = message.room(), + text = message.text(); + + if (!room || !roomList.includes(await room.topic())) return; + + for (let pattern in replyMap) + if (text.match(new RegExp(pattern, 'i'))) { + console.log(`【回复】 + ${text} + => + ${replyMap[pattern]}`); + + return message.say(replyMap[pattern]); + } }; diff --git a/source/index.js b/source/index.js index 4b20358..820052b 100644 --- a/source/index.js +++ b/source/index.js @@ -1,7 +1,9 @@ +#! /usr/bin/env node + const { Wechaty } = require('wechaty'), QRCoder = require('qrcode-terminal'); -const { roomJoin } = require('./core'); +const { roomJoin, message } = require('./core'); const robot = new Wechaty(); @@ -12,5 +14,5 @@ robot .on('login', user => console.log('登录成功:' + user)) .on('logout', user => console.log('登出成功:' + user)) .on('room-join', roomJoin) - .on('message', message => console.log('收到消息:' + message)) + .on('message', message) .start(); diff --git a/source/reply.json b/source/reply.json new file mode 100644 index 0000000..cc71a2f --- /dev/null +++ b/source/reply.json @@ -0,0 +1,5 @@ +{ + "(FCC|freeCodeCamp)\\s*成都.*官?方?网站?": "https://fcc-cd.tk/", + "(FCC|freeCodeCamp)\\s*中(文|国).*官?方?网站?": "https://freecodecamp.one/", + "(FCC|freeCodeCamp)\\s*(国际|全球).*官?方?网站?": "https://freecodecamp.org/" +} diff --git a/source/room.json b/source/room.json new file mode 100644 index 0000000..66ff7f6 --- /dev/null +++ b/source/room.json @@ -0,0 +1,7 @@ +[ + "FCC成都社区 - 1群", + "FCC成都社区 - 2群", + "FCC成都社区 - 3群", + "FCC成都社区 - 4群", + "微信机器人测试" +]