Skip to content

Commit

Permalink
Merge pull request #439 from hanshino/feat/quote-upload-image
Browse files Browse the repository at this point in the history
feat: 圖片上傳更改使用方式
  • Loading branch information
hanshino authored Sep 25, 2023
2 parents 26d680b + 7e35796 commit 0d1d7d2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 57 deletions.
3 changes: 2 additions & 1 deletion app/locales/zh_tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@
},
"image": {
"upload_start": "準備接收圖片,請直接上傳圖片",
"upload_failed": "上傳圖片失敗,請重新上傳",
"upload_failed": "上傳圖片失敗,請確認是否針對圖片回覆!",
"upload_without_quote": "請直接針對要上傳得圖片回覆 `#圖片上傳`",
"upload_success": "上傳圖片成功\n圖片編號 {{ id }}\n圖片網址 {{{ url }}}"
},
"creatures": {
Expand Down
85 changes: 29 additions & 56 deletions app/src/controller/application/ImageController.js
Original file line number Diff line number Diff line change
@@ -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,
})
);
}

0 comments on commit 0d1d7d2

Please sign in to comment.