@theotherwillembotha/node-red-whatsapp 0.0.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/Nodes.js ADDED
@@ -0,0 +1,24 @@
1
+
2
+ /*
3
+
4
+ This file was automatically generated using the NODERED Core utility.
5
+ Any modifications to his file will be overwritten the next time the code is regenerated.
6
+
7
+ You have been warned.
8
+
9
+ */
10
+ "use strict";
11
+ const NodeManager = require("@theotherwillembotha/node-red-plugincore").NodeManager;
12
+ const WhatsappAccountConfigNode = require("@theotherwillembotha/nodered_whatsapp").WhatsappAccountConfigNode;
13
+ const WhatsappGroupConfigNode = require("@theotherwillembotha/nodered_whatsapp").WhatsappGroupConfigNode;
14
+ const WhatsappSendMessageNode = require("@theotherwillembotha/nodered_whatsapp").WhatsappSendMessageNode;
15
+ const WhatsappReceiveMessageNode = require("@theotherwillembotha/nodered_whatsapp").WhatsappReceiveMessageNode;
16
+ module.exports = (RED) => {
17
+ let manager = new NodeManager(RED);
18
+ manager.registerNodeType("WhatsappAccountConfigNode", WhatsappAccountConfigNode);
19
+ manager.registerNodeType("WhatsappGroupConfigNode", WhatsappGroupConfigNode);
20
+ manager.registerNodeType("WhatsappSendMessageNode", WhatsappSendMessageNode);
21
+ manager.registerNodeType("WhatsappReceiveMessageNode", WhatsappReceiveMessageNode);
22
+ }
23
+
24
+
@@ -0,0 +1,70 @@
1
+
2
+ /*
3
+
4
+ This file was automatically generated using the NODERED Core utility.
5
+ Any modifications to his file will be overwritten the next time the code is regenerated.
6
+
7
+ You have been warned.
8
+
9
+ */
10
+ "use strict";
11
+ const runtime = require("node-red").runtime;
12
+ const WhatsappService = require("./whatsapp/service/WhatsappService").WhatsappService;
13
+
14
+ module.exports = function (RED) {
15
+
16
+ // 1. make a list of all the plugins that need to be installed.
17
+ let pluginList = [WhatsappService]
18
+ .map(service => service.getServiceDescriptor())
19
+ .filter(plugin => !RED.plugins.get(plugin.id()));
20
+
21
+ // 2. register a listener if there are any plugins in the pluginList.
22
+ if(pluginList.length > 0){
23
+ RED.events.on('registry:plugin-added', async pluginID => {
24
+
25
+ let addedPlugin = pluginList.find(plugin => plugin.id() === pluginID);
26
+ let plugin = RED.plugins.get(pluginID);
27
+
28
+ if(addedPlugin && plugin.instantiate && !plugin.instance){
29
+ console.log("Deploying instance of: " + plugin.id);
30
+ plugin.instance = new plugin.class();
31
+
32
+ // instantiate the plugin.
33
+ await plugin.instance.init(RED);
34
+
35
+ // publish an event that it has ben deployed.
36
+ RED.events.emit("plugin.instantiated", pluginID);
37
+
38
+ pluginList.splice(pluginList.findIndex(current => current.id() === addedPlugin.id()), 1);
39
+
40
+ // register a flow deployment listener.
41
+ let startupDeployment = true;
42
+ RED.events.on('runtime-event', async (event) => {
43
+ // startup deployment.
44
+ if ("runtime-deploy" === event?.id && startupDeployment) {
45
+ //console.log("STARTUP DEPLOYMENT");
46
+ startupDeployment = false;
47
+ const flows = await runtime.flows.getFlows({});
48
+ await plugin.instance.onDeploy(flows);
49
+ }
50
+
51
+ // stopping flows on redeployment.
52
+ if ("runtime-state" === event?.id && event?.payload?.state === "stop" && event?.payload?.deploy) {
53
+ //console.log("REDEPLOY!");
54
+ const flows = await runtime.flows.getFlows({});
55
+ await plugin.instance.onDeploy(flows);
56
+ }
57
+ });
58
+ }
59
+ });
60
+ }
61
+
62
+ // 3. register the plugins.
63
+ [...pluginList].forEach(plugin => {
64
+ RED.plugins.registerPlugin(plugin.id(), {
65
+ type: plugin.type(),
66
+ instantiate:true,
67
+ class: plugin.clazz()
68
+ });
69
+ })
70
+ };
Binary file
package/build/index.js ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./whatsapp/service/WhatsappService"), exports);
18
+ __exportStar(require("./whatsapp/node/WhatsappAccountConfigNode"), exports);
19
+ __exportStar(require("./whatsapp/node/WhatsappGroupConfigNode"), exports);
20
+ __exportStar(require("./whatsapp/node/WhatsappSendMessageNode"), exports);
21
+ __exportStar(require("./whatsapp/node/WhatsappReceiveMessageNode"), exports);
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.WhatsappAccountConfigNode = void 0;
13
+ const node_red_plugincore_1 = require("@theotherwillembotha/node-red-plugincore");
14
+ const WhatsappService_1 = require("../service/WhatsappService");
15
+ let WhatsappAccountConfigNode = class WhatsappAccountConfigNode extends node_red_plugincore_1.ConfigNode {
16
+ constructor(node, config) {
17
+ super(node, config);
18
+ // get a reference to the linked Account.
19
+ this.client = WhatsappService_1.WhatsappService.getClient(config.localConnectionId);
20
+ }
21
+ getGroup(groupId) {
22
+ return this.client ? this.client.getGroupClient(groupId) : undefined;
23
+ }
24
+ };
25
+ exports.WhatsappAccountConfigNode = WhatsappAccountConfigNode;
26
+ exports.WhatsappAccountConfigNode = WhatsappAccountConfigNode = __decorate([
27
+ (0, node_red_plugincore_1.NodeDescription)({
28
+ id: "WhatsappAccountConfigNode",
29
+ name: "Whatsapp Config Node",
30
+ group: "config",
31
+ sourceFile: node_red_plugincore_1.SourceUtility.getSourcePath("/build/", "/src/") + "WhatsappAccountConfigNode.html",
32
+ package: "@theotherwillembotha/nodered_whatsapp",
33
+ tags: ["Whatsapp"]
34
+ }),
35
+ __metadata("design:paramtypes", [Object, Object])
36
+ ], WhatsappAccountConfigNode);
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.WhatsappGroupConfigNode = void 0;
13
+ const node_red_plugincore_1 = require("@theotherwillembotha/node-red-plugincore");
14
+ let WhatsappGroupConfigNode = class WhatsappGroupConfigNode extends node_red_plugincore_1.ConfigNode {
15
+ constructor(node, config) {
16
+ super(node, config);
17
+ this.accountconfigNode = node_red_plugincore_1.NodeManager.RED.nodes.getNode(config.accountConfig).node();
18
+ this.group = this.accountconfigNode.getGroup(config.groupId);
19
+ }
20
+ send(message) {
21
+ console.log("Sending message to ", this.group.id());
22
+ this.group.sendMessage(message);
23
+ }
24
+ subscribe(subscription) {
25
+ this.group.subscribe(subscription);
26
+ return subscription;
27
+ }
28
+ unsubscribe(subscription) {
29
+ console.log("unsubscribing from", this.group.id());
30
+ this.group.unsubscribe(subscription);
31
+ return;
32
+ }
33
+ };
34
+ exports.WhatsappGroupConfigNode = WhatsappGroupConfigNode;
35
+ exports.WhatsappGroupConfigNode = WhatsappGroupConfigNode = __decorate([
36
+ (0, node_red_plugincore_1.NodeDescription)({
37
+ id: "WhatsappGroupConfigNode",
38
+ name: "Whatsapp Group Config Node",
39
+ group: "config",
40
+ sourceFile: node_red_plugincore_1.SourceUtility.getSourcePath("/build/", "/src/") + "WhatsappGroupConfigNode.html",
41
+ package: "@theotherwillembotha/nodered_whatsapp",
42
+ tags: ["Whatsapp"]
43
+ }),
44
+ __metadata("design:paramtypes", [Object, Object])
45
+ ], WhatsappGroupConfigNode);
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.WhatsappReceiveMessageNode = void 0;
13
+ const node_red_plugincore_1 = require("@theotherwillembotha/node-red-plugincore");
14
+ const node_red_plugincore_2 = require("@theotherwillembotha/node-red-plugincore");
15
+ const node_red_plugincore_3 = require("@theotherwillembotha/node-red-plugincore");
16
+ let WhatsappReceiveMessageNode = class WhatsappReceiveMessageNode extends node_red_plugincore_1.BaseNode {
17
+ constructor(node, config) {
18
+ super(node, config);
19
+ let accepts = [];
20
+ try {
21
+ accepts = JSON.parse(config.accepts || "[]");
22
+ }
23
+ catch (e) { }
24
+ const enabledTypes = new Set(accepts.filter(a => a.enabled).map(a => a.id));
25
+ this.groupNode = node_red_plugincore_1.NodeManager.RED.nodes.getNode(config.groupConfig).node();
26
+ this.subscription = this.groupNode.subscribe((message) => {
27
+ if (enabledTypes.size > 0 && !enabledTypes.has(message.type))
28
+ return;
29
+ if (!config.acceptOwnMessages && message.sender?.me)
30
+ return;
31
+ this.log.log(message);
32
+ this.counter.inc();
33
+ const output = {};
34
+ if (config.eventOutputType === 'flow') {
35
+ this.node().context().flow.set(config.eventOutputKey, message);
36
+ }
37
+ else if (config.eventOutputType === 'global') {
38
+ this.node().context().global.set(config.eventOutputKey, message);
39
+ }
40
+ else {
41
+ this.setPath(output, config.eventOutputKey, message);
42
+ }
43
+ this.node().send(output);
44
+ });
45
+ this.node().on("close", () => this.groupNode.unsubscribe(this.subscription));
46
+ }
47
+ setPath(obj, path, value) {
48
+ const parts = path.split('.');
49
+ let cur = obj;
50
+ for (let i = 0; i < parts.length - 1; i++) {
51
+ if (typeof cur[parts[i]] !== 'object' || cur[parts[i]] === null) {
52
+ cur[parts[i]] = {};
53
+ }
54
+ cur = cur[parts[i]];
55
+ }
56
+ cur[parts[parts.length - 1]] = value;
57
+ }
58
+ };
59
+ exports.WhatsappReceiveMessageNode = WhatsappReceiveMessageNode;
60
+ __decorate([
61
+ (0, node_red_plugincore_3.Logger)(),
62
+ __metadata("design:type", node_red_plugincore_3.Log)
63
+ ], WhatsappReceiveMessageNode.prototype, "log", void 0);
64
+ __decorate([
65
+ (0, node_red_plugincore_2.Metrics)({ name: "counter", type: node_red_plugincore_2.MetricType.Counter, description: "number of messages received" }),
66
+ __metadata("design:type", node_red_plugincore_2.CounterMetric)
67
+ ], WhatsappReceiveMessageNode.prototype, "counter", void 0);
68
+ exports.WhatsappReceiveMessageNode = WhatsappReceiveMessageNode = __decorate([
69
+ (0, node_red_plugincore_1.NodeDescription)({
70
+ id: "WhatsappReceiveMessageNode",
71
+ name: "Whatsapp Receive Message Node",
72
+ group: "whatsapp",
73
+ sourceFile: node_red_plugincore_1.SourceUtility.getSourcePath("/build/", "/src/") + "WhatsappReceiveMessageNode.html",
74
+ package: "@theotherwillembotha/nodered_whatsapp",
75
+ templates: [
76
+ { template: node_red_plugincore_3.LoggerTemplate, config: {} },
77
+ { template: node_red_plugincore_2.MetricsTemplate, config: {} },
78
+ ],
79
+ tags: ["Whatsapp"]
80
+ }),
81
+ __metadata("design:paramtypes", [Object, Object])
82
+ ], WhatsappReceiveMessageNode);
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.WhatsappSendMessageNode = void 0;
16
+ const node_red_plugincore_1 = require("@theotherwillembotha/node-red-plugincore");
17
+ const node_red_plugincore_2 = require("@theotherwillembotha/node-red-plugincore");
18
+ const node_red_plugincore_3 = require("@theotherwillembotha/node-red-plugincore");
19
+ const handlebars_1 = __importDefault(require("handlebars"));
20
+ let WhatsappSendMessageNode = class WhatsappSendMessageNode extends node_red_plugincore_1.BaseNode {
21
+ constructor(node, config) {
22
+ super(node, config);
23
+ this.groupNode = node_red_plugincore_1.NodeManager.RED.nodes.getNode(config.groupConfig).node();
24
+ }
25
+ resolveValue(field, message) {
26
+ switch (field.type) {
27
+ case "str":
28
+ return handlebars_1.default.compile(field.value)({ msg: message });
29
+ case "msg":
30
+ return field.value.split(".").reduce((obj, key) => obj?.[key], message);
31
+ case "flow":
32
+ return this.node().context().flow.get(field.value);
33
+ case "global":
34
+ return this.node().context().global.get(field.value);
35
+ default:
36
+ return undefined;
37
+ }
38
+ }
39
+ onMessageReceived(message) {
40
+ this.counter.inc();
41
+ let fields = [];
42
+ try {
43
+ fields = JSON.parse(this.config().payloads || "[]");
44
+ }
45
+ catch (e) { }
46
+ let payload = {};
47
+ for (let field of fields) {
48
+ if (!field.enabled)
49
+ continue;
50
+ let resolved = this.resolveValue(field, message);
51
+ if (field.id === "text")
52
+ payload.text = resolved != null ? String(resolved) : undefined;
53
+ if (field.id === "image")
54
+ payload.image = resolved;
55
+ }
56
+ let logPayload = {};
57
+ if (payload.text)
58
+ logPayload.text = payload.text;
59
+ if (payload.image)
60
+ logPayload.image = payload.image?.length;
61
+ this.log.log(logPayload);
62
+ console.log(payload);
63
+ this.groupNode.send(payload);
64
+ }
65
+ };
66
+ exports.WhatsappSendMessageNode = WhatsappSendMessageNode;
67
+ __decorate([
68
+ (0, node_red_plugincore_3.Logger)(),
69
+ __metadata("design:type", node_red_plugincore_3.Log)
70
+ ], WhatsappSendMessageNode.prototype, "log", void 0);
71
+ __decorate([
72
+ (0, node_red_plugincore_2.Metrics)({ name: "counter", type: node_red_plugincore_2.MetricType.Counter, description: "number of messages sent" }),
73
+ __metadata("design:type", node_red_plugincore_2.CounterMetric)
74
+ ], WhatsappSendMessageNode.prototype, "counter", void 0);
75
+ __decorate([
76
+ (0, node_red_plugincore_1.onInput)(),
77
+ __metadata("design:type", Function),
78
+ __metadata("design:paramtypes", [Object]),
79
+ __metadata("design:returntype", void 0)
80
+ ], WhatsappSendMessageNode.prototype, "onMessageReceived", null);
81
+ exports.WhatsappSendMessageNode = WhatsappSendMessageNode = __decorate([
82
+ (0, node_red_plugincore_1.NodeDescription)({
83
+ id: "WhatsappSendMessageNode",
84
+ name: "Whatsapp Send Message Node",
85
+ group: "whatsapp",
86
+ sourceFile: node_red_plugincore_1.SourceUtility.getSourcePath("/build/", "/src/") + "WhatsappSendMessageNode.html",
87
+ package: "@theotherwillembotha/nodered_whatsapp",
88
+ templates: [
89
+ { template: node_red_plugincore_3.LoggerTemplate, config: {} },
90
+ { template: node_red_plugincore_2.MetricsTemplate, config: {} },
91
+ ],
92
+ tags: ["Whatsapp"]
93
+ }),
94
+ __metadata("design:paramtypes", [Object, Object])
95
+ ], WhatsappSendMessageNode);