node-red-contrib-notifyimage
Version:
sending text,image via line notify
79 lines (70 loc) • 2.68 kB
JavaScript
module.exports = (RED) => {
'use strict';
var LineAPI = require('line-api');
const main = function(config){
RED.nodes.createNode(this, config);
this.config = config;
const LINE_TOKEN = config.AccToken;
var node = this;
const notify = new LineAPI.Notify({
token: LINE_TOKEN
})
function urlRespond(data)
{
// data is object { status: 200, message: 'ok' }
var b1 = (data.status===200);
node.send({payload:b1, respond:data});
node.status({fill:b1?"green":"red", shape:"dot", text:b1?"ok":"ng"});
};
node.on('input', async (msg) => {
var msgs = node.config.tmsg;
if(msg.hasOwnProperty('payload'))
{
if(msg.payload !== '')
msgs = msg.payload;
}
if(msgs === '')
{
node.status({fill:"red", shape:"dot", text:"*message"});
return;
}
var data = {message:msgs};
var imgfile1 = node.config.imgfile;
if(msg.hasOwnProperty('imagefile'))
{
imgfile1 = msg.imagefile;
}
if(imgfile1 != '')
{
const isImg = imgfile1.lastIndexOf('.png')>=0 || imgfile1.lastIndexOf('.jpg')>=0;
if(!isImg)
{
node.status({fill:"red", shape:"dot", text:"*png,jpg"});
return;
}
const isUrl = imgfile1.lastIndexOf('http')>=0;
if (isUrl)
{
// remote Url
data['image'] = {
fullsize:imgfile1,
thumbnail:imgfile1
};
}
else
{
data['image'] = imgfile1;
}
}
notify.send(data).then(urlRespond);
//notify.send({
// message: msg,
// sticker: 'smile', // shorthand
// sticker : { packageId: 1, id: 2 } // exact ids
// image: 'd:/temp/p1.png' // local file
// image: { fullsize: 'http://example.com/1024x1024.jpg', thumbnail: 'http://example.com/240x240.jpg' } // remote url
//}).then(console.log)
});
}
RED.nodes.registerType("NotifyImage", main);
}