2017-07-29 04:39:18 +04:00
import http from 'http' ;
2017-05-29 01:46:00 +03:00
/ * *
2017-09-29 01:01:11 +04:00
* @ param { String } uuid - Bot ID
* @ param { Function } callback - Function handler
* @ description Connection to the service and retrieves the session identifier .
2017-05-29 01:46:00 +03:00
* /
2017-09-29 01:01:11 +04:00
function connect ( uuid , callback ) {
if ( ! isVerification ( uuid ) ) {
throw new Error ( '\'uuid\' invalid variable.' ) ;
}
2017-07-29 04:39:18 +04:00
2017-09-29 01:01:11 +04:00
var query = {
path : ` /api/2.0/json/Chat.init/ ${ uuid } ` ,
hostname : 'iii.ru' ,
method : 'GET' ,
port : 80
} ;
2017-07-29 04:39:18 +04:00
2017-09-29 01:01:11 +04:00
var request = http . request ( query , ( response ) => {
var json = null ;
response . on ( 'data' , ( raw ) => {
json = decryptJSON ( raw ) ;
2017-06-10 20:42:29 +00:00
} ) ;
2017-09-29 01:01:11 +04:00
response . on ( 'end' , ( ) => {
callback ( json ) ;
} ) ;
} ) ;
request . on ( 'error' , ( error ) => {
callback ( error ) ;
2017-05-29 01:46:00 +03:00
} ) ;
2017-09-29 01:01:11 +04:00
request . end ( ) ;
2017-05-29 01:46:00 +03:00
}
/ * *
2017-09-29 01:01:11 +04:00
* @ param { String } cuid - Session ID
* @ param { String } text - Send messages
* @ param { Function } callback - Function handler
* @ description Sends a message to bot and returns a response .
2017-05-29 01:46:00 +03:00
* /
2017-09-29 01:01:11 +04:00
function send ( cuid , text , callback ) {
if ( ! isVerification ( cuid ) ) {
throw new Error ( '\'cuid\' invalid variable.' ) ;
}
2017-07-29 04:39:18 +04:00
2017-09-29 01:01:11 +04:00
if ( ! isString ( text ) ) {
throw new Error ( '\'text\' invalid variable.' ) ;
}
2017-07-29 04:39:18 +04:00
2017-09-29 01:01:11 +04:00
var query = {
path : '/api/2.0/json/Chat.request' ,
hostname : 'iii.ru' ,
method : 'POST' ,
port : 80 ,
} ;
var request = http . request ( query , ( response ) => {
var json = null ;
response . on ( 'data' , ( raw ) => {
json = decryptJSON ( raw ) ;
} ) ;
response . on ( 'end' , ( ) => {
callback ( json . result ) ;
2017-06-10 20:42:29 +00:00
} ) ;
2017-05-29 01:46:00 +03:00
} ) ;
2017-09-29 01:01:11 +04:00
request . on ( 'error' , ( error ) => {
callback ( error ) ;
} ) ;
request . write ( createPackage ( cuid , text ) ) ;
request . end ( ) ;
2017-05-29 01:46:00 +03:00
}
/ * *
2017-09-29 01:01:11 +04:00
* @ param { String } data - Data for encryption
* @ returns { String } Encrypted data
* @ description Encrypts the received string .
2017-05-29 01:46:00 +03:00
* /
2017-07-29 04:39:18 +04:00
function encrypt ( data ) {
2017-09-29 01:43:36 +04:00
var base64 = Buffer . from ( data ) . toString ( 'base64' ) ;
var string = Buffer . from ( base64 ) ;
2017-05-30 16:03:39 +03:00
return mergerString ( string ) . toString ( 'base64' ) ;
2017-05-29 01:46:00 +03:00
}
/ * *
2017-09-29 01:01:11 +04:00
* @ param { String } data - Data for decryption
* @ returns { String } Decrypted data
* @ description Decrypts the received string .
2017-05-29 01:46:00 +03:00
* /
2017-07-29 04:39:18 +04:00
function decrypt ( data ) {
2017-09-29 01:43:36 +04:00
var string = Buffer . from ( data , 'base64' ) ;
2017-09-29 01:01:11 +04:00
var decrypted = mergerString ( string ) . toString ( ) ;
2017-09-29 01:43:36 +04:00
return Buffer . from ( decrypted , 'base64' ) ;
2017-05-29 01:46:00 +03:00
}
/ * *
2017-09-29 01:01:11 +04:00
* @ param { String } json - Encrypted object
* @ returns { Object } Decrypted object
* @ description Decrypts the received object .
2017-05-29 01:46:00 +03:00
* /
2017-07-29 04:39:18 +04:00
function decryptJSON ( json ) {
2017-09-29 01:01:11 +04:00
var string = json . toString ( 'ascii' ) ;
var data = decrypt ( string ) ;
2017-05-29 01:46:00 +03:00
return JSON . parse ( data ) ;
}
/ * *
2017-09-29 01:01:11 +04:00
* @ param { String } data - Source string
* @ returns { String } Combined string
* @ description Merges the source string .
2017-05-29 01:46:00 +03:00
* /
2017-05-30 16:03:39 +03:00
function mergerString ( data ) {
2017-09-29 01:43:36 +04:00
var salt = Buffer . from ( 'some very-very long string without any non-latin characters due to different string representations inside of variable programming languages' ) ;
2017-10-09 19:00:02 +04:00
for ( var i = 0 ; i < data . length ; i ++ ) {
data [ i ] ^= salt [ i % salt . length ] ;
}
return data ;
2017-05-29 01:46:00 +03:00
}
/ * *
2017-09-29 01:01:11 +04:00
* @ param { String } cuid - Session ID
* @ param { String } text - Source string
* @ returns { String } Packed request
* @ description Creates a package to send .
2017-05-29 01:46:00 +03:00
* /
2017-07-29 04:39:18 +04:00
function createPackage ( cuid , text ) {
2017-09-29 01:01:11 +04:00
var data = [ ] ;
2017-07-29 04:39:18 +04:00
data . push ( cuid ) ;
data . push ( text . toString ( ) ) ;
2017-09-29 01:01:11 +04:00
var json = JSON . stringify ( data ) ;
2017-05-30 16:03:39 +03:00
return encrypt ( json ) ;
2017-05-29 01:46:00 +03:00
}
/ * *
2017-09-29 01:01:11 +04:00
* @ param { String } value - Variable to check
* @ returns { Boolean } Result of checking
* @ description Checks the type of the variable .
2017-05-29 01:46:00 +03:00
* /
2017-09-29 01:01:11 +04:00
function isVerification ( value ) {
var regexp = new RegExp ( '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$' , 'i' ) ;
return regexp . test ( value ) ;
2017-05-29 01:46:00 +03:00
}
2017-07-29 04:39:18 +04:00
/ * *
2017-09-29 01:01:11 +04:00
* @ param { String } value - Variable to check
* @ returns { Boolean } Result of checking
* @ description Checks the type of the variable .
2017-07-29 04:39:18 +04:00
* /
2017-09-29 01:01:11 +04:00
function isString ( value ) {
return typeof value === 'string' ;
}
export {
isVerification ,
decryptJSON ,
connect ,
decrypt ,
encrypt ,
send
} ;