Last active
May 19, 2018 22:50
-
-
Save TravisMullen/eb840534b1c1a0a9e26327e56f6d30e6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env node | |
// node index.js '+1...' 'My Message.' | |
// `npm i plivo` | |
// create file: `.keys/index.js` | |
// and add: | |
// ```JS | |
// module.exports = { | |
// PLIVO_AUTH_ID: 'PLIVO_AUTH_ID', | |
// PLIVO_AUTH_TOKEN: 'PLIVO_SENDER', | |
// PLIVO_SENDER: 'PLIVO_SENDER' | |
// } | |
// ``` | |
const { PLIVO_AUTH_ID, PLIVO_AUTH_TOKEN, PLIVO_SENDER } = require('./.keys') | |
const params = { | |
'src': PLIVO_SENDER, // Sender's phone number with country code | |
'dst': process.argv[2] // Receiver's phone Number with country code | |
} | |
const __now__ = new Date() | |
const os = require('os') | |
const plivo = require('plivo') | |
const client = new plivo.Client(PLIVO_AUTH_ID, PLIVO_AUTH_TOKEN) | |
const emojis = { | |
alertPattern: /[Aa]lert/g, | |
warningPattern: /[Ww]arning/g, | |
loginPattern: /[Ll]ogin/g, | |
paymentPattern: /[Pp]ayment/g, | |
lovePattern: /[Ll]ove/g, | |
killPattern: /[Kk]ill/g, | |
coldPattern: /[Cc]old/g, | |
hotPattern: /[Hh]ot/g, | |
foodPattern: /[Ff]ood/g | |
} | |
const postfix = ` | |
${__now__.getHours()}:${__now__.getMinutes()}:${__now__.getSeconds()} - ${os.hostname()} | |
${__now__.getMonth() + 1}/${__now__.getDate()}/${__now__.getFullYear()} | |
` | |
let text = `Hello World` | |
if (!(process.argv[2] && typeof (process.argv[2]) === 'string')) { | |
console.log(` | |
Phone number required as first argument. | |
`) | |
process.exit(1) | |
} | |
if (process.argv[3] && typeof (process.argv[3]) === 'string') { | |
text = process.argv[3] | |
} else { | |
console.log(` | |
No message provided. | |
`) | |
} | |
params.text = text.replace(emojis.alertPattern, 'π¨') | |
.replace(emojis.warningPattern, 'β οΈ') | |
.replace(emojis.loginPattern, 'π¨βπ»') | |
.replace(emojis.paymentPattern, 'π°') | |
.replace(emojis.lovePattern, 'π') | |
.replace(emojis.killPattern, 'β οΈοΈ') | |
.replace(emojis.coldPattern, 'βοΈ') | |
.replace(emojis.hotPattern, 'π‘οΈπ₯') | |
.replace(emojis.foodPattern, 'π') | |
console.log(` | |
Message Body | |
================================================================================ | |
${params.text} | |
================================================================================ | |
Characters: ${params.text.length} | |
Size: ${parseFloat((params.text.length / 160) * 100).toFixed(1)}% | |
`) | |
async function send (src, dst, text) { | |
try { | |
let { message, messageUuid } = await client.messages.create(src, dst, text) | |
console.log(` | |
Message Created. | |
"${message}" | |
`) | |
return messageUuid | |
} catch (error) { | |
console.error(` | |
Message Error. | |
"${error}" | |
`) | |
process.exit(1) | |
} | |
} | |
async function queue (src, dst, text) { | |
let messageUuid = await send(src, dst, text) | |
if (messageUuid) { | |
let { errorCode, messageState } = await client.messages.get(messageUuid) | |
if (errorCode) { | |
console.error(` | |
Message Failed. | |
"${errorCode}" | |
`) | |
// if failed | |
// setTImeout and call queue again | |
// let resent = await send(src, dst, `[Resent] ${params.text}`) | |
// console.log('Message Resent: ', resent) | |
process.exit(1) | |
} | |
console.log(` | |
Message Success. | |
"${messageState}" | |
`) | |
process.exit(messageState) | |
} | |
} | |
queue(params.src, params.dst, `${params.text} ${postfix}`) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment