@ -37,7 +37,9 @@ const defaultShell = os.platform() === 'win32' ? 'cmd.exe' : 'bash';
bot . command ( 'start' ,
ctx => {
ctx . replyWithHTML ( ` Welcome to tsh -- <code>Telegram Shell!</code> \n \n You are now connected to <code> ${ hostname } </code> as <strong> ${ username } </strong> ` ) ;
ctx . replyWithHTML ( ` Welcome to tsh -- <code>Telegram Shell!</code> \n \n `
+ ` You are now connected to <code> ${ hostname } </code> `
+ ` as <strong> ${ username } </strong> ` ) ;
const newProc = spawn ( defaultShell , {
cwd : home
} ) ;
@ -57,7 +59,8 @@ bot.command('attach',
ctx => {
const text = path ( [ 'update' , 'message' , 'text' ] , ctx ) ;
const sessionIndex = parseInt ( text . replace ( '/attach ' , '' ) . trim ( ) ) ;
if ( Number . isNaN ( sessionIndex ) || ! sessions [ sessionIndex ] ) return responder . fail ( 'Session not found. /ls for list of sessions' ) ( ctx ) ;
if ( Number . isNaN ( sessionIndex ) || ! sessions [ sessionIndex ] )
return responder . fail ( 'Session not found. /ls for list of sessions' ) ( ctx ) ;
sessions . currentSession = sessions [ sessionIndex ] ;
return responder . success ( ` Reattached to shell ${ sessionIndex } ` ) ( ctx ) ;
} ) ;
@ -66,8 +69,11 @@ bot.command('detach',
ctx => {
const text = path ( [ 'update' , 'message' , 'text' ] , ctx ) ;
const sessionIndex = parseInt ( text . replace ( '/detach ' , '' ) . trim ( ) ) ;
const currentSession = text . trim ( ) === '/detach' ? sessions . currentSession : sessions [ sessionIndex ] ;
if ( ! currentSession ) return responder . fail ( 'Session not found. /ls for list of sessions.' ) ( ctx ) ;
const currentSession =
text . trim ( ) === '/detach'
? sessions . currentSession : sessions [ sessionIndex ] ;
if ( ! currentSession )
return responder . fail ( 'Session not found. /ls for list of sessions.' ) ( ctx ) ;
sessions . currentSession = undefined ;
return responder . success ( ` Detached from shell ${ sessionIndex } ` ) ( ctx ) ;
} ) ;
@ -76,7 +82,8 @@ bot.command('kill',
ctx => {
const text = path ( [ 'update' , 'message' , 'text' ] , ctx ) ;
const sessionIndex = parseInt ( text . replace ( '/kill ' , '' ) . trim ( ) ) ;
if ( Number . isNaN ( sessionIndex ) || ! sessions [ sessionIndex ] ) return responder . fail ( 'Session not found. /ls for list of sessions.' ) ( ctx ) ;
if ( Number . isNaN ( sessionIndex ) || ! sessions [ sessionIndex ] )
return responder . fail ( 'Session not found. /ls for list of sessions.' ) ( ctx ) ;
const disconnect = sessions [ sessionIndex ] ;
delete sessions [ sessionIndex ] ;
if ( disconnect === sessions . currentSession ) sessions . currentSession = undefined ;
@ -85,14 +92,16 @@ bot.command('kill',
} )
bot . use ( ctx => {
if ( ! sessions . currentSession ) return responder . fail ( 'No active session. Start one with /start or view list of sessions by sending /ls.' ) ( ctx ) ;
if ( ! sessions . currentSession )
return responder . fail ( ` No active session. `
+ ` Start one with /start or view list of sessions by sending /ls. ` ) ( ctx ) ;
const cmd = ctx . update . message . text ;
const history = ` ${ new Date ( ) . toLocaleDateString ( 'en-IN' , dateOptions ) } : ${ cmd } ` ;
sessions . history . push ( history ) ;
console . log ( history ) ;
sessions . currentSession . stdin . write ( cmd + EOL ) ;
sessions . currentSession . stdout . on ( 'data' , d => responder . success ( d ) ( ctx ) ) ;
sessions . currentSession . stdout . on ( 'error ' , e => responder . success ( e ) ( ctx ) ) ;
sessions . currentSession . stderr . on ( 'data ' , e => responder . success ( e ) ( ctx ) ) ;
} ) ;
bot . startPolling ( ) ;