-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.js
More file actions
39 lines (35 loc) · 1.22 KB
/
index.js
File metadata and controls
39 lines (35 loc) · 1.22 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
const core = require('@actions/core');
const { TwitterApi } = require('twitter-api-v2');
async function run() {
try {
const message = core.getInput('message');
// 原有功能:打印 message
console.log(message);
// 检查是否配置了 Twitter API 环境变量
const apiKey = process.env.TWITTER_API_KEY;
const apiSecretKey = process.env.TWITTER_API_SECRET_KEY;
const accessToken = process.env.TWITTER_ACCESS_TOKEN;
const accessSecret = process.env.TWITTER_ACCESS_TOKEN_SECRET;
// 只有全部配置了才发推,否则跳过
if (apiKey && apiSecretKey && accessToken && accessSecret) {
try {
const client = new TwitterApi({
appKey: apiKey,
appSecret: apiSecretKey,
accessToken: accessToken,
accessSecret: accessSecret,
});
const tweet = await client.v2.tweet(message);
console.log('Tweet sent:', tweet);
} catch (tweetErr) {
// 发推失败只警告,不让整个 action 失败
console.warn('Failed to send tweet:', tweetErr.message);
}
} else {
console.log('Twitter API credentials not set, skip tweeting.');
}
} catch (error) {
core.setFailed(error.message);
}
}
run();