add page by phone

pull/29/head
Matthew 2020-12-01 15:25:18 -05:00
parent 3c25631bd3
commit c0d5a437f6
No known key found for this signature in database
GPG Key ID: 210AF32ADE3B5C4B
5 changed files with 95 additions and 10 deletions

80
src/class/PBX.ts Normal file
View File

@ -0,0 +1,80 @@
/* eslint-disable consistent-return */
import { TextableChannel } from 'eris';
import { Client } from '.';
import PageCommand from '../commands/page';
export default class PBX {
public client: Client;
constructor(client: Client) {
this.client = client;
this.install();
this.pageDTMF();
}
protected install() {
this.client.util.ari.start('page-dtmf');
}
public pageDTMF() {
this.client.util.ari.on('StasisStart', async (event, channel) => {
if (event.application !== 'page-dtmf') return;
const message = await (<TextableChannel> this.client.guilds.get(this.client.config.guildID).channels.get('501089664040697858')).getMessage('775604192013320203');
if (!message) return channel.hangup();
const member = await this.client.db.Staff.findOne({ extension: channel.caller.number }).lean().exec();
if (!member) return channel.hangup();
const pager = await this.client.db.PagerNumber.findOne({ individualAssignID: member.userID }).lean().exec();
if (!pager) return channel.hangup();
let status = 0;
const pagerNumber: string[] = [];
const pagerCode: string[] = [];
channel.answer();
await channel.play({
media: 'sound:please-enter-the-pn',
}, undefined);
channel.on('ChannelDtmfReceived', async (ev, chan) => {
if (status === 0) {
if (ev.digit === '#') {
await channel.play({
media: 'sound:please-enter-the-pc',
}, undefined);
status = 1;
return null;
}
pagerNumber.push(ev.digit);
} else if (status === 1) {
if (ev.digit === '#') {
await channel.play({
media: 'sound:pls-hold-process-tx',
}, undefined);
const Page = <PageCommand> this.client.commands.get('page');
const page = await Page.page(pagerNumber.join(''), pager.num, pagerCode.join(''), message);
if (page.status === true) {
const playback = await channel.play({
media: 'sound:page-delivered',
}, undefined);
playback.on('PlaybackFinished', () => channel.hangup());
} else if (page.status === false) {
try {
const ch = await this.client.getDMChannel(member.userID);
if (ch) {
ch.createMessage(`***Error Occurred over PBX Paging:*** ${page.message}`);
}
} catch {
this.client.util.handleError(new Error(page.message));
}
const playback = await channel.play({
media: 'sound:an-error-has-occurred',
}, undefined);
playback.on('PlaybackFinished', () => channel.hangup());
}
return null;
}
pagerCode.push(ev.digit);
}
});
});
}
}

View File

@ -5,7 +5,7 @@ import childProcess from 'child_process';
import ARIClient from 'ari-client'; import ARIClient from 'ari-client';
import signale from 'signale'; import signale from 'signale';
import { Member, Message, Guild, PrivateChannel, GroupChannel, Role, AnyGuildChannel, WebhookPayload } from 'eris'; import { Member, Message, Guild, PrivateChannel, GroupChannel, Role, AnyGuildChannel, WebhookPayload } from 'eris';
import { Client, Command, Moderation, RichEmbed } from '.'; import { Client, Command, Moderation, PBX, RichEmbed } from '.';
import { statusMessages as emotes } from '../configs/emotes.json'; import { statusMessages as emotes } from '../configs/emotes.json';
export default class Util { export default class Util {
@ -17,6 +17,8 @@ export default class Util {
public transporter: nodemailer.Transporter; public transporter: nodemailer.Transporter;
public pbx: PBX;
public ari: ARIClient.Client; public ari: ARIClient.Client;
public tts: TextToSpeechClient; public tts: TextToSpeechClient;
@ -45,6 +47,8 @@ export default class Util {
process.env.GOOGLE_APPLICATION_CREDENTIALS = `${__dirname}/../../google.json`; process.env.GOOGLE_APPLICATION_CREDENTIALS = `${__dirname}/../../google.json`;
this.tts = new GoogleTTS.TextToSpeechClient(); this.tts = new GoogleTTS.TextToSpeechClient();
this.pbx = new PBX(this.client);
} }
get emojis() { get emojis() {

View File

@ -4,6 +4,7 @@ export { default as Command } from './Command';
export { default as Event } from './Event'; export { default as Event } from './Event';
export { default as LocalStorage } from './LocalStorage'; export { default as LocalStorage } from './LocalStorage';
export { default as Moderation } from './Moderation'; export { default as Moderation } from './Moderation';
export { default as PBX } from './PBX';
export { default as Queue } from './Queue'; export { default as Queue } from './Queue';
export { default as RichEmbed } from './RichEmbed'; export { default as RichEmbed } from './RichEmbed';
export { default as Route } from './Route'; export { default as Route } from './Route';

View File

@ -218,10 +218,6 @@ export default class Page extends Command {
channel.hangup(); channel.hangup();
}); });
}); });
} catch (err) {
this.client.util.signale.log(`Unable to Dial ${member.extension} | ${err}`);
}
const recipient = this.mainGuild.members.get(recipientEntry.individualAssignID); const recipient = this.mainGuild.members.get(recipientEntry.individualAssignID);
const sender = this.mainGuild.members.get(senderEntry.individualAssignID); const sender = this.mainGuild.members.get(senderEntry.individualAssignID);
@ -230,6 +226,9 @@ export default class Page extends Command {
} else { } else {
this.logPage({ number: senderNumber, user: `${sender.username}#${sender.discriminator}` }, { number: recipientNumber, user: `${recipient.username}#${recipient.discriminator}` }, 'phone', code); this.logPage({ number: senderNumber, user: `${sender.username}#${sender.discriminator}` }, { number: recipientNumber, user: `${recipient.username}#${recipient.discriminator}` }, 'phone', code);
} }
} catch (err) {
this.client.util.signale.log(`Unable to Dial ${member.extension} | ${err}`);
}
} }
this.client.db.Stat.updateOne({ name: 'pages' }, { $inc: { value: 1 } }).exec(); this.client.db.Stat.updateOne({ name: 'pages' }, { $inc: { value: 1 } }).exec();
return { status: true, message: `Page to \`${recipientNumber}\` sent.` }; return { status: true, message: `Page to \`${recipientNumber}\` sent.` };

1
types/index.d.ts vendored
View File

@ -9,4 +9,5 @@ export declare interface Config {
internalKey: string; internalKey: string;
stripeKey: string; stripeKey: string;
stripeSubSigningSecret: string; stripeSubSigningSecret: string;
ariClientKey: string;
} }