From 8ac1f22405d68311b843379f65c79ca9b728ef26 Mon Sep 17 00:00:00 2001 From: fengkx Date: Sun, 23 Dec 2018 23:43:14 +0800 Subject: [PATCH] unsuball with confirmation --- i18n/en.yaml | 4 ++++ i18n/zh-cn.yaml | 4 ++++ index.js | 16 +++++++++++++++- middlewares/confirmation.js | 24 ++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/i18n/en.yaml b/i18n/en.yaml index 1c9ad977284..ecce35d32c7 100644 --- a/i18n/en.yaml +++ b/i18n/en.yaml @@ -26,3 +26,7 @@ SEND_FILE_IMPORT: Send a opml file to import subscriptions RSS_USAGE: USAGE: /rss Output subscriptions list add "raw" to show links USB_ALL_USAGE: USAGE: /allunsub unsubscribe all feeds WELCOME: Welcome to [NodeRSSBot](https://github.com/fengkx/NodeRSSBot) +CONFIRM: Do you want to continue the operation +YES: YES +NO: NO +CANCEL: Canceled diff --git a/i18n/zh-cn.yaml b/i18n/zh-cn.yaml index cd90ff426cd..1b0428cb4af 100644 --- a/i18n/zh-cn.yaml +++ b/i18n/zh-cn.yaml @@ -26,3 +26,7 @@ SEND_FILE_IMPORT: 发送 opml 文件导入订阅源 RSS_USAGE: 使用方法: /rss 输出订阅列表 加 raw 显示链接 USB_ALL_USAGE: 使用方法: /allunsub 退订所有源 WELCOME: 欢迎来到 [NodeRSSBot](https://github.com/fengkx/NodeRSSBot) +CONFIRM: 是否继续当前操作 +YES: 是 +NO: 否 +CANCEL: 取消操作 diff --git a/index.js b/index.js index 932dc8d3630..0a030ec4989 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,8 @@ const { sendError, testUrl, getUrlByTitle, - isAdmin + isAdmin, + confirmation } = require('./middlewares'); @@ -51,6 +52,7 @@ bot.command('start', async (ctx) => { text += `\n${i18n['RSS_USAGE']}` text += `\n${i18n['SEND_FILE_IMPORT']}` text += `\n${i18n['EXPORT']}` + text += `\n${i18n['USB_ALL_USAGE']}` await ctx.replyWithMarkdown(text); }); @@ -77,11 +79,23 @@ bot.command('unsubthis', ); bot.command('allunsub', + sendError, + isAdmin, + // RSS.unsubAll, + confirmation +); + +bot.action('UNSUB_ALL_YES', sendError, isAdmin, RSS.unsubAll ); +bot.action('UNSUB_ALL_NO', async (ctx, next) => { + const cb = ctx.callbackQuery; + const res = await ctx.telegram.answerCallbackQuery(cb.id, i18n['CANCEL']); +}); + bot.command('rss', sendError, isAdmin, diff --git a/middlewares/confirmation.js b/middlewares/confirmation.js index e69de29bb2d..7c7ac48fa3c 100644 --- a/middlewares/confirmation.js +++ b/middlewares/confirmation.js @@ -0,0 +1,24 @@ +const i18n = require('../i18n'); +const {promisify} = require('util'); +const setTimeOutPromise = promisify(setTimeout); + +module.exports = async (ctx, next) => { + await ctx.telegram.deleteMessage(ctx.state.chat.id, ctx.state.processMesId); + await ctx.telegram.sendMessage(ctx.state.chat.id, i18n['CONFIRM'], { + reply_markup: { + 'inline_keyboard': [ + [ + { + text: i18n['YES'], + callback_data: 'UNSUB_ALL_YES' + }, + { + text: i18n['NO'], + callback_data: 'UNSUB_ALL_NO' + } + ] + ] + } + }); + await next(); +};