-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.js
More file actions
214 lines (184 loc) · 5.36 KB
/
Copy pathsync.js
File metadata and controls
214 lines (184 loc) · 5.36 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const axios = require("axios");
const GITHUB_PAT = process.env.GIST_PAT;
const BOT_TOKEN = process.env.BOT_TOKEN;
const TELEGRAM_CHAT_ID = -1003584652910;
const Login = "zsxcoder";
const REPO = "weibo";
// GitHub API 客户端
const github = axios.create({
baseURL: "https://api.github.com/",
headers: {
Accept: "application/json",
Authorization: `bearer ${GITHUB_PAT}`,
},
});
// Telegram Bot API 客户端(替代 slimbot)
const telegram = {
sendMessage: async (chatId, text, config = {}) => {
const url = `https://api.telegram.org/bot${BOT_TOKEN}/sendMessage`;
return axios.post(url, {
chat_id: chatId,
text,
...config,
});
},
sendPhoto: async (chatId, photo) => {
const url = `https://api.telegram.org/bot${BOT_TOKEN}/sendPhoto`;
return axios.post(url, {
chat_id: chatId,
photo,
});
},
};
// 简单验证图片URL(替代 is-image-url)
function isImageUrl(url) {
if (!url) return false;
const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp', '.svg'];
const parsedUrl = url.toLowerCase().split('?')[0]; // 移除查询参数
return imageExtensions.some(ext => parsedUrl.endsWith(ext));
}
async function fetchGraphQL(query, variables = {}) {
if (!GITHUB_PAT) {
throw new Error(
"GitHub Personal Access Token (GITHUB_PAT) not found in environment variables."
);
}
try {
const response = await github.post("/graphql", { query, variables });
const data = response.data;
if (data.errors) {
console.error("GraphQL Errors:", JSON.stringify(data.errors, null, 2));
throw new Error(
`GraphQL request failed: ${data.errors
.map((e) => e.message)
.join(", ")}`
);
}
return data.data;
} catch (error) {
console.error("Error in fetchGraphQL:", error.message);
throw error;
}
}
async function getLatestIssues(owner, repo, count = 6) {
const query = `
query getIssues($owner: String!, $repo: String!, $count: Int!) {
repository(owner: $owner, name: $repo) {
issues(first: $count, orderBy: {field: CREATED_AT, direction: DESC},
filterBy: {createdBy: $owner, states: OPEN}) {
nodes {
title
body
createdAt
url
labels(first: 10) {
nodes {
name
}
}
}
}
}
}
`;
console.log(`Fetching latest ${count} issues from ${owner}/${repo}...`);
const variables = { owner, repo, count };
const data = await fetchGraphQL(query, variables);
if (
data &&
data.repository &&
data.repository.issues &&
data.repository.issues.nodes
) {
const issues = data.repository.issues.nodes;
console.log(`Found ${issues.length} issues.`);
return issues.map((issue) => ({
title: issue.title,
body: issue.body,
createdAt: new Date(issue.createdAt),
url: issue.url,
labels: issue.labels.nodes.map((label) => label.name),
}));
} else {
console.error("Failed to retrieve issues data.");
return [];
}
}
function formatIssueContent(issue) {
const labelsText =
issue.labels.length > 0 ? `Labels: ${issue.labels.join(", ")}\n\n` : "";
return `${issue.body}
---
${labelsText}Original post: https://gwitter.zsxcoder.top `;
}
function formatIssueTitle(issue) {
const date = issue.createdAt.toLocaleDateString("zh-CN", {
year: "numeric",
month: "2-digit",
day: "2-digit",
});
return `${issue.title} - ${date}`;
}
async function sendToTelegram(issue) {
if (!BOT_TOKEN) {
console.log("Telegram bot token not set, skipping Telegram notification");
return;
}
try {
console.log("Sending issue to Telegram group...");
const formattedContent = formatIssueContent(issue);
const telegramMessage = `*${issue.title}*\n\n${formattedContent}`;
const config = {
parse_mode: "Markdown",
disable_web_page_preview: false,
disable_notification: false,
};
await telegram.sendMessage(TELEGRAM_CHAT_ID, telegramMessage, config);
const imageRegex = /(?:!\[(.*?)\]\((.*?)\))/g;
const images = issue.body.match(imageRegex);
if (images && images.length > 0) {
console.log(
`Found ${images.length} images in issue, sending to Telegram...`
);
for (const image of images) {
const url = image.slice(image.indexOf("(") + 1, -1);
if (isImageUrl(url)) {
await telegram.sendPhoto(TELEGRAM_CHAT_ID, url);
}
}
}
console.log("Successfully sent issue to Telegram");
return true;
} catch (error) {
console.error("Error sending to Telegram:", error.message);
if (error.response) {
console.error("Telegram API response:", error.response.data);
}
return false;
}
}
async function main() {
if (!GITHUB_PAT) {
console.error("ERROR: GITHUB_PAT environment variable is not set.");
return;
}
if (!BOT_TOKEN) {
console.error("ERROR: BOT_TOKEN environment variable is not set.");
return;
}
try {
const issues = await getLatestIssues(Login, REPO);
if (issues.length === 0) {
console.error("No issues found. Exiting.");
return;
}
if (issues.length > 0) {
await sendToTelegram(issues[0]);
}
} catch (error) {
console.error("Error in main execution:", error);
}
}
main().catch((error) => {
console.error("Unhandled error in main execution:", error);
});