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 _interopRequireDefault ( obj ) { return obj && obj . _ _esModule ? obj : { 'default' : obj } ; }
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 ; }
2015-07-05 13:36:27 +00:00
var _functionsApi = require ( './functions/api' ) ;
2015-06-28 22:42:48 +00:00
2015-07-05 13:36:27 +00:00
var _functionsApi2 = _interopRequireDefault ( _functionsApi ) ;
2015-06-28 22:42:48 +00:00
2015-07-05 13:36:27 +00:00
var _functionsWebhook = require ( './functions/webhook' ) ;
2015-06-29 22:20:34 +00:00
2015-07-05 13:36:27 +00:00
var _functionsWebhook2 = _interopRequireDefault ( _functionsWebhook ) ;
2015-06-29 22:20:34 +00:00
2015-07-05 13:36:27 +00:00
var _functionsPoll = require ( './functions/poll' ) ;
2015-06-29 22:20:34 +00:00
2015-07-05 13:36:27 +00:00
var _functionsPoll2 = _interopRequireDefault ( _functionsPoll ) ;
var _functionsArgumentParser = require ( './functions/argument-parser' ) ;
var _functionsArgumentParser2 = _interopRequireDefault ( _functionsArgumentParser ) ;
2015-06-29 22:20:34 +00:00
2015-06-28 22:42:48 +00:00
var _events = require ( 'events' ) ;
2015-07-09 09:56:17 +00:00
var _typesMessage = require ( './types/Message' ) ;
var _typesMessage2 = _interopRequireDefault ( _typesMessage ) ;
2015-06-28 22:42:48 +00:00
var DEFAULTS = {
update : {
offset : 0 ,
2015-07-05 13:36:27 +00:00
timeout : 20 ,
2015-06-28 22:42:48 +00:00
limit : 100
}
} ;
2015-07-09 11:16:27 +00:00
var REQUIRED = 0 ;
var OPTIONAL = 1 ;
2015-06-28 22:42:48 +00:00
/ * *
* Bot class used to connect to a new bot
* Bots have an api property which gives access to all Telegram API methods ,
* see API class
* /
var Bot = ( function ( _EventEmitter ) {
/ * *
* Create and connect to a new bot
* @ param { object } options Bot properties .
* /
function Bot ( ) {
var options = arguments [ 0 ] === undefined ? { update : { } } : arguments [ 0 ] ;
_classCallCheck ( this , Bot ) ;
_get ( Object . getPrototypeOf ( Bot . prototype ) , 'constructor' , this ) . call ( this ) ;
if ( ! options . token ) {
throw new Error ( 'Token cannot be empty' ) ;
}
this . token = options . token ;
this . update = Object . assign ( options . update || { } , DEFAULTS . update ) ;
2015-07-05 13:36:27 +00:00
this . api = new _functionsApi2 [ 'default' ] ( this . token ) ;
2015-06-28 22:42:48 +00:00
this . msg = { } ;
// EventEmitter
this . _events = { } ;
this . _userEvents = [ ] ;
2015-07-04 11:18:07 +00:00
this . setMaxListeners ( 100 ) ;
2015-06-28 22:42:48 +00:00
}
_inherits ( Bot , _EventEmitter ) ;
_createClass ( Bot , [ {
key : 'start' ,
/ * *
2015-06-29 22:20:34 +00:00
* Gets information about the bot and then
* 1 ) starts polling updates from API
* 2 ) sets a webhook as defined by the first parameter and listens for updates
2015-06-28 22:42:48 +00:00
* Emits an ` update ` event after polling with the response from server
* Returns a promise which is resolved after the bot information is received
* and set to it ' s ` info ` property i . e . bot . info
2015-06-29 22:20:34 +00:00
*
* @ param { object } hook An object containg options passed to webhook
* properties :
* - url : HTTPS url to listen on POST requests coming
* from the Telegram API
* - port : the port to listen to , defaults to 443
* - server : An object passed to https . createServer
*
2015-06-28 22:42:48 +00:00
* @ return { promise } A promise which is resolved with the response of getMe
* /
2015-06-29 22:20:34 +00:00
value : function start ( hook ) {
var _this = this ;
2015-06-28 22:42:48 +00:00
2015-06-29 22:20:34 +00:00
if ( hook ) {
2015-07-05 13:36:27 +00:00
return ( 0 , _functionsWebhook2 [ 'default' ] ) ( hook , this ) ;
2015-06-29 22:20:34 +00:00
}
2015-06-28 22:42:48 +00:00
return this . api . getMe ( ) . then ( function ( response ) {
2015-06-29 22:20:34 +00:00
_this . info = response . result ;
_this . on ( 'update' , _this . _update ) ;
if ( hook ) {
2015-07-05 13:36:27 +00:00
return ( 0 , _functionsWebhook2 [ 'default' ] ) ( hook , _this ) ;
2015-06-29 22:20:34 +00:00
} else {
2015-07-05 13:36:27 +00:00
return ( 0 , _functionsPoll2 [ 'default' ] ) ( _this ) ;
2015-06-29 22:20:34 +00:00
}
2015-06-28 22:42:48 +00:00
} ) ;
}
} , {
key : 'get' ,
/ * *
* Listens on specific message matching the pattern which can be an string
* or a regexp .
* @ param { string / regex } pattern
* @ param { function } listener function to call when a message matching the
* pattern is found , gets the Update
* In case of string , the message should start
* with the string i . e . / ^ yourString /
* @ return { object } returns the bot object
* /
value : function get ( pattern , listener ) {
if ( typeof pattern === 'string' ) {
pattern = new RegExp ( '^' + pattern ) ;
}
this . _userEvents . push ( {
pattern : pattern , listener : listener
} ) ;
return this ;
}
} , {
key : 'command' ,
/ * *
* Listens on a command
2015-07-05 13:36:27 +00:00
* @ param { string } command the command string , should not include slash ( / )
2015-06-28 22:42:48 +00:00
* @ param { function } listener function to call when the command is received ,
* gets the update
* @ return { object } returns the bot object
* /
2015-07-05 13:36:27 +00:00
value : function command ( _command , listener ) {
var regex = /[^\s]+/ ;
2015-07-06 00:05:25 +00:00
var cmd = _command . match ( regex ) [ 0 ] . trim ( ) ;
2015-07-05 13:36:27 +00:00
2015-06-28 22:42:48 +00:00
this . _userEvents . push ( {
2015-07-04 19:35:58 +00:00
pattern : new RegExp ( '^/' + cmd ) ,
2015-07-05 13:36:27 +00:00
parse : _functionsArgumentParser2 [ 'default' ] . bind ( null , _command ) ,
2015-06-28 22:42:48 +00:00
listener : listener
} ) ;
return this ;
}
} , {
key : 'send' ,
/ * *
* Sends the message provided
* @ param { object } message The message to send . Gets it ' s send method called
* @ return { unknown } returns the result of calling message ' s send method
* /
value : function send ( message ) {
2015-06-29 01:00:29 +00:00
return message . send ( this ) [ 'catch' ] ( console . error ) ;
2015-06-28 22:42:48 +00:00
}
2015-06-29 22:20:34 +00:00
} , {
key : '_update' ,
2015-06-30 22:04:44 +00:00
/ * *
* The internal update event listener , used to parse messages and fire
* command / get events - YOU SHOULD NOT USE THIS
*
* @ param { object } update
* /
2015-06-29 22:20:34 +00:00
value : function _update ( update ) {
var _this2 = this ;
if ( ! this . update . offset ) {
var updateId = update [ update . length - 1 ] . update _id ;
this . update . offset = updateId ;
}
if ( this . update ) {
this . update . offset += 1 ;
}
2015-07-09 11:37:08 +00:00
console . log ( update ) ;
2015-06-29 22:20:34 +00:00
update . forEach ( function ( res ) {
2015-07-09 11:16:27 +00:00
var marked3$0 = [ getAnswer ] . map ( regeneratorRuntime . mark ) ;
2015-06-29 22:20:34 +00:00
var text = res . message . text ;
2015-07-06 22:43:33 +00:00
if ( ! text ) return ;
2015-07-05 11:08:07 +00:00
var selfUsername = '@' + _this2 . info . username ;
if ( text . startsWith ( '/' ) && text . indexOf ( selfUsername ) > - 1 ) {
2015-06-29 22:20:34 +00:00
// Commands are sent in /command@thisusername format in groups
2015-07-04 19:35:58 +00:00
var regex = new RegExp ( '(/.*)@' + _this2 . info . username ) ;
text = text . replace ( regex , '$1' ) ;
2015-07-04 15:53:37 +00:00
res . message . text = text ;
2015-06-29 22:20:34 +00:00
}
var ev = _this2 . _userEvents . find ( function ( _ref ) {
var pattern = _ref . pattern ;
return pattern . test ( text ) ;
} ) ;
if ( ! ev ) {
2015-07-04 19:35:58 +00:00
_this2 . emit ( 'command-notfound' , res . message ) ;
2015-06-29 22:20:34 +00:00
return ;
}
2015-07-04 19:27:00 +00:00
2015-07-09 11:16:27 +00:00
if ( ! ev . parse ) {
ev . listener ( res . message ) ;
return ;
}
2015-07-09 09:56:17 +00:00
2015-07-09 11:16:27 +00:00
var _ev$parse = ev . parse ( res . message . text ) ;
2015-07-09 09:56:17 +00:00
2015-07-09 11:16:27 +00:00
var params = _ev$parse . params ;
var args = _ev$parse . args ;
2015-07-09 09:56:17 +00:00
2015-07-09 11:37:08 +00:00
res . message . args = args ;
2015-07-09 11:16:27 +00:00
var requiredParams = Object . keys ( params ) . filter ( function ( param ) {
2015-07-09 11:30:01 +00:00
return params [ param ] === REQUIRED && ! args [ param ] ;
2015-07-09 11:16:27 +00:00
} ) ;
if ( ! requiredParams . length ) {
ev . listener ( res . message ) ;
return ;
}
2015-07-09 09:56:17 +00:00
2015-07-09 11:16:27 +00:00
var bot = _this2 ;
function getAnswer ( ) {
var _iteratorNormalCompletion , _didIteratorError , _iteratorError , _loop , _iterator , _step ;
return regeneratorRuntime . wrap ( function getAnswer$ ( context$4$0 ) {
var _this3 = this ;
while ( 1 ) switch ( context$4$0 . prev = context$4$0 . next ) {
case 0 :
_iteratorNormalCompletion = true ;
_didIteratorError = false ;
_iteratorError = undefined ;
context$4$0 . prev = 3 ;
_loop = regeneratorRuntime . mark ( function callee$4$0 ( ) {
var param , msg ;
return regeneratorRuntime . wrap ( function callee$4$0$ ( context$5$0 ) {
while ( 1 ) switch ( context$5$0 . prev = context$5$0 . next ) {
case 0 :
param = _step . value ;
msg = new _typesMessage2 [ 'default' ] ( ) . to ( res . message . chat . id ) . text ( 'Enter value for ' + param ) ;
context$5$0 . next = 4 ;
return bot . send ( msg ) . then ( function ( answer ) {
args [ param ] = answer . text ;
} ) ;
case 4 :
case 'end' :
return context$5$0 . stop ( ) ;
2015-07-09 09:56:17 +00:00
}
2015-07-09 11:16:27 +00:00
} , callee$4$0 , _this3 ) ;
} ) ;
_iterator = requiredParams [ Symbol . iterator ] ( ) ;
2015-07-09 09:56:17 +00:00
2015-07-09 11:16:27 +00:00
case 6 :
if ( _iteratorNormalCompletion = ( _step = _iterator . next ( ) ) . done ) {
context$4$0 . next = 11 ;
break ;
}
return context$4$0 . delegateYield ( _loop ( ) , 't0' , 8 ) ;
case 8 :
_iteratorNormalCompletion = true ;
context$4$0 . next = 6 ;
break ;
case 11 :
context$4$0 . next = 17 ;
break ;
2015-07-09 09:56:17 +00:00
2015-07-09 11:16:27 +00:00
case 13 :
context$4$0 . prev = 13 ;
context$4$0 . t1 = context$4$0 [ 'catch' ] ( 3 ) ;
_didIteratorError = true ;
_iteratorError = context$4$0 . t1 ;
2015-07-09 09:56:17 +00:00
2015-07-09 11:16:27 +00:00
case 17 :
context$4$0 . prev = 17 ;
context$4$0 . prev = 18 ;
if ( ! _iteratorNormalCompletion && _iterator [ 'return' ] ) {
_iterator [ 'return' ] ( ) ;
2015-07-09 09:56:17 +00:00
}
2015-07-09 11:16:27 +00:00
case 20 :
context$4$0 . prev = 20 ;
2015-07-09 09:56:17 +00:00
2015-07-09 11:16:27 +00:00
if ( ! _didIteratorError ) {
context$4$0 . next = 23 ;
break ;
}
2015-07-09 09:56:17 +00:00
2015-07-09 11:16:27 +00:00
throw _iteratorError ;
2015-07-09 09:56:17 +00:00
2015-07-09 11:16:27 +00:00
case 23 :
return context$4$0 . finish ( 20 ) ;
2015-07-09 09:56:17 +00:00
2015-07-09 11:16:27 +00:00
case 24 :
return context$4$0 . finish ( 17 ) ;
case 25 :
case 'end' :
return context$4$0 . stop ( ) ;
}
} , marked3$0 [ 0 ] , this , [ [ 3 , 13 , 17 , 25 ] , [ 18 , , 20 , 24 ] ] ) ;
2015-07-05 13:36:27 +00:00
}
2015-07-09 11:16:27 +00:00
var iterator = getAnswer ( ) ;
( function loop ( ) {
var next = iterator . next ( ) ;
if ( next . done ) {
ev . listener ( res . message ) ;
return ;
}
next . value . then ( loop ) ;
} ) ( ) ;
2015-06-29 22:20:34 +00:00
} ) ;
}
2015-06-28 22:42:48 +00:00
} ] ) ;
return Bot ;
} ) ( _events . EventEmitter ) ;
exports [ 'default' ] = Bot ;
module . exports = exports [ 'default' ] ;