Implemented max twilio messages per day and fixed slider decimal places.
This commit is contained in:
@@ -141,6 +141,7 @@ export interface ConfigTwilio {
|
||||
fromNumber: string;
|
||||
toNumbers: string[];
|
||||
aggregateTimeout: number;
|
||||
maxMessagesPerDay: number;
|
||||
}
|
||||
|
||||
export interface ConfigSupport {
|
||||
|
||||
@@ -33,6 +33,7 @@ export const DefaultConfiguration: Config = {
|
||||
fromNumber: '',
|
||||
toNumbers: [],
|
||||
aggregateTimeout: 15000,
|
||||
maxMessagesPerDay: 10,
|
||||
},
|
||||
support: {
|
||||
telephone: '+31613392837',
|
||||
|
||||
@@ -1,16 +1,47 @@
|
||||
import { join } from 'path';
|
||||
import { Main } from './Main';
|
||||
import { pathExists, readJSON, writeJSON } from 'fs-extra';
|
||||
|
||||
const PREFIX = '[Twilio]';
|
||||
export class TwilioHandler {
|
||||
private _Main: Main;
|
||||
|
||||
counter: {
|
||||
date: string;
|
||||
count: number;
|
||||
} = {
|
||||
date: new Date().toLocaleDateString('nl-NL'),
|
||||
count: 0,
|
||||
};
|
||||
|
||||
client;
|
||||
|
||||
constructor(Main: Main) {
|
||||
this._Main = Main;
|
||||
}
|
||||
|
||||
load() {
|
||||
canSendMessage() {
|
||||
const maxMessages = this._Main.Config.twilio.maxMessagesPerDay;
|
||||
return this.counter.count < maxMessages;
|
||||
}
|
||||
|
||||
async incrementCounter() {
|
||||
const today = new Date().toLocaleDateString('nl-NL');
|
||||
if (this.counter.date !== today) {
|
||||
this.counter.date = today;
|
||||
this.counter.count = 0;
|
||||
}
|
||||
this.counter.count++;
|
||||
await writeJSON(join(this._Main.dataPath, 'twilio.json'), this.counter);
|
||||
}
|
||||
|
||||
async load() {
|
||||
const twilioConfigPath = join(this._Main.dataPath, 'twilio.json');
|
||||
const twilioConfigExists = await pathExists(twilioConfigPath);
|
||||
if (twilioConfigExists) {
|
||||
this.counter = await readJSON(twilioConfigPath);
|
||||
}
|
||||
|
||||
this.client = require('twilio')(
|
||||
this._Main.Config.twilio.accountSid,
|
||||
this._Main.Config.twilio.authToken
|
||||
@@ -39,16 +70,21 @@ export class TwilioHandler {
|
||||
.join('\n');
|
||||
this.errorLog = [];
|
||||
|
||||
if (!this.canSendMessage())
|
||||
return console.log(PREFIX, 'Max messages per day reached');
|
||||
await this.incrementCounter();
|
||||
|
||||
console.log(PREFIX, `Sending to Twilio:\n`, errorMessage);
|
||||
|
||||
const promises = this._Main.Config.twilio.toNumbers.map(
|
||||
(toNumber) => this.sendMessage(toNumber, errorMessage)
|
||||
);
|
||||
await Promise.all(promises);
|
||||
}, 15000);
|
||||
}, this._Main.Config.twilio.aggregateTimeout);
|
||||
}
|
||||
|
||||
sendMessage(to: string, message: string) {
|
||||
return;
|
||||
return new Promise<boolean>((resolve) => {
|
||||
this.client.messages
|
||||
.create({
|
||||
|
||||
Reference in New Issue
Block a user