0.1.5 add no email send, get service can return all service, added getServicesList to get just names

master
David Kebler 2020-07-26 16:42:26 -07:00
parent 1fa512da93
commit 5f0532f8aa
5 changed files with 30 additions and 8 deletions

View File

@ -16,16 +16,20 @@ import Notifier from '@uci-utils/notify'
{ user: process.env.GMAIL_SMTP_USER, { user: process.env.GMAIL_SMTP_USER,
pass:process.env.GMAIL_SMTP_PW pass:process.env.GMAIL_SMTP_PW
}, },
to: process.env.NOTIFY_EMAIL_RECIPIENTS, to: process.env.NOTIFY_EMAIL_RECIPIENTS.split(','),
subject:'uci notification' subject:'uci notification'
} }
} }
}) })
console.log('registered services\n',notify.getServicesList())
console.log('registered services\n',notify.getService())
// console.log (notify._services.pushsafer._req) // console.log (notify._services.pushsafer._req)
// notify.disableService('pushsafer') // notify.disableService('pushsafer')
// notify.enableService('pushsafer') // notify.enableService('pushsafer')
// console.log(await notify.send('a test message','pushsafer')) console.log('pushsafter only', await notify.send('a pushsafter only message','pushsafer'))
console.log('not email', await notify.send('all but email message','!email'))
console.log('email only', await notify.send('email only message','email'))
let res = await notify.send('simple message to all services') let res = await notify.send('simple message to all services')
// let res = await notify.send({subject:'subject as message',text:'this details'}) // let res = await notify.send({subject:'subject as message',text:'this details'})
// if (res.error) console.log('errors',res) // if (res.error) console.log('errors',res)

View File

@ -4,7 +4,7 @@
"description": "notify example application", "description": "notify example application",
"main": "example.js", "main": "example.js",
"scripts": { "scripts": {
"start": "node -r esm example.js" "start": "./run"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",

7
example/run Executable file
View File

@ -0,0 +1,7 @@
#!/bin/bash
export PS_API_KEY=DLq9cXqNwBptT4CxD5cQ
export PS_DEVICE_ID=gs1992
export GMAIL_SMTP_USER=kebler.net@gmail.com
export GMAIL_SMTP_PW=seniobjybxwuhjvj
export NOTIFY_EMAIL_RECIPIENTS='kebler.net@gmail.com,dgkebler@gmail.com'
node -r esm example.js

View File

@ -1,10 +1,10 @@
{ {
"name": "@uci-utils/notify", "name": "@uci-utils/notify",
"version": "0.1.3", "version": "0.1.5",
"description": "Integrated Notification System with service plugins", "description": "Integrated Notification System with service plugins",
"main": "src/notify", "main": "src/notify",
"scripts": { "scripts": {
"example:dev": "./node_modules/.bin/nodemon -r esm ./examples/example.js", "example:dev": "./node_modules/.bin/nodemon -r esm ./example/example.js",
"test": "./node_modules/.bin/mocha -r esm --timeout 30000" "test": "./node_modules/.bin/mocha -r esm --timeout 30000"
}, },
"author": "David Kebler", "author": "David Kebler",
@ -26,5 +26,8 @@
"app-root-path": "^3.0.0", "app-root-path": "^3.0.0",
"await-to-js": "^2.1.1", "await-to-js": "^2.1.1",
"jsonfile": "^6.0.1" "jsonfile": "^6.0.1"
},
"devDependencies": {
"esm": "^3.2.25"
} }
} }

View File

@ -71,12 +71,15 @@ class Notifier {
} }
async send (msg,service,opts={}) { async send (msg,service,opts={}) {
if (service) { if (service!==null || service==='!email') {
if (this._services[service]) return await this._services[service].send(msg,opts) if (this._services[service]) return await this._services[service].send(msg,opts)
} }
let response = {} let response = {}
let services = Object.entries(this._services)
if (service==='!email') services = services.filter(service => service[0]!=='email')
// console.log('services for message send', services.map(service=>service[0]))
await Promise.all( await Promise.all(
Object.entries(this._services).map(async service => { services.map(async service => {
let res = this._disabled.includes(service[0]) ? 'disabled' : await service[1].send(msg,opts[service[0]]) let res = this._disabled.includes(service[0]) ? 'disabled' : await service[1].send(msg,opts[service[0]])
if (res.error) response.error ? response.error[service[0]] = res.error : response = { error:{[service[0]]:res.error}} if (res.error) response.error ? response.error[service[0]] = res.error : response = { error:{[service[0]]:res.error}}
response[service[0]]=res response[service[0]]=res
@ -86,7 +89,12 @@ class Notifier {
return response return response
} }
getService(name) { return this._services[name]} getServicesList() { return Object.keys(this._services) }
getService(name) {
if (name==null) return this._services
return this._services[name]
}
} // end Notifier class } // end Notifier class