-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
122 lines (103 loc) · 3.36 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { authenticateWithToken, setTwitchLoginToUser } from "./handlers.js";
import { DEFAULT_CONTEXT } from "./common/constants.js";
import { SecretManagerServiceClient } from "@google-cloud/secret-manager";
import { Client as TmiClient } from "tmi.js";
import { TranslationServiceClient } from "@google-cloud/translate";
import { getFirestore } from "firebase-admin/firestore";
import { getTwitchOauthToken } from "./service/secret.js";
import { getUidFromBase64 } from "./service/apigateway.js";
import { handleCors } from "./service/cors.js";
import { http } from "@google-cloud/functions-framework";
import { initializeApp } from "firebase-admin/app";
import { translateText } from "./service/translate.js";
const app = initializeApp();
const context = DEFAULT_CONTEXT;
http("translate-text", async (req, res) => {
if (!handleCors(req, res)) return;
// Keep Alive
if (req.query.keepAlive === "true") {
res.status(204).send("");
return;
}
// Validate environment
const { PROJECT_ID } = process.env;
if (typeof PROJECT_ID === "undefined")
throw new Error("PROJECT_ID not provided");
// Validate query
if (typeof req.query.targetLanguageCode !== "string") {
res.status(400).send("Invalid text");
return;
}
if (typeof req.query.text !== "string") {
res.status(400).send("Invalid text");
return;
}
const { targetLanguageCode, text } = req.query;
// Translate text
const translationClient = new TranslationServiceClient();
const response = await translateText(translationClient, {
projectId: PROJECT_ID,
targetLanguageCode,
text,
});
// Compose response
res.send(response);
});
http("send-text-from-bot-to-chat", async (req, res) => {
if (!handleCors(req, res)) return;
// Keep Alive
if (req.query.keepAlive === "true") {
res.status(204).send("");
return;
}
const db = getFirestore(app);
// Validate environment
const { PROJECT_ID } = process.env;
if (typeof PROJECT_ID === "undefined") {
throw new Error("PROJECT_ID not provided");
}
const secretManagerClient = new SecretManagerServiceClient();
const token = await getTwitchOauthToken(secretManagerClient, {
projectId: PROJECT_ID,
});
// Validate query
const idTokenBase64 = req.get("X-Apigateway-Api-Userinfo");
if (typeof idTokenBase64 === "undefined") {
console.error("X-Apigateway-Api-Userinfo missing");
res.status(401).send("Unauthorized");
return;
}
const uid = getUidFromBase64(idTokenBase64);
if (typeof req.query.text !== "string") {
console.error(req.query);
res.status(400).send("Invalid text");
return;
}
const { text } = req.query;
const docRef = await db.collection("userTwitchLogin").doc(uid).get();
const data = docRef.data();
if (typeof data?.login !== "string") {
console.error(data);
throw new Error("Invalid userTwitchLogin");
}
const { login } = data;
const client = new TmiClient({
connection: {
reconnect: true,
secure: true,
},
identity: {
password: `oauth:${token}`,
username: context.botUsername,
},
options: {
debug: true,
messagesLogLevel: "info",
},
});
await client.connect();
await client.say(login, text);
res.status(204).send("");
});
http("set-twitch-login-to-user", setTwitchLoginToUser.bind(undefined, app));
http("authenticate-with-token", authenticateWithToken.bind(undefined, app));