提交 edce2e10 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

#33 new example bot: friend-bot.js

上级 920f426a
const {
Wechaty
, Message
, Config
, log
} = require('../')
const welcome = `
=============== Powered by Wechaty ===============
-------- https://github.com/wechaty/wechaty --------
Hello,
I'm a Wechaty Botie with the following super powers:
1. Send Friend Request
2. Accept Friend Request
3. Recongenize Verify Message
If you send friend request to me,
with a verify message 'ding',
I will accept your request automaticaly!
__________________________________________________
Hope you like it, and you are very welcome to
upgrade me for more super powers!
Please wait... I'm trying to login in...
`
console.log(welcome)
const bot = new Wechaty({ profile: Config.DEFAULT_PROFILE })
bot
.on('login' , user => log.info('Bot', `${user.name()} logined`))
.on('logout' , user => log.info('Bot', `${user.name()} logouted`))
.on('error' , e => log.info('Bot', 'error: %s', e))
.on('scan', ({url, code}) => {
if (!/201|200/.test(code)) {
let loginUrl = url.replace(/\/qrcode\//, '/l/')
require('qrcode-terminal').generate(loginUrl)
}
console.log(`${url}\n[${code}] Scan QR Code in above url to login: `)
})
/**
*
* Wechaty Event: `friend`
*
*/
.on('friend', (contact, request) => {
let logMsg
const m = new Message()
m.set('to', 'filehelper')
contact.ready().then(_ => {
try {
logMsg = 'received `friend` event from ' + contact.get('name')
m.set('content', logMsg)
bot.send(m)
console.log(logMsg)
/**
*
* 1. New Friend Request
*
* when request is set, we can get verify message from `request.hello`,
* and accept this request by `request.accept()`
*/
if (request) {
if (request.hello === 'ding') {
logMsg = 'accepted because verify messsage is "ding"'
request.accept()
} else {
logMsg = 'not auto accepted, because verify message is: ' + request.hello
}
/**
*
* 2. Friend Ship Confirmed
*
*/
} else {
logMsg = 'friend ship confirmed with ' + contact.get('name')
}
} catch (e) {
logMsg = e.message
}
console.log(logMsg)
m.set('content', logMsg)
bot.send(m)
})
})
bot.init()
.catch(e => {
log.error('Bot', 'init() fail: %s', e)
bot.quit()
process.exit(-1)
})
......@@ -14,6 +14,7 @@
"CMD_CHROMIUM": "/wechaty/bin/xvfb-chromium"
},
"scripts": {
"node": "node",
"lint": "npm run eslint && npm run tslint",
"eslint": "eslint \"{src,test,bin}/**/*.js\"",
"tslint": "tslint \"{src,test,bin}/**/*.ts\"",
......
......@@ -346,7 +346,8 @@ function onServerMessage(data) {
const request = new FriendRequest()
request.receive(data.RecommendInfo)
this.emit('friend', request.contact, request)
const contact = Contact.load(request.contactId)
this.emit('friend', contact, request)
break
case Message.Type.SYS:
......
......@@ -36,9 +36,9 @@ class PuppetWebFriendRequest extends FriendRequest {
}
this.info = info
this.contact = Contact.load(info.UserName)
this.hello = info.Content
this.ticket = info.Ticket
this.contactId = info.UserName
this.hello = info.Content
this.ticket = info.Ticket
// ??? this.nick = info.NickName
if (!this.ticket) {
......
......@@ -130,7 +130,7 @@ class Server extends EventEmitter {
client.on('error' , e => {
log.error('PuppetWebServer', 'initEventsFromClient() client on error: %s', e)
// console.log(e)
log.silly('PuppetWebServer', 'initEventsFromClient() client on error: %s', e.stack)
})
// Events from Wechaty@Broswer --to--> Server
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册