ding-dong-bot.js 1.5 KB
Newer Older
1
const log = require('npmlog')
Huan (李卓桓)'s avatar
doc  
Huan (李卓桓) 已提交
2
//log.level = 'verbose'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
3
log.level = 'silly'
4

Huan (李卓桓)'s avatar
clean  
Huan (李卓桓) 已提交
5
const Wechaty = require('../src/wechaty')
6 7

const welcome = `
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
8 9
| __        __        _           _
| \\ \\      / /__  ___| |__   __ _| |_ _   _
10 11 12
|  \\ \\ /\\ / / _ \\/ __| '_ \\ / _\` | __| | | |
|   \\ V  V /  __/ (__| | | | (_| | |_| |_| |
|    \\_/\\_/ \\___|\\___|_| |_|\\__,_|\\__|\\__, |
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
13
|                                     |___/
14 15 16 17 18 19 20 21 22

=============== Powered by Wechaty ===============
-------- https://github.com/zixia/wechaty --------

I'm a bot, my super power is talk in Wechat.

If you send me a 'ding', I will reply you a 'dong'!
__________________________________________________

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
23
Hope you like it, and you are very welcome to
24 25 26 27 28 29 30
upgrade me for more super powers!

Please wait... I'm trying to login in...

`

console.log(welcome)
31
const bot = new Wechaty({head: true})
32

33
bot.init()
Huan (李卓桓)'s avatar
clean  
Huan (李卓桓) 已提交
34 35
.then(bot.getLoginQrImgUrl.bind(bot))
.then(url => console.log(`Scan qrcode in url to login: \n${url}`))
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
36 37 38 39 40
.catch(e => {
  log.error('Bot', 'init() fail:' + e)
  bot.quit()
  process.exit(-1)
})
41

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
42
bot.on('message', m => {
43
  m.ready()
Huan (李卓桓)'s avatar
clean  
Huan (李卓桓) 已提交
44
  .then(msg => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
45
    log.info('Bot', 'recv: %s'  , msg)
Huan (李卓桓)'s avatar
bug fix  
Huan (李卓桓) 已提交
46
  })
Huan (李卓桓)'s avatar
clean  
Huan (李卓桓) 已提交
47
  .catch(e => log.error('Bot', 'ready: %s' , e))
48

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
49
  if (/^(ding|ping|bing)$/i.test(m.get('content'))) {
Huan (李卓桓)'s avatar
format  
Huan (李卓桓) 已提交
50
    const r = new Wechaty.Message()
Huan (李卓桓)'s avatar
bug fix  
Huan (李卓桓) 已提交
51
    r.set('to', m.inGroup() ? m.get('group') : m.get('from'))
Huan (李卓桓)'s avatar
format  
Huan (李卓桓) 已提交
52 53
    r.set('content', 'dong')
    bot.send(r)
54
    .then(() => { log.warn('Bot', 'REPLY: dong') })
Huan (李卓桓)'s avatar
format  
Huan (李卓桓) 已提交
55
  }
56 57
})

Huan (李卓桓)'s avatar
fix #5  
Huan (李卓桓) 已提交
58 59
bot.on('login'	, () => log.info('Bot', 'logined'))
bot.on('logout'	, () => log.info('Bot', 'logouted'))
60