Пример использования ePochta SMS api 3.0 на языке TypeScript
Отправка смс
Получения статуса отправленной смс
Получение цены отправки
Получение баланса
Отправка смс:
import * as http from "http"; 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 = http.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)}); //
По запросу предоставляется необходимое количество дополнительных смс для тестирования сервиса массовых смс-уведомлений ePochta SMS.
Есть вопрос?
- 8 (800) 555-09-63
- Бесплатно по России
Новости:
- 2020-11-23
- ePochta объявляет старт Черной Пятницы: Спеццена на рассылку в WhatsApp
- 2020-11-18
- С 1 декабря 2020 года оператор МТС повышает стоимость СМС
- 2020-05-04
- Вебинар: СМС маркетинг. Что писать в тексте СМС рассылок?
- 2020-04-28
- Операторы повышают цены СМС рассылки по прямому каналу в РФ
- 2019-12-21
- Украинские операторы повышают цену СМС