-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (65 loc) · 1.65 KB
/
index.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const smsProUtil = require('./lib/util/smsPro')
const fetch = require('node-fetch')
const btoa = require('btoa')
class smsPro {
constructor ({
customerId,
customerid,
username,
customerPassword,
customerpassword,
endpoint,
password,
from,
shouldMockSms,
logstashUrl,
logstashPort,
}) {
this.customerId = customerId || customerid
this.customerPassword = customerPassword || customerpassword
this.username = username
this.password = password
this.endpoint = endpoint
this.from = from
this.shouldMockSms = shouldMockSms
this.logstashUrl = logstashUrl
this.logger = require('./lib/services/logger')({
source: 'smspro',
logstashUrl,
logstashPort,
shouldMockSms,
})
}
async send (body) {
if (this.shouldMockSms) {
return this.logger.info(body)
}
const headers = {
Authorization: `Basic ${btoa(`${this.username}:${this.password}`)}`,
'Content-Type': 'text/xml',
}
const data = await fetch(this.endpoint, {
method: 'POST',
headers,
body,
})
if (this.logstashUrl) {
await this.logger.info(body)
}
return smsProUtil.parseResult(await data.text())
}
async sendMtSms (messageProps) {
return this.send(smsProUtil.mtSms(Object.assign({}, this, messageProps)))
}
async parseIncoming (data) {
const smsAsJson = await smsProUtil.parseIncoming(data)
if (this.logstashUrl) {
this.logger.info(smsAsJson)
}
return smsAsJson
}
async sendFlashSms (messageProps) {
return this.send(smsProUtil.flashSms(Object.assign({}, this, messageProps)))
}
}
module.exports = smsPro