Last active
April 19, 2018 02:15
-
-
Save miguelmota/fcd2464cbb4f50264c2b4cfd62616413 to your computer and use it in GitHub Desktop.
Node.js twilio send SMS
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
const twilio = require('twilio') | |
// Twilio Credentials | |
const accountSid = 'abc123'; | |
const authToken = 'abc123'; | |
const client = new twilio(accountSid, authToken); | |
const fromNumber = '12225558888' // account phone number | |
export function normalizePhone(phone) { | |
let digits = phone.replace(/[^\d]+/g, '') | |
if (digits.length === 10) { | |
digits = `1${digits}` // US country code required for SNS | |
} | |
return `+${digits}` | |
} | |
export async function send(phone, message) { | |
const response = await client.messages.create({ | |
to: normalizePhone(phone), | |
from: fromNumber, | |
body: message, | |
}) | |
return response.id | |
} | |
// send(17774449999, 'hello') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment