-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #439 from hanshino/feat/quote-upload-image
feat: 圖片上傳更改使用方式
- Loading branch information
Showing
2 changed files
with
31 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
); | ||
} |