-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.js
75 lines (66 loc) · 1.35 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const os = require('os');
const fs = require('fs');
const path = require('path');
const WeChatBot = require('../wechat');
let options = {
// path to downloda qrcode image
basePath: path.join(os.userInfo().homedir, 'Desktop')
}
let weChatBot = new WeChatBot(options);
/**
*
* after downloaded qrcode image
*
*/
weChatBot.on('qrcode', args => {
console.log('qrcode file: ' + args.file);
console.log('you need to open this file, and scan it use wechat to login.');
});
/**
*
* after fetched current user's information
*
*
*/
weChatBot.on('selfInfo', args => {
console.log('self info: ')
console.dir(args.selfInfo);
});
/**
*
* after fetched contact list
*
*
*/
weChatBot.on('contacts', args => {
console.log('contact count is ' + args.contacts.length);
});
/**
*
* after msg arrived
*
*
*/
weChatBot.on('msg', args => {
let sender = args.from;
let msg = args.msg;
if(msg.MsgType != 1)
return;
let userName = msg.FromUserName;
if(sender) {
userName = sender.nickName + (sender.isSelf ? '(Self)' : '');
}
console.log(`${userName}: \n\t${msg.Content}`);
if(msg.Content.indexOf('hello') >= 0) {
// send hello wolrd to sender
weChatBot.sendTextMsg(sender.userName, 'hello bot', (err, retCode) => {
console.log('send msg ' + (err ? err : ' success.'));
});
}
});
/**
*
* login after setuped event listeners
*
*/
weChatBot.login();