Skip to content

Commit

Permalink
add lookup node
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiez committed Oct 26, 2021
1 parent 235ab60 commit c9a72ec
Show file tree
Hide file tree
Showing 14 changed files with 308 additions and 146 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
3.1.0
==================
* add lookup node

3.0.0
==================
* fixed nodes paths
Expand Down
30 changes: 20 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
![alt text](https://www.sms77.io/wp-content/uploads/2019/07/sms77-Logo-400x79.png "sms77")
# nodered-contrib-sms77
![sms77 logo](https://www.sms77.io/wp-content/uploads/2019/07/sms77-Logo-400x79.png "sms77")

<a href="http://nodered.org" target="_blank" rel="noopener noreferrer">Node-RED</a> node collection for sending SMS/Text2Speech message(s) via the Sms77.io gateway.
# nodered-contrib-sms77

[Node-RED](http://nodered.org) node collection
for [sending SMS](https://www.sms77.io/en/products/send-sms/)
, [text-to-speech calls](https://www.sms77.io/en/products/voice/)
and [number validation](https://www.sms77.io/en/products/number-validation/)
via [sms77](https://www.sms77.io/).

## Installation

Run the following command in your Node-RED user directory - usually `~/.node-red`:

<dl>
<dt>NPM</dt>
<dd><code>npm install nodered-contrib-sms77</code></dd><br>
<dt>Yarn</dt>
<dd><code>yarn add nodered-contrib-sms77</code></dd>
</dl>
**NPM**
`npm install nodered-contrib-sms77`

**Yarn**
`yarn add nodered-contrib-sms77`

### Request Options

Visit our <a href="http://www.sms77.io/en/docs/gateway/http-api/">API Documentation</a> for a detailed request description.
Visit our [API Documentation](https://www.sms77.io/en/docs/gateway/http-api/) for a
detailed request description.

#### Support

Need help? Feel free to [contact us](https://www.sms77.io/en/company/contact/).

[![MIT](https://img.shields.io/badge/License-MIT-teal.svg)](LICENSE)
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"author": "sms77 e.K.",
"author": "sms77 e.K. <[email protected]> (https://www.sms77.io/)",
"bugs": {
"email": "[email protected]",
"url": "https://github.com/sms77io/nodered-contrib-sms77/issues"
},
"dependencies": {
Expand All @@ -13,16 +14,20 @@
"node-red": {
"nodes": {
"sms77-config": "src/nodes/config/config.js",
"sms77-lookup": "src/nodes/lookup/lookup.js",
"sms77-sms": "src/nodes/sms/sms.js",
"sms77-voice": "src/nodes/voice/voice.js"
}
},
"homepage": "https://github.com/sms77io/nodered-contrib-sms77#readme",
"keywords": [
"sms",
"text2speech",
"cnam",
"hlr",
"mnp",
"node-red",
"sms77"
"sms",
"sms77",
"text2speech"
],
"license": "MIT",
"repository": {
Expand All @@ -32,5 +37,5 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"version": "3.0.0"
"version": "3.1.0"
}
34 changes: 34 additions & 0 deletions src/BaseMessageNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const globalThis = require('globalthis')()
if (!globalThis.fetch) globalThis.fetch = require('node-fetch')
const Util = require('./Util')
const NodeUtil = require('./NodeUtil')

module.exports = function(node, RED, config) {
try {
RED.nodes.createNode(node, config)

node.constructor.CFG = config
node.constructor.CLIENT = Util.initClient(
RED.nodes.getNode(config.config).credentials.apiKey)

node.status({fill: 'blue', shape: 'ring', text: `${node.name} connected`})

node._done = (done, error, msg) => done
? done(error) // Use done if defined (1.0+)
: node.error(error, msg) // Fallback to node.error (pre-1.0)

node._util = new NodeUtil(node)

node._emptyStringFallback = node._util.emptyStringFallback

node._errorHandler = (done, msg) => node._util.errorHandler(done, msg)

node._onSuccess = node._util.onSuccess

node.on('input', node._util.onInput)
} catch (err) {
console.log({err})

node.error(err)
}
}
61 changes: 0 additions & 61 deletions src/BaseNode.js

This file was deleted.

32 changes: 32 additions & 0 deletions src/NodeUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const Util = require('./Util')

module.exports = class NodeUtil {
constructor(node) {
this.node = node
}

emptyStringFallback = (key, value = null) => {
const cfg = this.node.constructor.CFG
if (!key in cfg) return value
return '' === (cfg[key] || '') ? value : cfg[key]
}

status = (text, fill = 'yellow', shape = 'dot') => {
this.node.status({fill, shape, text})
}

onSuccess = (sent, failed, send, msg, payload, done) => {
this.status(`${sent} sent | ${failed} failed`)
send({...msg, payload})
if (done) done() // Check done exists (1.0+)
}

onInput = async (msg, send, done) => {
if (!send) send = () => this.node.send.apply(this.node, [msg, send, done]) // If this is pre-1.0, 'send' will be undefined, so fallback to node.send
await this.node._onInput(msg, send, done)
}

errorHandler = (done, msg) => {
return e => this.node._done(done, Util.stringify(e), msg)
}
}
13 changes: 13 additions & 0 deletions src/Util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const Sms77Client = require('sms77-client')

module.exports = class Util {
static initClient(apiKey) {
return new Sms77Client(apiKey, 'node-red')
}

static stringify(value) {
if (typeof value === 'string') return value
if (typeof value.toString === 'function') return value.toString()
return JSON.stringify(value)
}
}
17 changes: 17 additions & 0 deletions src/nodes/lookup/locales/en/lookup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script data-help-name='sms77-lookup' type='text/html'>
Lookup phone numbers via sms77.io.
<hr/>

<p><b>Properties</b></p>

<code>Number(s)</code> Comma separated list of mobile numbers to look up.
Defaults to <i>msg.topic</i> if not set.
<br/>

<code>Type</code> CNAM, HLR, MNP or Format.
<br/>

<code>JSON</code> Set to <i>true</i> if you want a detailed response in JSON format.
Defaults to <i>false</i>.
<br/>
</script>
11 changes: 11 additions & 0 deletions src/nodes/lookup/locales/en/lookup.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"sms77-lookup": {
"cnam": "CNAM",
"config": "Config",
"format": "Format",
"hlr": "HLR",
"json": "Return JSON?",
"mnp": "MNP",
"type": "Type"
}
}
68 changes: 68 additions & 0 deletions src/nodes/lookup/lookup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<script type='text/javascript'>
try {
RED.nodes.registerType('sms77-lookup', {
category: 'mobile',
color: '#01C965',
defaults: {
config: {
required: true,
type: 'sms77-config',
},
json: {},
name: {value: 'sms77-lookup'},
numbers: {},
lookupType: {required: true},
},
icon: 'watch.png',
inputs: 1,
outputs: 1,
})
} catch (e) {
console.error('sms77_lookup_RegisterTpl', e)
}
</script>

<script data-template-name='sms77-lookup' type='text/html'>
<div class='form-row'>
<label for='node-input-config' data-i18n='sms77-lookup.config'><i
class='fa fa-user'></i></label>
<input id='node-input-config'>
</div>

<div class='form-row'>
<label for='node-input-numbers' data-i18n='sms77-lookup.numbers'><i
class='fa fa-mobile'
style='font-size: 160%'></i></label>
<input id='node-input-numbers' placeholder='msg.topic'/>
</div>

<div class='form-row'>
<label for='node-input-lookupType' data-i18n='sms77-lookup.type'><i
class='fa fa-microchip'></i></label>
<select id='node-input-lookupType' required>
<option value='cnam' data-i18n='sms77-lookup.cnam'></option>
<option value='format' data-i18n='sms77-lookup.format'></option>
<option value='hlr' data-i18n='sms77-lookup.hlr'></option>
<option value='mnp' data-i18n='sms77-lookup.mnp'></option>
</select>
</div>

<div class='form-row'>
<label for='node-input-json' data-i18n='sms77-lookup.json'><i
class='fa fa-microchip'></i></label>
<select id='node-input-json'>
<option value='true'
data-i18n='nodered-contrib-sms77/sms77-config:sms77-config.true'>
</option>
<option value='false'
data-i18n='nodered-contrib-sms77/sms77-config:sms77-config.false'>
</option>
</select>
</div>

<div class='form-row'>
<label for='node-input-name' data-i18n='sms77-lookup.name'>
<i class='fa fa-tag'></i></label>
<input id='node-input-name' placeholder='sms77-lookup'/>
</div>
</script>
31 changes: 31 additions & 0 deletions src/nodes/lookup/lookup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const BaseNode = require('../../BaseMessageNode')
const Util = require('../../Util')

module.exports = function(RED) {
'use strict'

class Sms77LookupNode {
constructor(config) {
BaseNode(this, RED, config)
}

async _onInput(msg, send, done) {
const params = {
json: 'true' === Sms77LookupNode.CFG.json,
number: this._emptyStringFallback('numbers', msg.topic),
type: Sms77LookupNode.CFG.lookupType,
}

try {
const response = await Sms77LookupNode.CLIENT.lookup(params)
this._util.status(Util.stringify(response))
send({...msg})
if (done) done() // Check done exists (1.0+)
} catch (e) {
this._errorHandler(done, msg)
}
}
}

RED.nodes.registerType('sms77-lookup', Sms77LookupNode)
}
Loading

0 comments on commit c9a72ec

Please sign in to comment.