-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
79 lines (67 loc) · 2.73 KB
/
index.js
File metadata and controls
79 lines (67 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
app.use(bodyParser.json())
const persephonySDK = require('@persephony/sdk')
const port = 3000
const host = process.env.HOST
const accountId = process.env.accountId
const authToken = process.env.authToken
const persephony = persephonySDK(accountId, authToken)
const _MESSAGE_ = 'This is a message sent by Persephony!'
// Specify this route with 'Voice URL' in App Config
app.post('/incomingCall', (req, res) => {
const promptForNumber = persephony.percl.say('Welcome to the send a SMS sample Application! Please enter a phone number starting with a one.')
// Create options for getDigits script
const options = {
prompts: persephony.percl.build(promptForNumber),
maxDigits: 11,
minDigits: 11,
flushBuffer: true,
initialTimeoutMs: 10000
}
const getDigits = persephony.percl.getDigits(`${host}/numberSelectDone`, options)
const percl = persephony.percl.build(getDigits)
res.status(200).json(percl)
})
app.post('/numberSelectDone', (req, res) => {
const options = {
notificationUrl: `${host}/notificationUrl`
}
const digits = req.body.digits
const formattedNum = `+${digits}`
const callbackReason = req.body.reason
let percl
if(digits[0] != '1'){
const errorScript = persephony.percl.say('The phone number entered does not start with a one. Please call back to try again.')
percl = persephony.percl.build(errorScript)
}
else if (callbackReason == persephony.enums.getDigitsReason.TIMEOUT) {
const timeoutScript = persephony.percl.say('The phone call has timed out. Please call back to try again.')
percl = persephony.percl.build(timeoutScript)
}
else if (callbackReason == persephony.enums.getDigitsReason.MAX_DIGITS) {
// Create sms PerCL that sends sms to current caller using the number handling the request
const smsCommand = persephony.percl.sms(req.body.to, formattedNum, _MESSAGE_, options)
const sayCommand = persephony.percl.say('Your message has been sent.')
const hangup = persephony.percl.hangup()
percl = persephony.percl.build(smsCommand, sayCommand, hangup)
}
else {
const errorScript = persephony.percl.say('The phone number entered is invalid or an error has occured. Please check Persephony Logs for more details.')
percl = persephony.percl.build(errorScript)
}
res.status(200).json(percl)
})
// Receive status updates of the sms
app.post('/notificationUrl', (req, res) => {
console.log('Outbound Message Status Change: ', req.body)
})
// Specify this route with 'Status Callback URL' in App Config
app.post('/status', (req, res) => {
// handle status changes
res.status(200)
})
app.listen(port, () => {
console.log(`Starting server on ${port}`)
})