diff --git a/src/class/PBX.ts b/src/class/PBX.ts new file mode 100644 index 0000000..7d0cf14 --- /dev/null +++ b/src/class/PBX.ts @@ -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 ( 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 = 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); + } + }); + }); + } +} diff --git a/src/class/Util.ts b/src/class/Util.ts index 1341d69..0aceafb 100644 --- a/src/class/Util.ts +++ b/src/class/Util.ts @@ -5,7 +5,7 @@ import childProcess from 'child_process'; import ARIClient from 'ari-client'; import signale from 'signale'; 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'; export default class Util { @@ -17,6 +17,8 @@ export default class Util { public transporter: nodemailer.Transporter; + public pbx: PBX; + public ari: ARIClient.Client; public tts: TextToSpeechClient; @@ -45,6 +47,8 @@ export default class Util { process.env.GOOGLE_APPLICATION_CREDENTIALS = `${__dirname}/../../google.json`; this.tts = new GoogleTTS.TextToSpeechClient(); + + this.pbx = new PBX(this.client); } get emojis() { diff --git a/src/class/index.ts b/src/class/index.ts index 222802e..08737a5 100644 --- a/src/class/index.ts +++ b/src/class/index.ts @@ -4,6 +4,7 @@ export { default as Command } from './Command'; export { default as Event } from './Event'; export { default as LocalStorage } from './LocalStorage'; export { default as Moderation } from './Moderation'; +export { default as PBX } from './PBX'; export { default as Queue } from './Queue'; export { default as RichEmbed } from './RichEmbed'; export { default as Route } from './Route'; diff --git a/src/commands/page.ts b/src/commands/page.ts index fc199bd..2cda747 100644 --- a/src/commands/page.ts +++ b/src/commands/page.ts @@ -218,18 +218,17 @@ export default class Page extends Command { channel.hangup(); }); }); + const recipient = this.mainGuild.members.get(recipientEntry.individualAssignID); + const sender = this.mainGuild.members.get(senderEntry.individualAssignID); + + if (!recipient || !sender) { + this.logPage({ number: senderNumber, user: 'N/A' }, { number: recipientNumber, user: 'N/A' }, 'phone', code); + } else { + 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}`); } - - const recipient = this.mainGuild.members.get(recipientEntry.individualAssignID); - const sender = this.mainGuild.members.get(senderEntry.individualAssignID); - - if (!recipient || !sender) { - this.logPage({ number: senderNumber, user: 'N/A' }, { number: recipientNumber, user: 'N/A' }, 'phone', code); - } else { - this.logPage({ number: senderNumber, user: `${sender.username}#${sender.discriminator}` }, { number: recipientNumber, user: `${recipient.username}#${recipient.discriminator}` }, 'phone', code); - } } this.client.db.Stat.updateOne({ name: 'pages' }, { $inc: { value: 1 } }).exec(); return { status: true, message: `Page to \`${recipientNumber}\` sent.` }; diff --git a/types/index.d.ts b/types/index.d.ts index 46016f7..8754d81 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -9,4 +9,5 @@ export declare interface Config { internalKey: string; stripeKey: string; stripeSubSigningSecret: string; + ariClientKey: string; }