Skip to content

Commit

Permalink
Support multiple commands in a single telegram message.
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown committed Jul 29, 2020
1 parent a3b967a commit 55eaafb
Showing 1 changed file with 91 additions and 87 deletions.
178 changes: 91 additions & 87 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,37 +52,39 @@ router.post('/api/gdurl/tgbot', async ctx => {
if (callback_query) {
const { id, message, data } = callback_query
const chat_id = callback_query.from.id
const [action, fid, target] = data.split(' ').filter(v => v)
if (action === 'count') {
if (counting[fid]) return sm({ chat_id, text: fid + ' 正在统计,请稍等片刻' })
counting[fid] = true
send_count({ fid, chat_id }).catch(err => {
console.error(err)
sm({ chat_id, text: fid + ' 统计失败:' + err.message })
}).finally(() => {
delete counting[fid]
})
} else if (action === 'copy') {
if (COPYING_FIDS[fid]) return sm({ chat_id, text: `正在处理 ${fid} 的复制命令` })
COPYING_FIDS[fid] = true
tg_copy({ fid, target: get_target_by_alias(target), chat_id }).then(task_id => {
task_id && sm({ chat_id, text: `开始复制,任务ID: ${task_id} 可输入 /task ${task_id} 查询进度` })
}).finally(() => COPYING_FIDS[fid] = false)
} else if (action === 'update') {
if (counting[fid]) return sm({ chat_id, text: fid + ' 正在统计,请稍等片刻' })
counting[fid] = true
send_count({ fid, chat_id, update: true }).finally(() => {
delete counting[fid]
})
} else if (action === 'clear_button') {
const { message_id, text } = message || {}
if (message_id) sm({ chat_id, message_id, text, parse_mode: 'HTML' }, 'editMessageText')
for (let line of data.split('\n')) {
const [action, fid, target] = data.split(' ').filter(v => v)
if (action === 'count') {
if (counting[fid]) return sm({ chat_id, text: fid + ' 正在统计,请稍等片刻' })
counting[fid] = true
send_count({ fid, chat_id }).catch(err => {
console.error(err)
sm({ chat_id, text: fid + ' 统计失败:' + err.message })
}).finally(() => {
delete counting[fid]
})
} else if (action === 'copy') {
if (COPYING_FIDS[fid]) return sm({ chat_id, text: `正在处理 ${fid} 的复制命令` })
COPYING_FIDS[fid] = true
tg_copy({ fid, target: get_target_by_alias(target), chat_id }).then(task_id => {
task_id && sm({ chat_id, text: `开始复制,任务ID: ${task_id} 可输入 /task ${task_id} 查询进度` })
}).finally(() => COPYING_FIDS[fid] = false)
} else if (action === 'update') {
if (counting[fid]) return sm({ chat_id, text: fid + ' 正在统计,请稍等片刻' })
counting[fid] = true
send_count({ fid, chat_id, update: true }).finally(() => {
delete counting[fid]
})
} else if (action === 'clear_button') {
const { message_id, text } = message || {}
if (message_id) sm({ chat_id, message_id, text, parse_mode: 'HTML' }, 'editMessageText')
}
}
return reply_cb_query({ id, data }).catch(console.error)
}

const chat_id = message && message.chat && message.chat.id
const text = (message && message.text && message.text.trim()) || ''
const texts = (message && message.text && message.text.trim().split('\n')) || ['']
let username = message && message.from && message.from.username
username = username && String(username).toLowerCase()
let user_id = message && message.from && message.from.id
Expand All @@ -95,71 +97,73 @@ router.post('/api/gdurl/tgbot', async ctx => {
return console.warn('收到非白名单用户的请求')
}

const fid = extract_fid(text) || extract_from_text(text) || extract_from_text(message_str)
const no_fid_commands = ['/task', '/help', '/bm']
if (!no_fid_commands.some(cmd => text.startsWith(cmd)) && !validate_fid(fid)) {
return sm({ chat_id, text: '未识别出分享ID' })
}
if (text.startsWith('/help')) return send_help(chat_id)
if (text.startsWith('/bm')) {
const [cmd, action, alias, target] = text.split(' ').map(v => v.trim()).filter(v => v)
if (!action) return send_all_bookmarks(chat_id)
if (action === 'set') {
if (!alias || !target) return sm({ chat_id, text: '别名和目标ID不能为空' })
if (alias.length > 24) return sm({ chat_id, text: '别名不要超过24个英文字符长度' })
if (!validate_fid(target)) return sm({ chat_id, text: '目标ID格式有误' })
set_bookmark({ chat_id, alias, target })
} else if (action === 'unset') {
if (!alias) return sm({ chat_id, text: '别名不能为空' })
unset_bookmark({ chat_id, alias })
} else {
send_bm_help(chat_id)
for (let text of texts) {
const fid = extract_fid(text) || extract_from_text(text) || extract_from_text(message_str)
const no_fid_commands = ['/task', '/help', '/bm']
if (!no_fid_commands.some(cmd => text.startsWith(cmd)) && !validate_fid(fid)) {
return sm({ chat_id, text: '未识别出分享ID' })
}
} else if (text.startsWith('/count')) {
if (counting[fid]) return sm({ chat_id, text: fid + ' 正在统计,请稍等片刻' })
try {
counting[fid] = true
if (text.startsWith('/help')) return send_help(chat_id)
if (text.startsWith('/bm')) {
const [cmd, action, alias, target] = text.split(' ').map(v => v.trim()).filter(v => v)
if (!action) return send_all_bookmarks(chat_id)
if (action === 'set') {
if (!alias || !target) return sm({ chat_id, text: '别名和目标ID不能为空' })
if (alias.length > 24) return sm({ chat_id, text: '别名不要超过24个英文字符长度' })
if (!validate_fid(target)) return sm({ chat_id, text: '目标ID格式有误' })
set_bookmark({ chat_id, alias, target })
} else if (action === 'unset') {
if (!alias) return sm({ chat_id, text: '别名不能为空' })
unset_bookmark({ chat_id, alias })
} else {
send_bm_help(chat_id)
}
} else if (text.startsWith('/count')) {
if (counting[fid]) return sm({ chat_id, text: fid + ' 正在统计,请稍等片刻' })
try {
counting[fid] = true
const update = text.endsWith(' -u')
await send_count({ fid, chat_id, update })
} catch (err) {
console.error(err)
sm({ chat_id, text: fid + ' 统计失败:' + err.message })
} finally {
delete counting[fid]
}
} else if (text.startsWith('/copy')) {
let target = text.replace('/copy', '').replace(' -u', '').trim().split(' ').map(v => v.trim()).filter(v => v)[1]
target = get_target_by_alias(target) || target
if (target && !validate_fid(target)) return sm({ chat_id, text: `目标ID ${target} 格式不正确` })
const update = text.endsWith(' -u')
await send_count({ fid, chat_id, update })
} catch (err) {
console.error(err)
sm({ chat_id, text: fid + ' 统计失败:' + err.message })
} finally {
delete counting[fid]
}
} else if (text.startsWith('/copy')) {
let target = text.replace('/copy', '').replace(' -u', '').trim().split(' ').map(v => v.trim()).filter(v => v)[1]
target = get_target_by_alias(target) || target
if (target && !validate_fid(target)) return sm({ chat_id, text: `目标ID ${target} 格式不正确` })
const update = text.endsWith(' -u')
tg_copy({ fid, target, chat_id, update }).then(task_id => {
task_id && sm({ chat_id, text: `开始复制,任务ID: ${task_id} 可输入 /task ${task_id} 查询进度` })
})
} else if (text.startsWith('/task')) {
let task_id = text.replace('/task', '').trim()
if (task_id === 'all') {
return send_all_tasks(chat_id)
} else if (task_id === 'clear') {
return clear_tasks(chat_id)
} else if (task_id === '-h') {
return send_task_help(chat_id)
} else if (task_id.startsWith('rm')) {
task_id = task_id.replace('rm', '')
tg_copy({ fid, target, chat_id, update }).then(task_id => {
task_id && sm({ chat_id, text: `开始复制,任务ID: ${task_id} 可输入 /task ${task_id} 查询进度` })
})
} else if (text.startsWith('/task')) {
let task_id = text.replace('/task', '').trim()
if (task_id === 'all') {
return send_all_tasks(chat_id)
} else if (task_id === 'clear') {
return clear_tasks(chat_id)
} else if (task_id === '-h') {
return send_task_help(chat_id)
} else if (task_id.startsWith('rm')) {
task_id = task_id.replace('rm', '')
task_id = parseInt(task_id)
if (!task_id) return send_task_help(chat_id)
return rm_task({ task_id, chat_id })
}
task_id = parseInt(task_id)
if (!task_id) return send_task_help(chat_id)
return rm_task({ task_id, chat_id })
}
task_id = parseInt(task_id)
if (!task_id) {
const running_tasks = db.prepare('select id from task where status=?').all('copying')
if (!running_tasks.length) return sm({ chat_id, text: '当前暂无运行中的任务' })
return running_tasks.forEach(v => send_task_info({ chat_id, task_id: v.id }).catch(console.error))
if (!task_id) {
const running_tasks = db.prepare('select id from task where status=?').all('copying')
if (!running_tasks.length) return sm({ chat_id, text: '当前暂无运行中的任务' })
return running_tasks.forEach(v => send_task_info({ chat_id, task_id: v.id }).catch(console.error))
}
send_task_info({ task_id, chat_id }).catch(console.error)
} else if (message_str.includes('drive.google.com/') || validate_fid(text)) {
return send_choice({ fid: fid || text, chat_id })
} else {
sm({ chat_id, text: '暂不支持此命令' })
}
send_task_info({ task_id, chat_id }).catch(console.error)
} else if (message_str.includes('drive.google.com/') || validate_fid(text)) {
return send_choice({ fid: fid || text, chat_id })
} else {
sm({ chat_id, text: '暂不支持此命令' })
}
})

Expand Down

0 comments on commit 55eaafb

Please sign in to comment.