2015-06-28 22:42:48 +00:00
'use strict' ;
Object . defineProperty ( exports , '__esModule' , {
value : true
} ) ;
var _createClass = ( function ( ) { function defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( 'value' in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ) ( ) ;
var _get = function get ( _x2 , _x3 , _x4 ) { var _again = true ; _function : while ( _again ) { var object = _x2 , property = _x3 , receiver = _x4 ; desc = parent = getter = undefined ; _again = false ; if ( object === null ) object = Function . prototype ; var desc = Object . getOwnPropertyDescriptor ( object , property ) ; if ( desc === undefined ) { var parent = Object . getPrototypeOf ( object ) ; if ( parent === null ) { return undefined ; } else { _x2 = parent ; _x3 = property ; _x4 = receiver ; _again = true ; continue _function ; } } else if ( 'value' in desc ) { return desc . value ; } else { var getter = desc . get ; if ( getter === undefined ) { return undefined ; } return getter . call ( receiver ) ; } } } ;
function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( 'Cannot call a class as a function' ) ; } }
function _inherits ( subClass , superClass ) { if ( typeof superClass !== 'function' && superClass !== null ) { throw new TypeError ( 'Super expression must either be null or a function, not ' + typeof superClass ) ; } subClass . prototype = Object . create ( superClass && superClass . prototype , { constructor : { value : subClass , enumerable : false , writable : true , configurable : true } } ) ; if ( superClass ) subClass . _ _proto _ _ = superClass ; }
var _events = require ( 'events' ) ;
2015-07-15 16:41:33 +00:00
var ANSWER _THRESHOLD = 10 ;
2015-06-28 22:42:48 +00:00
/ * *
* Base class of all classes
* /
var Base = ( function ( _EventEmitter ) {
2015-06-30 22:04:44 +00:00
function Base ( method ) {
2015-06-28 22:42:48 +00:00
_classCallCheck ( this , Base ) ;
_get ( Object . getPrototypeOf ( Base . prototype ) , 'constructor' , this ) . call ( this ) ;
2015-06-30 22:04:44 +00:00
this . method = method ;
2015-06-28 22:42:48 +00:00
this . properties = { } ;
}
_inherits ( Base , _EventEmitter ) ;
_createClass ( Base , [ {
2015-06-30 22:04:44 +00:00
key : 'send' ,
/ * *
* Sends the message , you should only use this method yourself if
* you are extending this class . Normally you should call bot . send ( message )
*
* Events : message : sent => Emitted after sending the message to API , gets the
* API ' s response
*
* message : answer => Emitted when your message gets an answer from
* the contact ( reply in case of groups )
* gets the Update object containing message
*
* @ param { object } bot
* @ return { promise } returns a promise , resolved with message : answer
* /
value : function send ( bot ) {
var _this = this ;
if ( this . _keyboard ) {
var reply _markup = JSON . stringify ( this . _keyboard . getProperties ( ) ) ;
this . properties . reply _markup = reply _markup ;
}
var messageId = undefined ;
return new Promise ( function ( resolve ) {
bot . api [ _this . method ] ( _this . properties ) . then ( function ( response ) {
messageId = response . result . message _id ;
_this . emit ( 'message:sent' , response ) ;
} ) ;
if ( _this . _keyboard . one _time _keyboard ) {
_this . _keyboard . replyMarkup = '' ;
}
var chat = _this . properties . chat _id ;
2015-07-15 16:41:33 +00:00
var answers = 0 ;
2015-06-30 22:04:44 +00:00
bot . on ( 'update' , function listener ( result ) {
2015-07-15 16:41:33 +00:00
answers += result . length ;
2015-06-30 22:04:44 +00:00
var update = result . find ( function ( _ref ) {
var message = _ref . message ;
// if in a group, there will be a reply to this message
if ( chat < 0 ) {
return message . chat . id === chat && message . reply _to _message && message . reply _to _message . message _id === messageId ;
} else {
return message . chat . id === chat ;
}
} ) ;
if ( update ) {
resolve ( update . message ) ;
this . emit ( 'message:answer' , update . message ) ;
bot . removeListener ( 'update' , listener ) ;
}
2015-07-15 16:41:33 +00:00
if ( answers >= ANSWER _THRESHOLD ) {
bot . removeListener ( 'update' , listener ) ;
}
2015-06-30 22:04:44 +00:00
} ) ;
} ) ;
}
} , {
2015-06-28 22:42:48 +00:00
key : 'getProperties' ,
/ * *
* Returns properties of the object
* @ return { object } properties of object
* /
value : function getProperties ( ) {
return this . properties ;
}
} , {
key : 'setProperties' ,
/ * *
* Set properties of the object
* @ param { object } object properties to set
* @ param { boolean } extend A boolean indicating if the properties should be
* extended by the object provided ( Object . assign )
* or properties should be replaced by the object
* defaults to true
* @ return { object } returns the properties ( same as getProperties )
* /
value : function setProperties ( object ) {
var extend = arguments [ 1 ] === undefined ? true : arguments [ 1 ] ;
this . properties = extend ? Object . assign ( this . properties , object ) : object ;
return this . getProperties ( ) ;
}
} ] ) ;
return Base ;
} ) ( _events . EventEmitter ) ;
exports [ 'default' ] = Base ;
module . exports = exports [ 'default' ] ;