Отправка СМС:

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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import * as https from "https";
import * as querystring from "querystring";
import {Md5} from "md5-typescript";
 
/**
 *
 */
class EpochtaApiv3 {
     
    gatewayhost: string;
    gatewaypath: string;
    debug: boolean = false;
    public_key: string;
    private_key: string;
 
    /**
     *
     * @param public_key
     * @param private_key
     */
    constructor(public_key: string, private_key: string) {
        this.gatewayhost = 'api.atompark.com';
        this.gatewaypath = '/api/sms/3.0/';
        this.public_key = public_key;
        this.private_key = private_key;
    }
 
    private calcSum(params: any, action: string): string {
        // action = action.toLowerCase();
        params ["key"] = this.public_key;
        params["version"] = "3.0";
        params["action"] = action;
        var data: Array<any> = [];
        for (var pkey in params) {
            data.push({key: pkey, val: params[pkey]});
        }
        data.sort((a:any, b:any): any => {
            if (a.key === b.key)
                return 0;
            return a.key > b.key ? 1 : -1;
        });
        let control_str: string = "";
        for (var i=0;i < data.length;i++) {
            control_str += data[i].val;
        }
        control_str += this.private_key;
        return Md5.init(control_str);
    }
 
    /**
     *
     * @param sender
     * @param phone
     * @param text
     * @param datetime
     * @param callback
     * @param error_callback
     */
    public sendSMS(sender: string, phone: string, text: string, datetime: string = "", callback?: Function, error_callback?: Function): boolean {
        let action = "sendSMS"
        let sms_lifetime = "0"
        let params = <any> {};
        params["sender"] = sender
        params["text"] = text
        params["phone"] = phone
        params["datetime"] = datetime
        params["sms_lifetime"] = sms_lifetime
        let control_sum = this.calcSum(params, action);
        let data = <any> {
            "key":          this.public_key,
            "sum":          control_sum,
            "sender":       sender,
            "text":         text,
            "phone":        phone,
            "datetime":     "",
            "sms_lifetime": "0",
        }
        return this.request(data, action, callback, error_callback)
    }
     
    /**
     *
     * @param sender
     * @param phones
     * @param text
     * @param datetime
     * @param sms_lifetime
     * @param batch
     * @param batchinterval
     * @param callback
     * @param error_callback
     */
    public sendSMSGroup(sender: string, phones: Array<Array <string>>, text: string, datetime: string = "", sms_lifetime: number = 0, batch: number = 0, batchinterval: number = 0, controlnumber: string = "", callback?: Function, error_callback?: Function): boolean {
        let action = "sendSMSGroup"
        action = action.toLowerCase();
        let params = <any> {};
        params["sender"] = sender;
        params["text"] = text;
        params["phones"] = JSON.stringify(phones);
        params["datetime"] = datetime;
        params["sms_lifetime"] = sms_lifetime;
        params["batch"] = batch;
        params["batchinterval"] = batchinterval;
        params["controlnumber"] = controlnumber;
        let control_sum = this.calcSum(params, action);
 
        let data = <any> {
            "sender":       sender,
            "text":         text,
            "phones":       JSON.stringify(phones),
            "datetime":     datetime,
            "sms_lifetime": sms_lifetime,
            "batch":        batch,
            "batchinterval":batchinterval,
            "sum":          control_sum,
            "key":          this.public_key,
            "controlnumber":controlnumber
        }
        return this.request(data, action, callback, error_callback)
    }
     
    /**
     * Create campaign
     *
     * @param [string] sender
     * @param [string] text
     * @param [int] list_id
     * @param [string] datetime
     * @param [int] batch
     * @param [int] batchinterval
     * @param [int] sms_lifetime
     * @param [string] control_phone
     * @return [map]
     */
    public createCampaign(sender: string, text: string, list_id: number, datetime: string, batch: number, batchinterval: number, sms_lifetime: number, control_phone: string, callback?: Function, error_callback?: Function): boolean {
        let action = "createCampaign";
        let params = <any> {};
        params["sender"] = sender
        params["text"] = text
        params["list_id"] = list_id
        params["datetime"] = datetime
        params["batch"] = batch
        params["batchinterval"] = batchinterval
        params["sms_lifetime"] = sms_lifetime
        params["control_phone"] = control_phone
        let control_sum = this.calcSum(params, action);
        let data = <any> {
            "sender":       sender,
            "text":         text,
            "list_id":      list_id,
            "datetime":     datetime,
            "batch":        batch,
            "batchinterval":batchinterval,
            "sms_lifetime": sms_lifetime,
            "control_phone":control_phone,
            "key":          this.public_key,
            "sum":          control_sum,
        }
        return this.request(data, action, callback, error_callback)
    }
 
     
    /**
     * Add phone to addressbook
     *
     * @param [int] id_addressbook
     * @param [string] phone
     * @param [string] variables
     * @return [map]
     */
    public addPhoneToAddressBook(id_addressbook: number, phone: string, variables: string, callback?: Function, error_callback?: Function): boolean {
        let action = "addPhoneToAddressBook";
        let params = <any> {};
        params["idAddressBook"] = id_addressbook;
        params["phone"] = phone;
        params["variables"] = variables;
        let control_sum = this.calcSum(params, action)
        let data = <any> {
            "idAddressBook": id_addressbook,
            "phone": phone,
            "variables": variables,
            "key": this.public_key,
            "sum": control_sum
        }
        return this.request(data, action, callback, error_callback);
    }
 
    /**
     * Get user balance
     *
     * @param [string] currency
     * @return [map]
     */
    public getUserBalance(currency: string = "USD", callback?: Function, error_callback?: Function): boolean {
        let action = "getUserBalance"
        let params = <any> {};
        params["currency"] = currency;
        let control_sum = this.calcSum(params, action)
        let data = <any> {
            "currency":   currency,
            "key":        this.public_key,
            "sum":        control_sum
        }
        return this.request(data, action, callback, error_callback);
    }
 
    /**
     * Add addressbook
     *
     * @param [string] name
     * @param [string] description
     * @return [map]
     */
    public addAddressbook(name: string, description: string, callback?: Function, error_callback?: Function): boolean {
        let action = "addAddressbook";
        let params = <any> {};
        params["name"] = name;
        params["description"] = description;
        let control_sum = this.calcSum(params, action)
        let data = <any> {       
            "name":  name,
            "description": description,
            "key": this.public_key,
            "sum": control_sum
        }
        return this.request(data, action, callback, error_callback);
    }
 
    private handle_result (data: any): any {
        if (data['http_code'] != 200) {
          data['is_error'] = true;
        }
        return data;
    }
 
    /**
     * 1
     * @param xml
     * @param type
     * @param callback
     * @param error_callback
     */
    private request(data: any, action: string, callback?: Function, error_callback?: Function): boolean {
        if (this.debug) {
            console.log({data: data, action: action});
            return true;
        }
        const _this = this;
        let postData = querystring.stringify(data);
        let options = <any> {
            hostname: this.gatewayhost,
            port: 443,
            path: this.gatewaypath + action,
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Content-Length': postData.length
            }
        };
        var req = https.request(options, (res) => {
            if (res.statusCode == 200) {
                res.on('data', (d) => {
                    let result = JSON.parse(d);
                    let code = parseInt(result.code);
                    if (code >= 0) { // OK
                        callback(result);
                    } else {
                        error_callback(result)
                    }
                });
            } else {
                error_callback({error: "uknown", result: 'false'})
            }
        });
        req.on('error', (e) => {
            error_callback({error: "uknown", message: e+'', result: 'false'})
        });
        req.write(postData);
        req.end();
        return true;
    }
     
    /**
     *
     * @param status
     */
    private error_handler(custom_message: string): any {
        let data = <any> { 'is_error': true }
        if (custom_message) {
            data['message'] = custom_message
        }
        return data;
    }
 
}
 
var api3 = new EpochtaApiv3(
    'your public key',
    'your private key'
);
 
// api3.addAddressbook("typeScript3", "create from typescript", (res: any) => {console.log(res)}, (res: any) => {console.log(res)}); // { result: { addressbook_id: 1485588 } }
 
// api3.addPhoneToAddressBook(1485588, "380933630000", "", (res: any) => {console.log(res)}, (res: any) => {console.log(res)}); // { result: { phone_id: 457466228 } }
 
// api3.createCampaign("smSender", "message from typescript", 1485588, "", 0, 0, 0, "", (res: any) => {console.log(res)}, (res: any) => {console.log(res)}); // { result: { id: 118970671, price: 0.013300399011970358, currency: 'USD' } }
 
// api3.getUserBalance("USD", (res: any) => {console.log(res)}, (res: any) => {console.log(res)});
 
// api3.sendSMS("smSender", "380933630000", "", "", (res: any) => {console.log(res)}, (res: any) => {console.log(res)}); //
 
api3.sendSMSGroup("smSender", [["380933630000"]], "qeqeqeqwe", "", 0, 0, 0, "", (res: any) => {console.log(res)}, (res: any) => {console.log(res)}); //
vector
image

Нужна помощь?

Задайте вопрос специалисту технической поддержки