Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ad0d975a12 | |||
| 7f07710d23 | |||
| 3b58e7ee2d | |||
| 22ec582e0a | |||
| 05194ad7b8 |
+1
-1
@@ -4,4 +4,4 @@
|
||||
/package-lock.json
|
||||
/config.ts
|
||||
|
||||
log*
|
||||
*log*
|
||||
|
||||
+5
-5
@@ -1,5 +1,5 @@
|
||||
# # ##### ####### ### ##### ####### # # # # # # ###### # #
|
||||
## # # # # # ## # # # # # # # # # # #
|
||||
# ## # # # # # # ###### # # # # # # # # ## # #
|
||||
# ## # # # # ## # # # # # # # # # #
|
||||
# # ##### # ### ##### ####### ###### # # ###### ###### #######
|
||||
# # ###### ##### ### ###### ####### # # # # # # ###### # #
|
||||
## # # # # # # # # # # # # # # # # #
|
||||
# ## # # # # # # ####### # # # # # # # # ## # #
|
||||
# ## # # # # # # # # # # # # # # #
|
||||
# # ###### # ### ###### ####### ###### # # ###### ###### ######
|
||||
|
||||
@@ -22,6 +22,20 @@ const config: configTypes = {
|
||||
splitCount: 4, // 返信の分割数
|
||||
},
|
||||
|
||||
// 緊急時設定
|
||||
emergency: {
|
||||
function: true, // 緊急時のコンソール表示
|
||||
mail: {
|
||||
function: true, // 緊急時のメール送信
|
||||
host: "smtp.example.com", // SMTPサーバー
|
||||
port: 465, // SMTPポート
|
||||
user: "mailUser@example.com", // BOTメール送信元
|
||||
password: "mailPassword", // SMTPパスワード
|
||||
secure: false, // SMTPsecure設定
|
||||
to: "admin@noticeuwuzu.example.com" // 緊急時メール送信先(配列可)
|
||||
}
|
||||
},
|
||||
|
||||
apiToken: "TOKEN_EXAMPLE", // BOTアカウントのAPIトークン
|
||||
uwuzuServer: "uwuzu.example.com", // uwuzuのサーバー
|
||||
};
|
||||
|
||||
@@ -13,6 +13,10 @@ asciiArt();
|
||||
// フォローバック機能読み込み
|
||||
import followBack from "./scripts/followBack.js";
|
||||
|
||||
// 正常終了確認読み込み
|
||||
import successExit from "./scripts/successExit.js";
|
||||
successExit();
|
||||
|
||||
// 地震情報観測開始
|
||||
earthquakeNotice();
|
||||
|
||||
|
||||
+4
-1
@@ -1,11 +1,12 @@
|
||||
{
|
||||
"name": "noticeuwuzu",
|
||||
"version": "v4.3.2@uwuzu1.5.4",
|
||||
"version": "v5.0.3@uwuzu1.5.4",
|
||||
"description": "uwuzu Notice Bot",
|
||||
"main": "dist/main.js",
|
||||
"scripts": {
|
||||
"start": "node .",
|
||||
"build": "tsc",
|
||||
"main": "tsc && node .",
|
||||
"dev": "tsx main.ts"
|
||||
},
|
||||
"keywords": [
|
||||
@@ -28,9 +29,11 @@
|
||||
"@types/date-fns": "^2.5.3",
|
||||
"@types/dotenv": "^6.1.1",
|
||||
"@types/node-cron": "^3.0.11",
|
||||
"@types/nodemailer": "^6.4.17",
|
||||
"date-fns": "^4.1.0",
|
||||
"fs": "^0.0.1-security",
|
||||
"node-cron": "^4.1.1",
|
||||
"nodemailer": "^7.0.4",
|
||||
"tsx": "^4.20.3",
|
||||
"typescript": "^5.8.3",
|
||||
"ws": "^8.18.3"
|
||||
|
||||
+62
-15
@@ -1,5 +1,6 @@
|
||||
import WebSocket from "ws";
|
||||
import { differenceInMinutes, subMinutes } from "date-fns";
|
||||
import sendMail from "../src/mailer.js";
|
||||
|
||||
import config from "../config.js";
|
||||
|
||||
@@ -156,32 +157,38 @@ async function event(earthquakeInfo: any): Promise<void> {
|
||||
// 地震詳細
|
||||
let descriptionEarthquake: string = "";
|
||||
|
||||
if (earthquakeInfo.earthquake.description !== "") {
|
||||
if (
|
||||
earthquakeInfo.earthquake.description !== "" ||
|
||||
earthquakeInfo.earthquake.description !== undefined
|
||||
) {
|
||||
descriptionEarthquake = `この地震について:${earthquakeInfo.earthquake.description}`;
|
||||
}
|
||||
|
||||
// 発令詳細
|
||||
let description: string = "";
|
||||
|
||||
if (earthquakeInfo.comments.freeFormComment !== "") {
|
||||
if (
|
||||
earthquakeInfo.comments.freeFormComment !== "" ||
|
||||
earthquakeInfo.comments.freeFormComment !== undefined
|
||||
) {
|
||||
description = `この発令について:${earthquakeInfo.comments.freeFormComment}`;
|
||||
}
|
||||
|
||||
// テスト・訓練
|
||||
let test: string = "";
|
||||
|
||||
if (earthquakeInfo.test) {
|
||||
if (earthquakeInfo.test !== undefined) {
|
||||
test = "この情報にテスト・訓練かの情報はありません";
|
||||
} else if (earthquakeInfo.test) {
|
||||
test = "これはテスト、あるいは訓練です";
|
||||
} else if (earthquakeInfo.test === false) {
|
||||
test = "これはテスト・訓練ではありません";
|
||||
} else {
|
||||
test = "この情報にテスト・訓練かの情報はありません";
|
||||
}
|
||||
|
||||
// 対象地域
|
||||
let areas: string = "";
|
||||
|
||||
if (earthquakeInfo.areas !== null) {
|
||||
if (earthquakeInfo.areas !== undefined) {
|
||||
const areaNames: Array<string> = Array.from(
|
||||
new Set(
|
||||
earthquakeInfo.areas.map((point: any) => point.name).filter(Boolean),
|
||||
@@ -202,7 +209,7 @@ async function event(earthquakeInfo: any): Promise<void> {
|
||||
|
||||
if (
|
||||
earthquakeInfo.earthquake.hypocenter.magnitude !== -1 ||
|
||||
earthquakeInfo.earthquake.hypocenter.magnitude === null
|
||||
earthquakeInfo.earthquake.hypocenter.magnitude === undefined
|
||||
) {
|
||||
magnitude += "マグニチュードの情報はありません";
|
||||
} else {
|
||||
@@ -226,7 +233,9 @@ async function event(earthquakeInfo: any): Promise<void> {
|
||||
// 国内津波
|
||||
let domesticTsunami;
|
||||
|
||||
if (earthquakeInfo.earthquake.domesticTsunami === "None") {
|
||||
if (earthquakeInfo.earthquake.domesticTsunami === undefined) {
|
||||
domesticTsunami = "この地震による国内の津波情報はありません";
|
||||
} else if (earthquakeInfo.earthquake.domesticTsunami === "None") {
|
||||
domesticTsunami = "この地震による国内の津波の心配はありません";
|
||||
} else if (earthquakeInfo.earthquake.domesticTsunami === "Unknown") {
|
||||
domesticTsunami = "この地震による国内の津波情報はありません";
|
||||
@@ -239,21 +248,22 @@ async function event(earthquakeInfo: any): Promise<void> {
|
||||
domesticTsunami = "この地震により国内で津波注意報が発令しています";
|
||||
} else if (earthquakeInfo.earthquake.domesticTsunami === "Warning") {
|
||||
domesticTsunami = "この地震による国内の津波予報があります";
|
||||
} else {
|
||||
domesticTsunami = "この地震による国内の津波情報はありません";
|
||||
}
|
||||
|
||||
// 最大震度
|
||||
let maxScale: string = "最大深度:";
|
||||
|
||||
if (earthquakeInfo.earthquake.maxScale < config.earthquake.maxScaleMin) {
|
||||
if (
|
||||
earthquakeInfo.earthquake.maxScale !== undefined &&
|
||||
earthquakeInfo.earthquake.maxScale < config.earthquake.maxScaleMin
|
||||
) {
|
||||
console.log("最低震度に満たしていないため投稿されませんでした");
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
earthquakeInfo.earthquake.maxScale === -1 ||
|
||||
earthquakeInfo.earthquake.maxScale === null
|
||||
earthquakeInfo.earthquake.maxScale === -1 &&
|
||||
earthquakeInfo.earthquake.maxScale === undefined
|
||||
) {
|
||||
maxScale = "最大震度情報なし";
|
||||
} else if (earthquakeInfo.earthquake.maxScale === 10) {
|
||||
@@ -276,10 +286,42 @@ async function event(earthquakeInfo: any): Promise<void> {
|
||||
maxScale += "震度7";
|
||||
}
|
||||
|
||||
// 警告
|
||||
if (
|
||||
earthquakeInfo.earthquake.maxScale !== undefined &&
|
||||
earthquakeInfo.earthquake.maxScale >= 60 &&
|
||||
config.emergency.function
|
||||
) {
|
||||
console.log("----------------");
|
||||
|
||||
console.log("震度6強以上の地震を受信しました");
|
||||
console.log("サーバーがダウンする可能性があります");
|
||||
|
||||
// メール送信
|
||||
if (config.emergency.function) {
|
||||
sendMail({
|
||||
to: config.emergency.mail.to,
|
||||
subject: "【警告】震度6強以上の地震を受信しました",
|
||||
html: `
|
||||
※noticeUwuzu自動送信によるメールです。
|
||||
【警告】
|
||||
BOT管理者さん、noticeUwuzu自動送信メールです。
|
||||
震度6強以上の地震を受信したため警告メールが送信されました。
|
||||
物理、システム的にサーバーがダウンする可能性があります。
|
||||
ご自身の身をお守りください。
|
||||
`
|
||||
});
|
||||
|
||||
console.log("管理者へ警告メールを送信しました");
|
||||
}
|
||||
|
||||
console.log("----------------");
|
||||
}
|
||||
|
||||
// 対象地域
|
||||
let areas: string = "";
|
||||
|
||||
if (earthquakeInfo.points !== null) {
|
||||
if (earthquakeInfo.points !== undefined) {
|
||||
const areaNames: Array<string> = Array.from(
|
||||
new Set(
|
||||
earthquakeInfo.points.map((point: any) => point.addr).filter(Boolean),
|
||||
@@ -291,7 +333,10 @@ async function event(earthquakeInfo: any): Promise<void> {
|
||||
// 詳細
|
||||
let description: string = "";
|
||||
|
||||
if (earthquakeInfo.comments.freeFormComment !== "") {
|
||||
if (
|
||||
earthquakeInfo.comments.freeFormComment !== "" &&
|
||||
earthquakeInfo.comments.freeFormComment !== undefined
|
||||
) {
|
||||
description = `この地震について:${earthquakeInfo.comments.freeFormComment}`;
|
||||
}
|
||||
|
||||
@@ -300,6 +345,7 @@ async function event(earthquakeInfo: any): Promise<void> {
|
||||
|
||||
if (
|
||||
earthquakeInfo.earthquake.hypocenter.depth !== null ||
|
||||
earthquakeInfo.earthquake.hypocenter.depth !== undefined ||
|
||||
earthquakeInfo.earthquake.hypocenter.depth !== -1
|
||||
) {
|
||||
if (earthquakeInfo.earthquake.hypocenter.depth === 0) {
|
||||
@@ -314,6 +360,7 @@ async function event(earthquakeInfo: any): Promise<void> {
|
||||
|
||||
if(
|
||||
earthquakeInfo.earthquake.hypocenter.magnitude !== null ||
|
||||
earthquakeInfo.earthquake.hypocenter.magnitude !== undefined ||
|
||||
earthquakeInfo.earthquake.hypocenter.magnitude !== -1
|
||||
) {
|
||||
magnitude = `マグニチュード:${earthquakeInfo.earthquake.hypocenter.magnitude}`;
|
||||
|
||||
@@ -16,7 +16,7 @@ export default async function followBack() {
|
||||
|
||||
const meData: types.meApi = await resMe.json();
|
||||
|
||||
console.log(`BOTプロフィール:${meData}`);
|
||||
console.log(`BOTプロフィール:${JSON.stringify(meData)}`);
|
||||
|
||||
const followers: Array<string> = meData.follower;
|
||||
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
import * as fs from "fs";
|
||||
import { format, isAfter } from "date-fns";
|
||||
import { parse } from "date-fns/fp";
|
||||
|
||||
import config from "../config.js";
|
||||
import sendMail from "../src/mailer.js";
|
||||
|
||||
const formatParse = parse(new Date(), "yyyy-MM-dd HH:mm:ss.SSS")
|
||||
|
||||
// 初期化
|
||||
if (fs.existsSync("iolog.json") === false) {
|
||||
fs.writeFileSync("iolog.json", JSON.stringify({
|
||||
start: format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"),
|
||||
stop: "",
|
||||
}), "utf-8");
|
||||
}
|
||||
|
||||
export default function successExit() {
|
||||
const iolog = JSON.parse(fs.readFileSync("iolog.json", "utf-8"));
|
||||
|
||||
if (config.emergency.function) {
|
||||
// 前回の終了確認
|
||||
const start = formatParse(iolog.start);
|
||||
const stop = formatParse(iolog.stop);
|
||||
|
||||
if (isAfter(start, stop)) {
|
||||
console.log("前回の終了が適切でない可能性があります");
|
||||
|
||||
if (config.emergency.mail.function) {
|
||||
sendMail({
|
||||
to: config.emergency.mail.to,
|
||||
subject: "【警告】前回終了が不適切な可能性",
|
||||
html: `
|
||||
※noticeUwuzu自動送信によるメールです。
|
||||
【警告】
|
||||
BOT管理者さん、noticeUwuzu自動送信メールです。
|
||||
BOTの前回終了で不適切なデータを検出しました。
|
||||
これは適切な終了時にはデータを残しデータがない場合に送信されます。
|
||||
電源を強制的に遮断するなどの行為による可能性があります。
|
||||
その場合は今後やめ、OSからのシャットダウンを使用してください。
|
||||
BOTのプログラムが破損していないかご確認ください。
|
||||
`
|
||||
});
|
||||
}
|
||||
|
||||
console.log("----------------");
|
||||
}
|
||||
}
|
||||
|
||||
// 起動時に起動時刻を保存
|
||||
iolog.start = format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
|
||||
fs.writeFileSync("iolog.json", JSON.stringify(iolog), "utf-8");
|
||||
|
||||
// 終了時に終了時刻を保存
|
||||
process.on("exit", () => {
|
||||
const iolog = JSON.parse(fs.readFileSync("iolog.json", "utf-8"));
|
||||
iolog.stop = format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
|
||||
|
||||
fs.writeFileSync("iolog.json", JSON.stringify(iolog), "utf-8");
|
||||
});
|
||||
}
|
||||
|
||||
successExit();
|
||||
+18
-20
@@ -3,29 +3,26 @@ import { format } from "date-fns";
|
||||
import type * as types from "types/types";
|
||||
import config from "../config.js";
|
||||
|
||||
|
||||
// 停止時間
|
||||
// 時刻取得
|
||||
const start = config.time.stopTimes.start;
|
||||
const stop = config.time.stopTimes.stop;
|
||||
|
||||
// 現在の時間を取得
|
||||
const nowHour = new Date().getHours();
|
||||
|
||||
// 停止時刻内かどうかの判定
|
||||
let inRange: boolean = false;
|
||||
|
||||
if (start < stop) {
|
||||
inRange = nowHour >= start && nowHour < stop;
|
||||
} else {
|
||||
inRange = nowHour >= start || nowHour < stop;
|
||||
}
|
||||
|
||||
|
||||
export default async function timeNotice() {
|
||||
console.log("----------------");
|
||||
// 停止時間
|
||||
// 時刻取得
|
||||
const start = config.time.stopTimes.start;
|
||||
const stop = config.time.stopTimes.stop;
|
||||
|
||||
// 現在の時間を取得
|
||||
const nowHour = new Date().getHours();
|
||||
|
||||
// 停止時刻内かどうかの判定
|
||||
let inRange: boolean = false;
|
||||
|
||||
if (start < stop) {
|
||||
inRange = nowHour >= start && nowHour < stop;
|
||||
} else {
|
||||
inRange = nowHour >= start || nowHour < stop;
|
||||
}
|
||||
|
||||
if (inRange) {
|
||||
console.log("----------------");
|
||||
console.log("時報休止期間のため投稿されませんでした");
|
||||
return;
|
||||
} else {
|
||||
@@ -43,6 +40,7 @@ export default async function timeNotice() {
|
||||
|
||||
const ueuseData: types.ueuseCreateApi = await resUeuse.json();
|
||||
|
||||
console.log("----------------");
|
||||
console.log(`時報投稿:${JSON.stringify(ueuseData)}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import config from "../config.js";
|
||||
import * as nodemailer from "nodemailer";
|
||||
|
||||
import type SMTPTransport from "nodemailer/lib/smtp-transport";
|
||||
|
||||
export interface EmailMessage {
|
||||
to: string | string[];
|
||||
subject: string;
|
||||
text?: string;
|
||||
html?: string;
|
||||
}
|
||||
|
||||
async function createTransporter() {
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: config.emergency.mail.host,
|
||||
port: config.emergency.mail.port,
|
||||
secure: config.emergency.mail.secure,
|
||||
auth: {
|
||||
user: config.emergency.mail.user,
|
||||
pass: config.emergency.mail.password,
|
||||
},
|
||||
} as SMTPTransport.Options);
|
||||
|
||||
// 接続テスト
|
||||
try {
|
||||
await transporter.verify();
|
||||
console.log("SMTPサーバーに接続できました");
|
||||
} catch (error) {
|
||||
console.error("SMTP接続テストに失敗:", error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
return transporter;
|
||||
}
|
||||
|
||||
export default async function sendMail(message: EmailMessage): Promise<void> {
|
||||
try {
|
||||
const transporter = await createTransporter();
|
||||
|
||||
await transporter.sendMail({
|
||||
from: config.emergency.mail.user,
|
||||
to: Array.isArray(message.to) ? message.to.join(",") : message.to,
|
||||
subject: message.subject,
|
||||
text: message.text,
|
||||
html: message.html,
|
||||
});
|
||||
console.log("メール送信成功");
|
||||
} catch (error) {
|
||||
console.error("メール送信に失敗しました:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
Vendored
+17
@@ -19,11 +19,28 @@ interface timeTypes {
|
||||
stopTimes: stopsTypes;
|
||||
}
|
||||
|
||||
interface emergencyMailTypes {
|
||||
function: Boolean;
|
||||
host: string | undefined;
|
||||
port: number;
|
||||
user: string;
|
||||
password: string;
|
||||
secure: Boolean;
|
||||
to: string;
|
||||
}
|
||||
|
||||
interface emergencyTypes {
|
||||
function: Boolean;
|
||||
mail: emergencyMailTypes;
|
||||
}
|
||||
|
||||
export interface configTypes {
|
||||
time: timeTypes,
|
||||
earthquake: earthquakeTypes;
|
||||
weather: weatherTypes;
|
||||
|
||||
emergency: emergencyTypes;
|
||||
|
||||
apiToken: string;
|
||||
uwuzuServer: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user