-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreceive-reply.js
More file actions
62 lines (56 loc) · 1.92 KB
/
receive-reply.js
File metadata and controls
62 lines (56 loc) · 1.92 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
const SDK = require('ringcentral')
const dotenv = require('dotenv')
dotenv.config()
const rcsdk = new SDK({
server: process.env.RINGCENTRAL_SERVER_URL,
appKey: process.env.RINGCENTRAL_CLIENT_ID,
appSecret: process.env.RINGCENTRAL_CLIENT_SECRET
})
const platform = rcsdk.platform()
platform.login({
username: process.env.RINGCENTRAL_USERNAME,
extension: process.env.RINGCENTRAL_EXTENSION,
password: process.env.RINGCENTRAL_PASSWORD
}).then(response => {
var subscription = rcsdk.createSubscription()
var eventFilters = [
'/account/~/extension/~/message-store/instant?type=SMS',
'/account/~/extension/~/voicemail'
]
subscription
.setEventFilters(eventFilters)
.register()
.then(response => {
console.log("Waiting for notifications ...")
})
subscription.on(subscription.events.notification, function(msg){
if (msg.event.indexOf('/message-store/instant') > -1) {
console.log("Received a text message from: " + msg.body.from.phoneNumber)
var message = 'This is an automatic reply. Thank you for your message!'
sendReplySMS(message, msg.body.from.phoneNumber)
}else if (msg.event.indexOf('/voicemail') > -1) {
if (msg.body.from.hasOwnProperty("phoneNumber")){
console.log("Received a voicemail from: " + msg.body.from.phoneNumber)
var message = 'This is an automatic reply. Thank you for your voice message! I will get back to you asap.'
sendReplySMS(message, msg.body.from.phoneNumber)
}
}else {
console.log("Not an event we are waiting for.")
}
})
}).catch(e => {
console.error(e)
})
function sendReplySMS(message, recipientNumber){
platform.post('/account/~/extension/~/sms', {
from: { phoneNumber: process.env.RINGCENTRAL_USERNAME },
to: [
{ phoneNumber: recipientNumber }
],
text: message
}).then(response => {
console.log('Replied message sent.')
}).catch(e => {
console.error(e)
})
}