Skip to content

Commit

Permalink
perf: 新增按钮功能
Browse files Browse the repository at this point in the history
  • Loading branch information
yikZero committed Nov 19, 2024
1 parent 74819f5 commit c94da07
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
11 changes: 8 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,14 @@ async function generateRankingSummary() {
参考: ${hotSearchRanking}`,
});

const fullContent = `💥 *本周影视热榜 (${dateRange.start} - ${dateRange.end})*\n\n${result.text}\n\n#周五下班快乐`;

await sendTelegramNotification(fullContent);
const fullContent = `💥 *本周影视热榜 (${dateRange.start} - ${dateRange.end})*\n\n${result.text}\n\n#周末愉快 #影视热榜`;

await sendTelegramNotification(fullContent, [
{
text: "开始追剧",
url: "https://apps.apple.com/us/app/infuse-video-player/id1136220934",
},
]);

return fullContent;
} catch (error) {
Expand Down
31 changes: 25 additions & 6 deletions notification.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import axios from "axios";

// 定义按钮接口
interface TelegramButton {
text: string;
url: string;
}

export async function sendBarkNotification(title: string, content: string) {
const BARK_KEY = process.env.BARK_KEY;
if (!BARK_KEY) {
Expand All @@ -24,6 +30,7 @@ export async function sendBarkNotification(title: string, content: string) {

export async function sendTelegramNotification(
message: string,
buttons?: TelegramButton[],
chatId?: string | number
) {
const TELEGRAM_BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN;
Expand All @@ -33,20 +40,32 @@ export async function sendTelegramNotification(
}

try {
// 如果没有提供特定的 chatId,则从环境变量中获取默认值
const TELEGRAM_CHAT_ID = chatId || process.env.TELEGRAM_CHAT_ID;
if (!TELEGRAM_CHAT_ID) {
console.error("TELEGRAM_CHAT_ID not found");
return;
}

const requestData: any = {
chat_id: TELEGRAM_CHAT_ID,
text: message,
parse_mode: "Markdown",
};

if (buttons && buttons.length > 0) {
requestData.reply_markup = {
inline_keyboard: [
buttons.map((button) => ({
text: button.text,
url: button.url,
})),
],
};
}

const response = await axios.post(
`https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage`,
{
chat_id: TELEGRAM_CHAT_ID,
text: message,
parse_mode: "Markdown",
}
requestData
);

if (response.status === 200) {
Expand Down

0 comments on commit c94da07

Please sign in to comment.