2015-06-28 22:42:48 +00:00
'use strict' ;
2016-03-27 20:29:00 +00:00
Object . defineProperty ( exports , "__esModule" , {
2015-06-28 22:42:48 +00:00
value : true
} ) ;
2016-03-27 20:29:00 +00:00
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 ; } ; } ( ) ;
2015-06-28 22:42:48 +00:00
2016-03-27 20:29:00 +00:00
var _events = require ( 'events' ) ;
2015-06-28 22:42:48 +00:00
2016-03-27 20:29:00 +00:00
function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
2015-06-28 22:42:48 +00:00
2016-03-27 20:29:00 +00:00
function _possibleConstructorReturn ( self , call ) { if ( ! self ) { throw new ReferenceError ( "this hasn't been initialised - super() hasn't been called" ) ; } return call && ( typeof call === "object" || typeof call === "function" ) ? call : self ; }
2015-06-28 22:42:48 +00:00
2016-03-27 20:29:00 +00:00
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 ) Object . setPrototypeOf ? Object . setPrototypeOf ( subClass , superClass ) : subClass . _ _proto _ _ = superClass ; }
2015-06-28 22:42:48 +00:00
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
* /
2016-03-27 20:29:00 +00:00
var Base = function ( _EventEmitter ) {
_inherits ( Base , _EventEmitter ) ;
2015-06-30 22:04:44 +00:00
function Base ( method ) {
2015-06-28 22:42:48 +00:00
_classCallCheck ( this , Base ) ;
2016-03-27 20:29:00 +00:00
var _this = _possibleConstructorReturn ( this , Object . getPrototypeOf ( Base ) . call ( this ) ) ;
2015-06-30 22:04:44 +00:00
2016-03-27 20:29:00 +00:00
_this . method = method ;
_this . properties = { } ;
return _this ;
2015-06-28 22:42:48 +00:00
}
2016-03-27 20:29:00 +00:00
/ * *
* 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
* /
2015-06-28 22:42:48 +00:00
_createClass ( Base , [ {
2015-06-30 22:04:44 +00:00
key : 'send' ,
value : function send ( bot ) {
2016-03-27 20:29:00 +00:00
var _this2 = this ;
2015-06-30 22:04:44 +00:00
if ( this . _keyboard ) {
var reply _markup = JSON . stringify ( this . _keyboard . getProperties ( ) ) ;
this . properties . reply _markup = reply _markup ;
}
2016-03-27 20:29:00 +00:00
var messageId = void 0 ;
2015-06-30 22:04:44 +00:00
return new Promise ( function ( resolve ) {
2016-03-27 20:29:00 +00:00
bot . api [ _this2 . method ] ( _this2 . properties ) . then ( function ( response ) {
2015-06-30 22:04:44 +00:00
messageId = response . result . message _id ;
2016-03-27 20:29:00 +00:00
_this2 . emit ( 'message:sent' , response ) ;
2015-06-30 22:04:44 +00:00
} ) ;
2016-03-27 20:29:00 +00:00
if ( _this2 . _keyboard . one _time _keyboard ) {
_this2 . _keyboard . replyMarkup = '' ;
2015-06-30 22:04:44 +00:00
}
2016-03-27 20:29:00 +00:00
var chat = _this2 . 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
/ * *
* Returns properties of the object
* @ return { object } properties of object
* /
2016-03-27 20:29:00 +00:00
} , {
key : 'getProperties' ,
2015-06-28 22:42:48 +00:00
value : function getProperties ( ) {
return this . properties ;
}
/ * *
* 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 )
* /
2016-03-27 20:29:00 +00:00
} , {
key : 'setProperties' ,
2015-06-28 22:42:48 +00:00
value : function setProperties ( object ) {
2016-03-27 20:29:00 +00:00
var extend = arguments . length <= 1 || arguments [ 1 ] === undefined ? true : arguments [ 1 ] ;
2015-06-28 22:42:48 +00:00
this . properties = extend ? Object . assign ( this . properties , object ) : object ;
return this . getProperties ( ) ;
}
} ] ) ;
return Base ;
2016-03-27 20:29:00 +00:00
} ( _events . EventEmitter ) ;
2015-06-28 22:42:48 +00:00
2016-03-27 20:29:00 +00:00
exports . default = Base ;
2015-06-28 22:42:48 +00:00
module . exports = exports [ 'default' ] ;