Created
July 2, 2020 10:39
-
-
Save dumebi/bcf9539b286fb4fd4a27a60a194c1be3 to your computer and use it in GitHub Desktop.
JS OOP example (messaging provider)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseProvider { | |
constructor (_name) { | |
if ( | |
_name == null || | |
_name === undefined || | |
typeof _name !== 'string' | |
) { | |
throw new Error('Class needs to be instantiated with an provider name') | |
} | |
this.name = _name | |
} | |
/** | |
* Implementation optional | |
*/ | |
name () { | |
return this.name | |
} | |
/** | |
* Implementation required | |
* @param {String} country Country address operates in | |
* @param {Number} areaCode Area code of country | |
* @param {String} channel sms (SMS), wa (Whatsapp), fb (Facebook) | |
*/ | |
getAddress (country, areaCode, channel) { | |
throw new Error('You have to implement the method getPrice!') | |
} | |
/** | |
* Implementation required | |
* @param {String} addressId Address ID from provider | |
*/ | |
deleteAddress (addressId) { | |
throw new Error('You have to implement the method getPrice!') | |
} | |
/** | |
* Implementation required | |
* @param {String} addressId Address ID from provider | |
*/ | |
sendMessage (addressId) { | |
throw new Error('You have to implement the method getPrice!') | |
} | |
} | |
module.exports = BaseProvider | |
onst BaseProvider = require('./base') | |
const TwilioClient = require('twilio')(`${process.env.TWILIO_SID}`, `${process.env.TWILIO_AUTH}`) | |
class TwilioProvider extends BaseProvider { | |
/** | |
* Implementation required | |
* @param {String} country Country address operates in | |
* @param {Number} areaCode Area code of country | |
* @param {String} channel sms (SMS), wa (Whatsapp), fb (Facebook) | |
*/ | |
async getAddress (country, areaCode, channel) { | |
const availableNumber = await TwilioClient.availablePhoneNumbers(country).local.list({ areaCode, limit: 1 }) | |
return TwilioClient.incomingPhoneNumbers.create({ phoneNumber: availableNumber[0].phone_number, areaCode }) | |
}; | |
/** | |
* Implementation required | |
* @param {String} addressId Address ID from provider | |
*/ | |
async deleteAddress (addressId) { | |
return TwilioClient.incomingPhoneNumbers(addressId).remove() | |
}; | |
/** | |
* Implementation required | |
* @param {String} body Message Body | |
* @param {String} from Sender Address | |
* @param {String} to Receiver Address | |
* @param {String} mediaUrl Media | |
*/ | |
async sendMessage (body, from, to, mediaUrl, channel, callback) { | |
if (channel == 'wa') { | |
to = `whatsapp:${to}` | |
from = `whatsapp:${from}` | |
} else if (channel == 'fb') { | |
to = `messenger:${to}` | |
from = `messenger:${from}` | |
} else if (channel == 'line') { | |
to = `line:${to}` | |
from = `line:${from}` | |
} | |
return mediaUrl ? TwilioClient.messages.create({ body, from, to, mediaUrl }) : TwilioClient.messages.create({ body, from, statusCallback: callback, to }) | |
}; | |
} | |
module.exports = new TwilioProvider('Twilio') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment