Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
fix: bugs and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ilbertt committed Aug 20, 2021
1 parent 63389a6 commit 4f5bf92
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 51 deletions.
124 changes: 73 additions & 51 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ const fileHandler = async (ctx, fileType) => {
return;
};

const getGenericErrorWithCommands = async (ctx) => {
const commands = await ctx.getMyCommands();
const availableCommands = commands.map(cmd => `/${cmd.command} - ${cmd.description}`).join('\n');
return `${constants.genericError}\n${availableCommands}`;
};

const bot = new Telegraf('');
// bot.use(Telegraf.log());
bot.use(new LocalSession({ storage: LocalSession.storageMemory }).middleware());
Expand Down Expand Up @@ -235,17 +241,21 @@ bot.action(constants.thisDirAction, async (ctx) => {
`Moved *${fileName}*\nfrom \`${sourcePath}\`\nto \`${currentPath}\``
);
}
return ctx.editMessageText(
`${message}${constants.currentPathMessage}\`${currentPath}\``,
{
parse_mode: 'Markdown',
reply_markup: {
...Markup.inlineKeyboard([constants.backInlineButton])
.reply_markup,
force_reply: true,
},
}
);
if (message) {
return ctx.editMessageText(
`${message}${constants.currentPathMessage}\`${currentPath}\``,
{
parse_mode: 'Markdown',
reply_markup: {
...Markup.inlineKeyboard([constants.backInlineButton])
.reply_markup,
force_reply: true,
},
}
);
} else {
return ctx.reply(await getGenericErrorWithCommands(ctx));
}
});
bot.action(constants.parentDirAction, async (ctx) => {
const currentPath = helpers.getCurrentPath(ctx);
Expand All @@ -255,34 +265,37 @@ bot.action(constants.parentDirAction, async (ctx) => {
const inlineKeyboardButtons = await filesystem.getKeyboardDirectories(
ctx,
newCurrentPath,
action === constants.EXPLORER_ACTION,
action === constants.EXPLORER_ACTION
action !== constants.SAVE_FILE_ACTION && action !== constants.MKDIR_ACTION,
action !== constants.SAVE_FILE_ACTION && action !== constants.MKDIR_ACTION
);

let message = '';
if (action === constants.MKDIR_ACTION) {
message = constants.createDirMessage;
} else if (action === constants.SAVE_FILE_ACTION) {
message = constants.saveFileMessage;
} else if (action === constants.DELETE_FILE_ACTION) {
message = constants.deleteFileMessage;
} else if (action === constants.DELETE_DIR_ACTION) {
message = constants.deleteDirMessage;
}

return ctx.editMessageText(
`${message}${constants.currentPathMessage}\`${newCurrentPath}\``,
{
parse_mode: 'Markdown',
reply_markup: {
...Markup.inlineKeyboard(inlineKeyboardButtons).reply_markup,
},
}
);
if (message) {
return ctx.editMessageText(
`${message}${constants.currentPathMessage}\`${newCurrentPath}\``,
{
parse_mode: 'Markdown',
reply_markup: {
...Markup.inlineKeyboard(inlineKeyboardButtons).reply_markup,
},
}
);
} else {
return ctx.reply(await getGenericErrorWithCommands(ctx));
}
});
bot.action(constants.backAction, async (ctx) => {
const action = ctx.session.action;
const currentPath = helpers.getCurrentPath(ctx);
const inlineKeyboardButtons = await filesystem.getKeyboardDirectories(
ctx,
currentPath
);

let message = '';
if (action === constants.MKDIR_ACTION) {
Expand All @@ -293,15 +306,24 @@ bot.action(constants.backAction, async (ctx) => {

ctx.session.waitReply = null;

return ctx.editMessageText(
`${message}${constants.currentPathMessage}\`${currentPath}\``,
{
parse_mode: 'Markdown',
reply_markup: {
...Markup.inlineKeyboard(inlineKeyboardButtons).reply_markup,
},
}
);
if (message) {
const currentPath = helpers.getCurrentPath(ctx);
const inlineKeyboardButtons = await filesystem.getKeyboardDirectories(
ctx,
currentPath
);
return ctx.editMessageText(
`${message}${constants.currentPathMessage}\`${currentPath}\``,
{
parse_mode: 'Markdown',
reply_markup: {
...Markup.inlineKeyboard(inlineKeyboardButtons).reply_markup,
},
}
);
} else {
return ctx.reply(await getGenericErrorWithCommands(ctx));
}
});
bot.action(constants.deleteAction, async (ctx) => {
const currentPath = helpers.getCurrentPath(ctx); // format /parentDir/.../childDir/currentDir/
Expand All @@ -325,10 +347,8 @@ bot.action(/^(.?$|[^\/].+)/, async (ctx) => {
const inlineKeyboardButtons = await filesystem.getKeyboardDirectories(
ctx,
newCurrentPath,
action === constants.EXPLORER_ACTION ||
action === constants.DELETE_DIR_ACTION ||
action === constants.RENAME_FILE_ACTION ||
action === constants.SELECT_MOVE_FILE_ACTION,
action !== constants.SAVE_FILE_ACTION &&
action !== constants.MKDIR_ACTION,
action === constants.EXPLORER_ACTION ||
action === constants.DELETE_FILE_ACTION ||
action === constants.RENAME_FILE_ACTION ||
Expand Down Expand Up @@ -399,18 +419,20 @@ bot.action(/^\//, async (ctx) => {
},
}
);
}
const fileMessageId = ctx.callbackQuery.data.split('/')[1];
try {
if (fileMessageId) {
return await ctx.telegram.sendMessage(ctx.chat.id, 'requested file', {
reply_to_message_id: fileMessageId,
});
} else {
throw new Error('File message not found');
} else if (action === constants.EXPLORER_ACTION) {
const fileMessageId = ctx.callbackQuery.data.split('/')[1];
try {
if (fileMessageId) {
return await ctx.telegram.sendMessage(ctx.chat.id, `File: *${fileName}*\nPath: \`${currentPath}\``, {
parse_mode: 'Markdown',
reply_to_message_id: fileMessageId,
});
} else {
throw new Error('File message not found');
}
} catch (err) {
return await ctx.reply('Error: ' + err.message);
}
} catch (err) {
return await ctx.reply('Error: ' + err.message);
}
});
bot.on('text', async (ctx) => {
Expand Down
4 changes: 4 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const deleteFileMessage = 'DELETE FILE:\n';
const moveFileMessage = 'MOVE FILE:\n';

const fileSystemNotFound = 'Filesystem not found';
const genericError = 'Error!\nUse one of the available commands instead:';

/* INLINE BUTTONS */
const mkdirInlineButton = Markup.button.callback(
Expand All @@ -51,6 +52,8 @@ const deleteInlineButton = Markup.button.callback(
);

module.exports = {
dashboardUrl,

thisDirAction,
parentDirAction,
mkdirAction,
Expand Down Expand Up @@ -79,6 +82,7 @@ module.exports = {
moveFileMessage,

fileSystemNotFound,
genericError,

mkdirInlineButton,
parentDirInlineButton,
Expand Down

0 comments on commit 4f5bf92

Please sign in to comment.