Skip to content

Commit

Permalink
Merge pull request #45 from k-kozika/codespace-improved-acorn-rjg647p…
Browse files Browse the repository at this point in the history
…965wfxpjw

Fix translation flow
  • Loading branch information
k-kozika authored Jan 18, 2025
2 parents 825b8ca + 6443aee commit 85a5418
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function doPost(e: GoogleAppsScript.Events.DoPost) {
const events = data.events;

const engine = new Pipeline(...handlers);
await Promise.all(events.map((event) => engine.execute(event)));
await Promise.all(events.map((event) => engine.execute([event, []])));
})();
}

Expand Down
18 changes: 8 additions & 10 deletions src/handlers/definition.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import type { Handler } from ".";
import { getTextFromEvent, reply, sendLoader } from "../lib/line";
import { translate } from "../lib/translator";
import { getDefinitions } from "../lib/wordnik";

export const definitionHandler: Handler = async (event, next) => {
export const definitionHandler: Handler = async (
[event, [translation, lang]],
next,
) => {
if (event.type !== "message") return next();
if (event.message.type !== "text") return next();
if (event.source.type !== "user") return next();

const text = getTextFromEvent(event);

const translated = translate(text);
if (!translated.isEng) return next();
if (lang !== "en") return next();

sendLoader(event.source.userId);

try {
let definitions;
let definitions: ReturnType<typeof getDefinitions>;
try {
definitions = getDefinitions(text);
} catch {
Expand Down Expand Up @@ -66,9 +66,7 @@ export const definitionHandler: Handler = async (event, next) => {
{
type: "text",
text:
definition.examples
.map((example) => `- ${example}`)
.join("\n") === ""
definition.examples.length === 0
? "例文は見つかりませんでした"
: definition.examples
.map((example) => `- ${example}`)
Expand Down Expand Up @@ -107,7 +105,7 @@ export const definitionHandler: Handler = async (event, next) => {
},
{
type: "span",
text: translated.res,
text: translation,
},
],
weight: "bold",
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/groupGreet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Handler } from ".";
import { countMembersInGroup, leaveGroup, reply } from "../lib/line";

export const groupGreet: Handler = async (event, next) => {
export const groupGreet: Handler = async ([event], next) => {
next();
if (event.type !== "join" || event.source.type !== "group") return;

Expand Down
2 changes: 1 addition & 1 deletion src/handlers/groupLeave.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Handler } from ".";
import { countMembersInGroup, leaveGroup, reply } from "../lib/line";

export const groupLeave: Handler = async (event, next) => {
export const groupLeave: Handler = async ([event], next) => {
next();
if (event.type !== "memberLeft" || event.source.type !== "group") return;

Expand Down
2 changes: 1 addition & 1 deletion src/handlers/groupNotice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { reply } from "../lib/line";

let messageSent = false;

export const groupNotice: Handler = async (event, next) => {
export const groupNotice: Handler = async ([event], next) => {
next();
if (event.type !== "memberJoined") return;
if (messageSent) return;
Expand Down
8 changes: 6 additions & 2 deletions src/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ import { groupGreet } from "./groupGreet";
import { groupLeave } from "./groupLeave";
import { groupNotice } from "./groupNotice";
import { synonymHandler } from "./synonym";
import { translationHandler } from "./translationHandler";

export type Handler = Middleware<webhook.Event>;
type LanguageType = "ja" | "en" | "";
type TranslationCache = string[];
export type Handler = Middleware<[webhook.Event, TranslationCache]>;
export const handlers: Handler[] = [
// messageNotifier,
pronounceAudio,
// friendNotifier,
// groupNotifier,
groupGreet,
groupNotice,
groupLeave,
groupNotice,
translationHandler,
definitionHandler,
synonymHandler,
urlTranslator,
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/pronounceAudio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Handler } from ".";
import { reply, sendLoader } from "../lib/line";
import { getAudio } from "../lib/wordnik";

export const pronounceAudio: Handler = async (event, next) => {
export const pronounceAudio: Handler = async ([event], next) => {
if (event.type !== "postback") return next();
if(event.source.type !== "user") return next();

Expand Down
19 changes: 10 additions & 9 deletions src/handlers/synonym.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import type { Handler } from ".";
import { getTextFromEvent, reply, sendLoader } from "../lib/line";
import { translate } from "../lib/translator";
import { getRelatedWords } from "../lib/wordnik";

export const synonymHandler: Handler = async (event, next) => {
export const synonymHandler: Handler = async (
[event, [translated, language]],
next,
) => {
if (event.type !== "message") return next();
if (event.message.type !== "text") return next();
if (event.source.type !== "user") return next();

const text = getTextFromEvent(event);

const translated = translate(text);
if (translated.isEng) return next();
if (language !== "en") return next();

sendLoader(event.source.userId);

try {
const relatedWords = getRelatedWords(translated.res);
const relatedWords = getRelatedWords(translated);
const flex = {
type: "bubble",
body: {
Expand All @@ -31,7 +32,7 @@ export const synonymHandler: Handler = async (event, next) => {
{
type: "span",
style: "italic",
text: translated.res,
text: translated,
},
{
type: "span",
Expand All @@ -58,7 +59,7 @@ export const synonymHandler: Handler = async (event, next) => {
},
{
type: "span",
text: translated.res,
text: translated,
weight: "bold",
},
],
Expand All @@ -67,8 +68,8 @@ export const synonymHandler: Handler = async (event, next) => {
type: "button",
action: {
type: "message",
label: `${translated.res}の意味を調べる`,
text: translated.res,
label: `${translated}の意味を調べる`,
text: translated,
},
},
{ type: "separator" },
Expand Down
15 changes: 15 additions & 0 deletions src/handlers/translationHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Handler } from ".";
import { getTextFromEvent } from "../lib/line";
import { translate } from "../lib/translator";

export const translationHandler: Handler = async ([event, cache], next) => {
if (event.type !== "message") return next();
if (event.message.type !== "text") return next();

const text = getTextFromEvent(event);

const translated = translate(text);

cache.push(translated.res, translated.isJapanese ? "ja" : translated.isEnglish ? "en" : "");
next();
};
13 changes: 6 additions & 7 deletions src/handlers/translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,27 @@ import {
getTextFromEvent,
reply,
} from "../lib/line";
import { translate } from "../lib/translator";

export const translator: Handler = async (event, next) => {
export const translator: Handler = async (
[event, [translated, lang]],
next,
) => {
if (event.type !== "message") return next();
if (event.message.type !== "text") return next();

const text = getTextFromEvent(event);

const translated = translate(text);

const option: Partial<messagingApi.TextMessage> = {};

if (event.source.type === "group") {
const profile = getProfile(event.source.userId);
const senderName = profile.displayName;
const lang = translated.isEng ? "JA" : "EN";
option.sender = {
name: `${
senderName.length <= 15 ? senderName : `${senderName.slice(0, 12)}...`
} (${lang})`,
} (${lang === "ja" ? "EN" : "JA"})`,
// iconUrl: profile.pictureUrl,
};
}
return reply(event.replyToken, createTextMessage(translated.res, option));
return reply(event.replyToken, createTextMessage(translated, option));
};
2 changes: 1 addition & 1 deletion src/handlers/urlTranslator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Handler } from ".";
import { createTextMessage, getTextFromEvent, reply } from "../lib/line";

export const urlTranslator: Handler = (event, next) => {
export const urlTranslator: Handler = ([event], next) => {
if (event.type !== "message") return next();
if (event.message.type !== "text") return next();

Expand Down
11 changes: 7 additions & 4 deletions src/lib/translator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export const translate = (text: string) => {
const translate = LanguageApp.translate(text, "", "en");
const translate = LanguageApp.translate(text, "", "ja");

const isEng = text.toLowerCase().trim() === translate.toLowerCase().trim();
const res = isEng ? LanguageApp.translate(text, "en", "ja") : translate;
return { res, isEng };
const isJapanese =
text.toLowerCase().trim() === translate.toLowerCase().trim();
const enRes = LanguageApp.translate(text, "", "en");
const isEnglish = text.toLowerCase().trim() === enRes.toLowerCase().trim();
const res = isJapanese ? enRes : translate;
return { res, isJapanese, isEnglish };
};

0 comments on commit 85a5418

Please sign in to comment.