From b7d31e82faac1b4e63ed4d829b4fe0b68d75ad19 Mon Sep 17 00:00:00 2001 From: Reinhart Previano Koentjoro Date: Sat, 15 Oct 2022 11:13:34 +0700 Subject: [PATCH 1/2] feat: Add Misskey notifier --- src/helpers/notifme.ts | 47 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/helpers/notifme.ts b/src/helpers/notifme.ts index 186d36e0..f227786a 100644 --- a/src/helpers/notifme.ts +++ b/src/helpers/notifme.ts @@ -1,4 +1,4 @@ -import NotifmeSdk, { EmailProvider, SlackProvider, SmsProvider } from "notifme-sdk"; +import NotifmeSdk, { EmailProvider, MisskeyProvider, SlackProvider, SmsProvider } from "notifme-sdk"; import axios from "axios"; import type { Channel } from "notifme-sdk"; import { replaceEnvironmentVariables } from "./environment"; @@ -6,6 +6,7 @@ import { getSecret } from "./secrets"; const channels: { email?: Channel; + misskey?: Channel; sms?: Channel; slack?: Channel; } = {}; @@ -226,12 +227,56 @@ export const sendNotification = async (message: string) => { } console.log("Finished sending Discord"); } + if (getSecret("NOTIFICATION_MISSKEY") && getSecret("NOTIFICATION_MISSKEY_INSTANCEURL") && getSecret("NOTIFICATION_MISSKEY_API_KEY")) { + console.log("Sending Misskey"); + const instanceUrl = new URL(getSecret("NOTIFICATION_MISSKEY_INSTANCEURL") as string); + const baseUrl = `${instanceUrl.protocol}://${instanceUrl.hostname}/api`; + if (getSecret("NOTIFICATION_MISSKEY_CHAT") && getSecret("NOTIFICATION_MISSKEY_CHAT_USER_ID")) { + await axios.post( + `${baseUrl}/messaging/messages/create`, + { + userId: getSecret("NOTIFICATION_MISSKEY_CHAT_USER_ID"), + text: message, + } + ); + } + if (getSecret("NOTIFICATION_MISSKEY_NOTE")) { + type MisskeyNoteVisibility = "public" | "home" | "followers" | "specified"; + let visibility: MisskeyNoteVisibility = "public"; + let visibleUserIds: string[] | undefined; + if (getSecret("NOTIFICATION_MISSKEY_NOTE_VISIBILITY")) { + try { + visibility = getSecret("NOTIFICATION_MISSKEY_NOTE_VISIBILITY") as MisskeyNoteVisibility; + } catch (e) { + console.log(`Unsupported Misskey note visibility mode: ${getSecret("NOTIFICATION_MISSKEY_NOTE_VISIBILITY")}`); + } + } + if (visibility == "specified") { + if (Array.isArray(getSecret("NOTIFICATION_MISSKEY_NOTE_VISIBLE_USER_IDS"))) { + visibleUserIds = getSecret("NOTIFICATION_MISSKEY_NOTE_VISIBLE_USER_IDS") + } else { + console.log("Error: No visible user ID list specified"); + } + } + await axios.post( + `${baseUrl}/notes/create`, + { + i: getSecret("NOTIFICATION_MISSKEY_API_KEY") as string, + visibility: visibility, + visibleUserIds: visibleUserIds, + text: message, + } + ); + } + console.log("Success Misskey"); + } if (getSecret("NOTIFICATION_TELEGRAM") && getSecret("NOTIFICATION_TELEGRAM_BOT_KEY")) { console.log("Sending Telegram"); try { await axios.post( `https://api.telegram.org/bot${getSecret("NOTIFICATION_TELEGRAM_BOT_KEY")}/sendMessage`, { + i: getSecret("NOTIFICATION_MISSKEY_API_KEY") as string, parse_mode: "Markdown", disable_web_page_preview: true, chat_id: getSecret("NOTIFICATION_TELEGRAM_CHAT_ID"), From 1081ac13fe316009ceff1a2895711c4e59c70044 Mon Sep 17 00:00:00 2001 From: Reinhart Previano Koentjoro Date: Sat, 15 Oct 2022 11:14:55 +0700 Subject: [PATCH 2/2] fix: Misskey API key placement --- src/helpers/notifme.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/notifme.ts b/src/helpers/notifme.ts index f227786a..57f24489 100644 --- a/src/helpers/notifme.ts +++ b/src/helpers/notifme.ts @@ -235,6 +235,7 @@ export const sendNotification = async (message: string) => { await axios.post( `${baseUrl}/messaging/messages/create`, { + i: getSecret("NOTIFICATION_MISSKEY_API_KEY") as string, userId: getSecret("NOTIFICATION_MISSKEY_CHAT_USER_ID"), text: message, } @@ -276,7 +277,6 @@ export const sendNotification = async (message: string) => { await axios.post( `https://api.telegram.org/bot${getSecret("NOTIFICATION_TELEGRAM_BOT_KEY")}/sendMessage`, { - i: getSecret("NOTIFICATION_MISSKEY_API_KEY") as string, parse_mode: "Markdown", disable_web_page_preview: true, chat_id: getSecret("NOTIFICATION_TELEGRAM_CHAT_ID"),