-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
136 lines (124 loc) · 4.37 KB
/
index.mjs
File metadata and controls
136 lines (124 loc) · 4.37 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import * as Misskey from "misskey-js";
// 環境変数の検証
const requiredEnvVars = [
"MISSKEY_IO_HOST",
"MISSKEY_IO_TOKEN",
"NIJIMISS_MOE_HOST",
"NIJIMISS_MOE_TOKEN",
"NINEVERSE_COM_HOST",
"NINEVERSE_COM_TOKEN",
"MISSKEY_SYSTEMS_HOST",
"MISSKEY_SYSTEMS_TOKEN",
];
requiredEnvVars.forEach((envVar) => {
if (!process.env[envVar]) {
throw new Error(`Environment variable ${envVar} is required`);
}
});
// Misskeyアカウントの作成
const misskeyAccount = (main, host, token) => {
console.log("misskeyAccount host=" + host);
return {
main: main,
apiClient: new Misskey.api.APIClient({ origin: `https://${host}`, credential: token }),
};
};
const misskeyAccounts = [
misskeyAccount(true, process.env.MISSKEY_IO_HOST, process.env.MISSKEY_IO_TOKEN),
// Error: certificate has expired
// misskeyAccount(true, process.env.BACKSPACE_FM_HOST, process.env.BACKSPACE_FM_TOKEN),
misskeyAccount(false, process.env.NIJIMISS_MOE_HOST, process.env.NIJIMISS_MOE_TOKEN),
misskeyAccount(false, process.env.NINEVERSE_COM_HOST, process.env.NINEVERSE_COM_TOKEN),
misskeyAccount(false, process.env.MISSKEY_SYSTEMS_HOST, process.env.MISSKEY_SYSTEMS_TOKEN),
];
const search = async (user, account) => {
console.log("search start: " + JSON.stringify(user));
try {
const users = await account.apiClient.request("users/search-by-username-and-host", {
username: user.username,
host: user.host,
limit: 1,
detail: false,
});
console.log("search end: " + JSON.stringify(users));
return users[0];
} catch (error) {
console.error("Error during search: " + JSON.stringify(error));
throw error;
}
};
// ユーザー検索関数
const searchUser = async (event, body, account) => {
const isSameHost = `https://${event.headers["x-misskey-host"]}` === account.apiClient.origin;
const user = body.type === "followed" ? body.body.user : body.body.note.user;
if (isSameHost) return user;
else return await search(user, account);
};
// フォロー作成関数
async function createFollowing(event, body, account) {
const user = await searchUser(event, body, account);
console.log("user: " + JSON.stringify(user));
await account.apiClient.request("following/create", { userId: user.id });
}
// リアクション作成関数
const reactionsMap = {
":ohayoo:": ":ohayoo:",
ありがとうございます: ":kotira_koso:",
};
async function createReactions(_event, body, account) {
console.log("createReactions: note=" + JSON.stringify(body.body.note));
const text = body.body.note.text;
for (const [keyword, reaction] of Object.entries(reactionsMap)) {
if (text.includes(keyword)) {
try {
const create = await account.apiClient.request("notes/reactions/create", {
noteId: body.body.note.id,
reaction: reaction,
});
console.log(`Reaction '${reaction}' created for keyword '${keyword}': ${JSON.stringify(create)}`);
} catch (error) {
console.error(`Failed to create reaction '${reaction}' for keyword '${keyword}': ${JSON.stringify(error)}`);
}
break;
}
}
}
// イベントハンドラー
export const handler = async (event, _context) => {
try {
console.log(JSON.stringify(event));
let response = {};
if ("body" in event) {
const body = JSON.parse(event.body);
console.log(JSON.stringify(body));
for (const account of misskeyAccounts) {
if (account.main || `https://${event.headers["x-misskey-host"]}` === account.apiClient.origin) {
console.log("run: " + account.apiClient.origin);
try {
if (body.type === "followed" || body.type === "renote") {
await createFollowing(event, body, account);
} else if (body.type === "mention") {
await createReactions(event, body, account);
}
} catch (error) {
console.log(`error '${account.apiClient.origin}': ${JSON.stringify(error)}`);
}
}
}
} else {
response = { message: "no body" };
}
console.log("response: " + JSON.stringify(response));
return {
statusCode: 200,
body: JSON.stringify(response, null, 2),
headers: { "Content-Type": "application/json" },
};
} catch (err) {
return {
statusCode: 400,
body: JSON.stringify(err.message, null, 2),
headers: { "Content-Type": "application/json" },
};
}
};