-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
243 lines (205 loc) · 5.72 KB
/
index.js
File metadata and controls
243 lines (205 loc) · 5.72 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const axios = require("axios");
const responses = require("./lib/responses");
/**
* @author Joshua Commey
* @class Node JS mnotify connector
* @license MIT License
* htps://github.com/commisol/mnotify
*/
class mNotify{
constructor(apiKey){
if(!apiKey) {
throw new Error("No API Key provided.");
}
/* Enabled API key */
this.apiKey = apiKey
/* Base endpoint */
this.baseEndpoint = "https://api.mnotify.com/api";
}
/**
* @description Retrieve all your message templates.
* @return {[type]} */
async listTemplates(){
return await this._get('template')
}
/**
* @param {String}
* @description Retrieve a specific message template with templateId as a parameter.
* @return {Object} */
async getTemplate(payload){
return await this._get('template', payload)
}
/**
* @param {String} title Title of message template
* @param {String} content Content of message template
* @description Add a message template with data. */
async addTemplate(payload){
return await this._post('template', payload)
}
/**
* @description Update a Message Template
* @field {int} id of message template to update
* @field {String} title Title of message template
* @field {String} content Content of message template
* @return {[type]} */
async updateTemplate(payload){
return await this._put("template", payload)
}
async deleteTemplate(payload){
return await this._delete("template", payload)
}
async listGroups(){
return await this._get("group")
}
/**
* @param {[type]}
* @return {[type]}
*/
async getGroup(payload){
return await this._get("group", payload)
}
async addGroup(payload){
return await this._post("group", payload)
}
async updateGroup(payload){
const {id} = payload
return await this._put("group/" + id, payload)
}
async deleteGroup(payload){
return await this._delete("group", payload)
}
async listContacts(){
return await this._get("contact")
}
async listGroupContacts(payload){
return await this._get("contact/group", payload)
}
async getContact(payload){
return await this._get("contact", payload)
}
/**
* @param {Object}
* @field int group_id --> Id of group you want to save contact to @required
* @field string phone --> Phone number of contact @required
* @field string title --> Title of contact eg Mr, Dr, Miss etc @required
* @field string firstname --> First name of contact @required
* @field string lastname --> Last name of contact
* @field string email --> Email name of contact
* @field date dob --> Date of birth of contact in YYYY-MM-DD format
* @return {Promise}
*/
async addContact(payload){
let {group_id} = payload;
return await this._post("contact" + "/" + group_id, payload)
}
/**
* @param {Object}
* @field int id Contact id to update @required
**/
async updateContact(payload){
const {id} = payload
return await this._put("contact/"+id, payload)
}
/**
* @description Delete a specific contact with id and group_id as parameters
* @field id @require int --> id of specified contact
* @field int @required group_id --> id of group which contact belong
*/
async deleteContact(payload){
return await this._delete("contact", payload)
}
async sendBulkSMS(payload){
return await this._post("sms/quick", payload)
}
async sendGroupBulkSMS(payload){
return await this._post("sms/group", payload)
}
async sendBulkCall(payload){
return await this._post("voice/quick", payload)
}
async sendGroupBulkVoice(payload){
return await this._post("voice/group", payload)
}
async registerSenderId(payload){
return await this._post("senderid/register", payload)
}
/**
* @return {[type]}
*/
async smsBalance(){
return await this._get("balance/sms")
}
/**
* @return {[type]}
*/
async voiceBalance(){
return await this._get("balance/voice")
}
async deliveryReport(payload){
return await this._get("campaign", payload)
}
async specificDeliveryReport(payload){
return await this._get("status", payload)
}
async periodicDeliveryReport(payload){
return await this._get("report", payload)
}
async voiceCallReport(payload){
return await this._get("calls", payload)
}
async specificVoiceCallReport(payload){
return await this._get("call-status", payload)
}
/**
* @description Retrieves voice call report in between specified dates
* @field from date --> Date from in YYYY-MM-DD format
* @field to date --> Date to in YYYY-MM-DD format
*/
async periodicVoiceCallReport(payload){
return await this._get("call-period", payload)
}
async _get(endpoint, params) {
return await this._request("GET", endpoint, params)
.then(response => response.data)
.catch(e => { throw new Error(e.message) })
}
async _post(endpoint, params) {
return await this._request("POST", endpoint, params)
.then(response => response.data)
.catch(e => { throw new Error(e.message) })
}
async _put(endpoint, params) {
return await this._request("PUT", endpoint, params)
.then(response => response.data)
.catch(e => { throw new Error(e.message) })
}
async _delete(endpoint, params) {
let {id} = params
return await this._request("DELETE", endpoint + "/" + id, params)
.then(response => response.data)
.catch(e => { throw new Error(e.message) })
}
async _request(method, endpoint, payload){
method = method || "GET";
payload = Object.assign({key: this.apiKey}, payload)
let url = this.baseEndpoint + "/" + endpoint
let request;
switch(method){
default:
case "GET":
request = await axios.get(url, {params: payload})
break;
case "POST":
request = await axios.post(url, payload)
break;
case "PUT":
request = await axios.put(url, payload)
break;
case "DELETE":
request = await axios.delete(url, {payload: payload})
break;
}
return request
}
}
module.exports = mNotify;