Skip to content

Commit

Permalink
更 又写错了
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxing committed Apr 25, 2018
1 parent 34be5a3 commit 4b61afa
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lesson12/README.md
Expand Up @@ -2,7 +2,7 @@

现在,日常生活已经离不开微信,本文将会抛砖引玉演示如何使用[wechaty](https://github.com/Chatie/wechaty)操作微信个人号做一些有意思的东西,可以实现自动通过好友请求、关键词回复、自动拉群等功能。大大提高了社群运营的效率。

wechaty(https://github.com/Chatie/wechaty)是一款开源的微信个人号SDK,进行了一系列的封装,提供简单好用的接口,然后开发者可以在其之上进行微信机器人的开发。你可以用它来做很多事:
wechaty([https://github.com/Chatie/wechaty](https://github.com/Chatie/wechaty))是一款开源的微信个人号SDK,进行了一系列的封装,提供简单好用的接口,然后开发者可以在其之上进行微信机器人的开发。你可以用它来做很多事:

1)管理和维护好友;
2)快速有序地处理聊天信息
Expand Down
66 changes: 66 additions & 0 deletions lesson12/bot.js
@@ -0,0 +1,66 @@
const { Wechaty, MediaMessage, Room } = require('wechaty')
const QrcodeTerminal = require('qrcode-terminal')
const path = require('path')
const QRODE_IMAGE_FILE = path.join(__dirname, 'qrcode.jpg')

Wechaty.instance() // Singleton
.on('scan', (url, code) => {
let loginUrl = url.replace('qrcode', 'l')
QrcodeTerminal.generate(loginUrl)
console.log(url)
})
.on('login', user => console.log(`User ${user} logined`))
.on('friend', async (contact, request) => {
if (request) {
if (/JavaScript|Js/i.test(request.hello)) {
logMsg = 'accepted because verify messsage is "JS"'
request.accept()
} else {
logMsg = 'not auto accepted, because verify message is: ' + request.hello
}
} else {
logMsg = 'friend ship confirmed with ' + contact.get('name')
}
})
.on('message', async (message) => {
const contact = message.from()
const content = message.content()
const room = message.room()
if (room) {
console.log(`Room: ${room.topic()} Contact: ${contact.name()} Content: ${content}`)
} else {
console.log(`Contact: ${contact.name()} Content: ${content}`)
}

if (message.self()) {
return
}

if (content === '指令') {
await message.say(
`欢迎欢迎。。。
你可以回复以下指令:
回复【指令】查看指令
回复【js】自动回复
回复【加群】自动加群
来都来了,快去关注下公众号
JavaScript之禅`)
await message.say(new MediaMessage(QRODE_IMAGE_FILE))
}


if (/JavaScript|Js/i.test(content)) {
await message.say('关注公众号 JavaScript之禅没?')
await message.say(new MediaMessage(QRODE_IMAGE_FILE))
}

if (/加群/.test(content)) {
let keyroom = await Room.find({ topic: 'test' })
if (keyroom) {
await keyroom.add(contact)
await keyroom.say('欢迎新朋友!', contact)
}
}
})
.start()

0 comments on commit 4b61afa

Please sign in to comment.