diff --git a/app/locales/zh_tw.json b/app/locales/zh_tw.json index 4f714d07..fe52cafb 100644 --- a/app/locales/zh_tw.json +++ b/app/locales/zh_tw.json @@ -191,7 +191,8 @@ }, "image": { "upload_start": "準備接收圖片,請直接上傳圖片", - "upload_failed": "上傳圖片失敗,請重新上傳", + "upload_failed": "上傳圖片失敗,請確認是否針對圖片回覆!", + "upload_without_quote": "請直接針對要上傳得圖片回覆 `#圖片上傳`", "upload_success": "上傳圖片成功\n圖片編號 {{ id }}\n圖片網址 {{{ url }}}" }, "creatures": { diff --git a/app/src/controller/application/ImageController.js b/app/src/controller/application/ImageController.js index 95e3cff6..d6d3dcc2 100644 --- a/app/src/controller/application/ImageController.js +++ b/app/src/controller/application/ImageController.js @@ -1,76 +1,49 @@ -const { text, route } = require("bottender/router"); +const { text } = require("bottender/router"); const { getClient } = require("bottender"); -const { get, uniq } = require("lodash"); +const { get } = require("lodash"); const i18n = require("../../util/i18n"); const LineClient = getClient("line"); const imgur = require("../../util/imgur"); const { isImageUrl } = require("../../util/string"); -exports.router = [text(/^[./#]圖片上傳$/, handleStartUpload), isUploading(handleUpload)]; - -function isUploading(action) { - return route(context => { - if (!context.event.isImage) { - return false; - } - - const { userId } = context.event.source; - const imageUpload = get(context.state, "imageUpload", []); - return imageUpload.includes(userId); - }, action); -} +exports.router = [text(/^[./#]圖片上傳$/, handleUpload)]; /** - * 處理圖片上傳的前置作業 + * 處理圖片上傳 * @param {import ("bottender").LineContext} context */ -async function handleStartUpload(context) { - const { userId } = context.event.source; +async function handleUpload(context) { + const { quotedMessageId: id } = context.event.message; - if (!userId) { - return context.replyText(i18n.__("message.user_unreconized")); + if (!id) { + return context.replyText(i18n.__("message.image.upload_without_quote")); } - const imageUpload = get(context.state, "imageUpload", []); - imageUpload.push(userId); - - context.setState({ - imageUpload: uniq(imageUpload), - }); - - return await context.replyText(i18n.__("message.image.upload_start")); -} + try { + const buf = await LineClient.getMessageContent(id); -/** - * 處理圖片上傳 - * @param {import ("bottender").LineContext} context - */ -async function handleUpload(context) { - const { id } = context.event.image; - const buf = await LineClient.getMessageContent(id); + const result = await imgur.upload({ + type: "buffer", + image: buf, + }); - const result = await imgur.upload({ - type: "buffer", - image: buf, - }); + const url = get(result, "data.link"); - const url = get(result, "data.link"); + if (!isImageUrl(url)) { + return context.replyText(i18n.__("message.image.upload_failed")); + } - if (!isImageUrl(url)) { + const imageUpload = get(context.state, "imageUpload", []); + imageUpload.splice(imageUpload.indexOf(context.event.source.userId), 1); + + return context.replyText( + i18n.__("message.image.upload_success", { + id: get(result, "data.id"), + url, + }) + ); + } catch (e) { + console.log(e); return context.replyText(i18n.__("message.image.upload_failed")); } - - const imageUpload = get(context.state, "imageUpload", []); - imageUpload.splice(imageUpload.indexOf(context.event.source.userId), 1); - - context.setState({ - imageUpload: uniq(imageUpload), - }); - - return context.replyText( - i18n.__("message.image.upload_success", { - id: get(result, "data.id"), - url, - }) - ); }