Browse Source

[response] Added options object to responder

master
Muthu Kumar 6 years ago
parent
commit
193236c6dd
  1. 7
      app.js
  2. 4
      lib/listeners.js
  3. 17
      lib/responseHandler.js

7
app.js

@ -60,7 +60,7 @@ bot.command('start',
return responder.success(`Welcome to tsh -- <code>Telegram Shell!</code>\n\n`
+ `You are now connected to <code>${runtime.hostname}</code>`
+ ` as <strong>${runtime.username}</strong>.`,
'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 <code>${identifier}</code>.`, 'html')(ctx);
return responder.success(
`Saved session <code>${identifier}</code>.`,
{ mode: 'html' }
)(ctx);
});
bot.command('ls',

4
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(`<code>${d}</code>`, { mode: 'html' })(ctx));
emitter.stderr.on('data', e => responder.success(`<code>${e}</code>`, { mode: 'html' })(ctx));
};
const removeListeners = emitter => {

17
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 = {

Loading…
Cancel
Save