Implemented max twilio messages per day and fixed slider decimal places.

This commit is contained in:
2025-10-24 17:12:46 +02:00
parent 7ce9003621
commit 33c3080867
7 changed files with 102 additions and 7 deletions

62
dist/Twilio.js vendored
View File

@@ -37,15 +37,65 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TwilioHandler = void 0;
var path_1 = require("path");
var fs_extra_1 = require("fs-extra");
var PREFIX = '[Twilio]';
var TwilioHandler = /** @class */ (function () {
function TwilioHandler(Main) {
this.counter = {
date: new Date().toLocaleDateString('nl-NL'),
count: 0,
};
this.lastErrors = new Map();
this.errorLog = [];
this._Main = Main;
}
TwilioHandler.prototype.canSendMessage = function () {
var maxMessages = this._Main.Config.twilio.maxMessagesPerDay;
return this.counter.count < maxMessages;
};
TwilioHandler.prototype.incrementCounter = function () {
return __awaiter(this, void 0, void 0, function () {
var today;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
today = new Date().toLocaleDateString('nl-NL');
if (this.counter.date !== today) {
this.counter.date = today;
this.counter.count = 0;
}
this.counter.count++;
return [4 /*yield*/, (0, fs_extra_1.writeJSON)((0, path_1.join)(this._Main.dataPath, 'twilio.json'), this.counter)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
TwilioHandler.prototype.load = function () {
this.client = require('twilio')(this._Main.Config.twilio.accountSid, this._Main.Config.twilio.authToken);
return __awaiter(this, void 0, void 0, function () {
var twilioConfigPath, twilioConfigExists, _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
twilioConfigPath = (0, path_1.join)(this._Main.dataPath, 'twilio.json');
return [4 /*yield*/, (0, fs_extra_1.pathExists)(twilioConfigPath)];
case 1:
twilioConfigExists = _b.sent();
if (!twilioConfigExists) return [3 /*break*/, 3];
_a = this;
return [4 /*yield*/, (0, fs_extra_1.readJSON)(twilioConfigPath)];
case 2:
_a.counter = _b.sent();
_b.label = 3;
case 3:
this.client = require('twilio')(this._Main.Config.twilio.accountSid, this._Main.Config.twilio.authToken);
return [2 /*return*/];
}
});
});
};
TwilioHandler.prototype.resetError = function (category) {
return __awaiter(this, void 0, void 0, function () {
@@ -78,21 +128,27 @@ var TwilioHandler = /** @class */ (function () {
.map(function (error) { return "- ".concat(error); })
.join('\n');
this.errorLog = [];
if (!this.canSendMessage())
return [2 /*return*/, console.log(PREFIX, 'Max messages per day reached')];
return [4 /*yield*/, this.incrementCounter()];
case 1:
_a.sent();
console.log(PREFIX, "Sending to Twilio:\n", errorMessage);
promises = this._Main.Config.twilio.toNumbers.map(function (toNumber) { return _this.sendMessage(toNumber, errorMessage); });
return [4 /*yield*/, Promise.all(promises)];
case 1:
case 2:
_a.sent();
return [2 /*return*/];
}
});
}); }, 15000);
}); }, this._Main.Config.twilio.aggregateTimeout);
return [2 /*return*/];
});
});
};
TwilioHandler.prototype.sendMessage = function (to, message) {
var _this = this;
return;
return new Promise(function (resolve) {
_this.client.messages
.create({