-
Notifications
You must be signed in to change notification settings - Fork 6
/
kavenegar.js
107 lines (106 loc) · 3.36 KB
/
kavenegar.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
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
var https = require('https');
var querystring = require('querystring');
var KavenegarApi = function(options) {
this.options = {};
this.options.host = 'api.kavenegar.com';
this.options.version = 'v1';
this.options.apikey = options.apikey;
};
KavenegarApi.prototype.request = function(action, method, params, callback) {
var path = '/' + this.options.version + '/' + this.options.apikey + '/' + action + '/' + method + '.json';
var postdata = querystring.stringify(params);
var post_options = {
host: this.options.host,
port: '443',
path: path,
method: 'POST',
headers: {
'Content-Length': postdata.length,
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
}
};
var req = https.request(post_options, function(e) {
e.setEncoding('utf8');
var result = '';
e.on('data', function(data) {
result += data;
});
e.on('end',function () {
try {
var jsonObject = JSON.parse(result);
if (callback) callback(
jsonObject.entries,
jsonObject.return.status,
jsonObject.return.message
);
} catch (e){
console.log('Exception!',e);
if(callback){
callback(e.message,500)
}
}
})
});
req.write(postdata, "utf8");
req.on("error", function(e) {
if (callback) callback(JSON.stringify({
error: e.message
}));
});
req.end();
};
KavenegarApi.prototype.Send = function(data, callback) {
this.request("sms", "send", data, callback);
};
KavenegarApi.prototype.SendArray = function(data, callback) {
this.request("sms", "sendarray", data, callback);
};
KavenegarApi.prototype.Status = function(data, callback) {
this.request("sms", "status", data, callback);
};
KavenegarApi.prototype.StatusLocalMessageid = function(data, callback) {
this.request("sms", "statuslocalmessageid", data, callback);
};
KavenegarApi.prototype.Select = function(data, callback) {
this.request("sms", "select", data, callback);
};
KavenegarApi.prototype.SelectOutbox = function(data, callback) {
this.request("sms", "selectoutbox", data, callback);
};
KavenegarApi.prototype.LatestOutbox = function(data, callback) {
this.request("sms", "latestoutbox", data, callback);
};
KavenegarApi.prototype.CountOutbox = function(data, callback) {
this.request("sms", "countoutbox", data, callback);
};
KavenegarApi.prototype.Cancel = function(data, callback) {
this.request("sms", "cancel", data, callback);
};
KavenegarApi.prototype.Receive = function(data, callback) {
this.request("sms", "receive", data, callback);
};
KavenegarApi.prototype.CountInbox = function(data, callback) {
this.request("sms", "countinbox", data, callback);
};
KavenegarApi.prototype.CountPostalCode = function(data, callback) {
this.request("sms", "countpostalcode", data, callback);
};
KavenegarApi.prototype.SendByPostalCode = function(data, callback) {
this.request("sms", "sendbypostalcode", data, callback);
};
KavenegarApi.prototype.VerifyLookup = function(data, callback) {
this.request("verify", "lookup", data, callback);
};
KavenegarApi.prototype.AccountInfo = function(data, callback) {
this.request("account", "info", data, callback);
};
KavenegarApi.prototype.AccountConfig = function(data, callback) {
this.request("account", "config", data, callback);
};
KavenegarApi.prototype.CallMakeTTS = function(data, callback) {
this.request("call", "maketts", data, callback);
};
module.exports.KavenegarApi = function (options) {
var obj = new KavenegarApi(options);
return obj;
}