From 193236c6dd36c8956dd70c1c3b62dc276f869e56 Mon Sep 17 00:00:00 2001 From: Muthu Kumar Date: Wed, 18 Jul 2018 14:44:09 +0530 Subject: [PATCH] [response] Added options object to responder --- app.js | 7 +++++-- lib/listeners.js | 4 ++-- lib/responseHandler.js | 17 ++++++++++++----- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/app.js b/app.js index 14c4eda..1db4e3f 100644 --- a/app.js +++ b/app.js @@ -60,7 +60,7 @@ bot.command('start', return responder.success(`Welcome to tsh -- Telegram Shell!\n\n` + `You are now connected to ${runtime.hostname}` + ` as ${runtime.username}.`, - 'html', + { mode: 'html' }, )(ctx); }); @@ -76,7 +76,10 @@ bot.command('save', const identifier = extractCommandText('save')(ctx); if(!identifier) return responder.fail('Need a valid identifier to save session.')(ctx); runtime.currentSession.identifier = identifier; - return responder.success(`Saved session ${identifier}.`, 'html')(ctx); + return responder.success( + `Saved session ${identifier}.`, + { mode: 'html' } + )(ctx); }); bot.command('ls', diff --git a/lib/listeners.js b/lib/listeners.js index 08089ff..8c0c898 100644 --- a/lib/listeners.js +++ b/lib/listeners.js @@ -1,8 +1,8 @@ const addListeners = (emitter, responder, ctx) => { emitter.stdout.setEncoding('utf8'); emitter.stderr.setEncoding('utf8'); - emitter.stdout.on('data', d => responder.success(d)(ctx)); - emitter.stderr.on('data', e => responder.success(e)(ctx)); + emitter.stdout.on('data', d => responder.success(`${d}`, { mode: 'html' })(ctx)); + emitter.stderr.on('data', e => responder.success(`${e}`, { mode: 'html' })(ctx)); }; const removeListeners = emitter => { diff --git a/lib/responseHandler.js b/lib/responseHandler.js index 340e802..9add545 100644 --- a/lib/responseHandler.js +++ b/lib/responseHandler.js @@ -1,9 +1,15 @@ const { EOL } = require('os'); const { path } = require('../util/index.js'); -const success = (response, mode) => ctx => { - const respondAs = (mode && mode.toLowerCase() === 'html') ? 'replyWithHTML' : 'reply'; - return response ? ctx[respondAs](response) : null; +const parseOptions = ({ mode, asReply }, ctx) => { + const respondMode = (mode && mode.toLowerCase() === 'html') ? 'replyWithHTML' : 'reply'; + const options = asReply ? { reply_to_message_id: ctx.message.message_id } : {}; + return [ respondMode, options ]; +} + +const success = (response, options) => ctx => { + const [ respondMode, replyOptions ] = parseOptions(options, ctx); + return response ? ctx[respondMode](response, replyOptions) : null; }; const convertCtx = ctx => { @@ -19,7 +25,8 @@ const convertCtx = ctx => { }, null, 2); }; -const fail = response => ctx => { +const fail = (response, options) => ctx => { + const [ respondMode, replyOptions ] = parseOptions(options, ctx); if(!response || !path(['update', 'message'], ctx) ) return; @@ -30,7 +37,7 @@ const fail = response => ctx => { `With context: `, convertCtx(ctx), ); - return ctx.reply(response); + return ctx[respondMode](response, replyOptions); }; module.exports = {