forked from snsogbl/clip-save
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpushMessageDingtalk.js
More file actions
90 lines (77 loc) · 2.1 KB
/
pushMessageDingtalk.js
File metadata and controls
90 lines (77 loc) · 2.1 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
/**
* 将剪贴板内容推送到钉钉群
* @author ClipSave
* @param {string} access_token - 钉钉机器人 access_token
* @param {string} message - 消息内容,从剪贴板内容获取
* @returns {object} - 推送结果
* @returns {object} - 错误信息
*/
import { csRequest } from "@clipsave/api";
const access_token = "your-access-token";
// 从剪贴板项获取消息内容
if (item.ContentType !== "Text") {
return {
error: "只支持文本类型的剪贴板内容",
};
}
const message = item.Content || "";
if (!message) {
return {
error: "剪贴板内容为空,无法发送推送",
};
}
// ===== 配置区域 =====
// 钉钉机器人 Webhook URL(从钉钉群机器人设置中获取)
const webhookUrl =
"https://oapi.dingtalk.com/robot/send?access_token=" + access_token;
// ===== 发送消息 =====
try {
// 如果设置了加签密钥,生成签名并添加到 URL
let finalUrl = webhookUrl;
// 构建消息体
const messageBody = {
msgtype: "text",
text: {
content: message,
},
};
// 使用通用 csRequest 函数发送请求
const responseJson = await csRequest(
"POST",
finalUrl,
JSON.stringify({ "Content-Type": "application/json" }),
JSON.stringify(messageBody)
);
// 解析响应
const response = JSON.parse(responseJson);
// 检查 HTTP 状态码
if (response.status !== 200) {
return {
error: `请求失败: ${response.status} ${response.statusText}`,
};
}
// 解析响应体(可能是字符串或对象)
let resultBody = response.body;
if (typeof resultBody === "string") {
try {
resultBody = JSON.parse(resultBody);
} catch (e) {
// 如果不是 JSON,直接使用字符串
}
}
// 检查钉钉返回的错误码
if (resultBody.errcode !== undefined && resultBody.errcode !== 0) {
return {
error: `推送失败: ${resultBody.errmsg || "未知错误"}`,
};
}
return {
success: true,
message: "推送成功",
response: resultBody,
};
} catch (error) {
return {
error: `推送失败: ${error.message || String(error)}`,
};
}