Skip to content

Commit 1786c5a

Browse files
committed
add optional email notification using mailgun
1 parent 075696f commit 1786c5a

File tree

5 files changed

+120
-14
lines changed

5 files changed

+120
-14
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ including per-group support tickets management.
88
# installation
99

1010
```
11-
brew install chromedriver
1211
npm install
1312
```
1413

@@ -35,4 +34,4 @@ Run and re-run tests anytime files are updated:
3534

3635
```
3736
npm run watch
38-
```
37+
```

bot.js

+38-12
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,46 @@
55
const {Wechaty} = require('wechaty')
66

77
const reload = require('./reloader')
8+
const tmp = require('tmp')
89

910
const data = {}
11+
const urls = {}
1012

11-
Wechaty.instance()
12-
.on('scan', (url, code) => {
13+
function handleScan(url, code) {
14+
if(urls[url]) {
15+
console.log(`already notified user for ${url}`)
16+
return
17+
}
18+
urls[url] = true
19+
var config = reload('./config.json')
20+
if(config.mailgun) {
21+
var mg = reload('./mailgun.js')
22+
if(mg.isValidMailgunConfig(config.mailgun)) {
23+
/* eslint-disable no-sync */
24+
var filename = tmp.tmpNameSync({template: '/tmp/qrcode-XXXXXX.jpg'});
25+
mg.getHttpFile(url, filename, () => mg.email(
26+
mg.createMailgunClient(config.mailgun),
27+
mg.createEmailData(config.mailgun, filename, url))
28+
.then((body) => console.log(body))
29+
.catch((err) => console.log(err)))
30+
} else {
31+
console.log(`invalid configuration ${JSON.stringify(config.mailgun)}`)
32+
}
33+
} else {
34+
console.log('warning: mailgun not configured')
35+
}
1336
console.log(`Scan QR Code to login: ${code}\n${url}`)
14-
})
15-
.on('login', (user) => {
16-
console.log(`User ${user} logined`)
17-
})
18-
.on('message', (message) => {
19-
message.ready().then(() => {
20-
var config = reload('./config.json')
21-
reload('./handler.js')(config, data, message)
37+
}
38+
39+
Wechaty.instance()
40+
.on('scan', handleScan)
41+
.on('login', (user) => {
42+
console.log(`User ${user} logined`)
43+
})
44+
.on('message', (message) => {
45+
message.ready().then(() => {
46+
var config = reload('./config.json')
47+
reload('./handler.js')(config, data, message)
48+
})
2249
})
23-
})
24-
.init()
50+
.init()

mailgun.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict'
2+
3+
const fs = require('fs')
4+
const os = require('os')
5+
const request = require('request');
6+
7+
function email(mailgun, emailData) {
8+
return new Promise((resolve, reject) => {
9+
mailgun.messages().send(emailData, function (error, body) {
10+
if(error) {
11+
reject(error)
12+
} else {
13+
resolve(body);
14+
}
15+
});
16+
})
17+
}
18+
19+
function getHttpFile(url, filename, cb) {
20+
request(url)
21+
.on('error', function(err) {
22+
cb(err)
23+
})
24+
.on('end', function() {
25+
cb()
26+
})
27+
.pipe(fs.createWriteStream(filename))
28+
}
29+
30+
function isValidMailgunConfig(config) {
31+
return Boolean(config.apiKey &&
32+
config.domain &&
33+
config.from &&
34+
config.to &&
35+
config.subject)
36+
}
37+
38+
function createMailgunClient(config) {
39+
var mailgun = require('mailgun-js')({
40+
apiKey: config.apiKey,
41+
domain: config.domain
42+
});
43+
return mailgun
44+
}
45+
46+
function createEmailData(config, filename, text) {
47+
var emailData = {
48+
from: config.from,
49+
inline: filename,
50+
subject: `${new Date().getTime()} ${config.subject}`,
51+
text: `${text}\n\n${new Date()}\n${os.hostname()}`,
52+
to: config.to,
53+
};
54+
return emailData
55+
}
56+
57+
module.exports = {
58+
createEmailData,
59+
createMailgunClient,
60+
email,
61+
getHttpFile,
62+
isValidMailgunConfig
63+
}

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"main": "bot.js",
66
"dependencies": {
77
"candobot-data": "git+ssh://git@github.com/coderbunker/candobot-data.git",
8+
"mailgun-js": "^0.8.1",
9+
"request": "^2.79.0",
810
"require-reload": "^0.2.2",
911
"wechaty": "^0.7.3"
1012
},

test/mailgun_test.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict'
2+
3+
const assert = require('chai').assert
4+
const mg = require('../mailgun')
5+
6+
describe('mailgun', () => {
7+
it('validates config', () => {
8+
assert.isTrue(mg.isValidMailgunConfig({
9+
'apiKey': 'key-testkey',
10+
'domain': 'mg.coderbunker.com',
11+
'from': 'Coderbunker IT <it@coderbunker.com>',
12+
'subject': 'Please scan WeChat QRcode with Coderbunker Phone',
13+
'to': 'wechaty@coderbunker.com',
14+
}))
15+
})
16+
})

0 commit comments

Comments
 (0)