-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend-mms.js
57 lines (52 loc) · 1.81 KB
/
send-mms.js
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
const SDK = require('ringcentral')
const dotenv = require('dotenv')
const FormData = require('form-data')
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 => {
const formData = new FormData()
const body = {
from: { phoneNumber: process.env.RINGCENTRAL_USERNAME },
to: [
{ phoneNumber: process.env.RECIPIENT_PHONE_NUMBER }
],
text: 'This is a test MMS from Node JS'
}
formData.append('body', Buffer.from( JSON.stringify(body) ), {
filename: 'body.json',
contentType: 'application/json'
})
formData.append('attachment', require('fs').createReadStream('test.jpg'))
platform.post('/account/~/extension/~/sms', formData)
.then(response => {
var jsonObj = response.json()
console.log('MMS sent. Delivery status: ' + jsonObj.messageStatus)
//track_status(jsonObj.id, jsonObj.messageStatus)
}).catch(e => {
console.error(e)
})
}).catch(e => {
console.error(e)
})
function track_status(messageId, messageStatus){
if (messageStatus == "Queued"){
setTimeout(function(){
platform.get(`/account/~/extension/~/message-store/${messageId}`)
.then(response => {
messageStatus = response.json().messageStatus
var jsonObj = response.json()
console.log("MMS message delivery status: " + jsonObj.messageStatus)
track_status(jsonObj.id, jsonObj.messageStatus)
})
}, 1000)
}
}