t-comm
Version:
专业、稳定、纯粹的工具库
227 lines (224 loc) • 6.58 kB
JavaScript
import _regeneratorRuntime from '@babel/runtime/regenerator';
import { _ as __awaiter } from '../tslib.es6-848120af.js';
import { sendToRobot } from './helper.mjs';
import 'axios';
/**
* 企业微信机器人 markdown 消息内容最大长度(单位:字节,UTF-8 编码)
* 官方文档:https://developer.work.weixin.qq.com/document/path/91770
* 预留 256 字节安全余量,避免边界情况(如服务端对 JSON 包体的额外限制)
*/
var MARKDOWN_CONTENT_MAX_BYTES = 4096 - 256;
/**
* 计算字符串的 UTF-8 字节长度
*/
function getUtf8ByteLength(str) {
/** 用于计算 UTF-8 字节长度的编码器(浏览器 / Node 16+ 均内置) */
var textEncoder = typeof TextEncoder !== 'undefined' ? new TextEncoder() : null;
if (textEncoder) {
return textEncoder.encode(str).length;
}
// 兜底:手动计算 UTF-8 字节数
var bytes = 0;
var i = 0;
while (i < str.length) {
var code = str.charCodeAt(i);
if (code < 0x80) {
bytes += 1;
i += 1;
} else if (code < 0x800) {
bytes += 2;
i += 1;
} else if (code >= 0xd800 && code <= 0xdbff) {
// 代理对(emoji 等增补平面字符)
bytes += 4;
i += 2;
} else {
bytes += 3;
i += 1;
}
}
return bytes;
}
/**
* 按 UTF-8 字节长度安全裁剪字符串,确保不会把一个多字节字符截成半截
*/
function sliceByUtf8Bytes(str, maxBytes) {
var bytes = 0;
var i = 0;
while (i < str.length) {
var code = str.charCodeAt(i);
var charBytes = void 0;
var step = 1;
if (code < 0x80) charBytes = 1;else if (code < 0x800) charBytes = 2;else if (code >= 0xd800 && code <= 0xdbff) {
charBytes = 4;
step = 2;
} else charBytes = 3;
if (bytes + charBytes > maxBytes) break;
bytes += charBytes;
i += step;
}
return str.slice(0, i);
}
/**
* 当 markdown 内容超出最大字节长度时进行裁剪,并在结尾追加说明
* @param content 原始 markdown 内容
* @returns 裁剪后的 markdown 内容
*/
function truncateMarkdownContent(content) {
if (!content) return content;
var originalBytes = getUtf8ByteLength(content);
if (originalBytes <= MARKDOWN_CONTENT_MAX_BYTES) {
return content;
}
var tip = "\n\n> \u5185\u5BB9\u8FC7\u957F\u5DF2\u88C1\u526A\uFF08\u539F\u59CB ".concat(originalBytes, " \u5B57\u8282\uFF0C\u4E0A\u9650 ").concat(MARKDOWN_CONTENT_MAX_BYTES, " \u5B57\u8282\uFF09");
var tipBytes = getUtf8ByteLength(tip);
var sliced = sliceByUtf8Bytes(content, MARKDOWN_CONTENT_MAX_BYTES - tipBytes);
return "".concat(sliced).concat(tip);
}
/**
* 给机器人发送普通消息
* @param {Object} config 配置内容
* @param {string} config.webhookUrl - 钩子链接
* @param {string} config.chatId - 会话id
* @param {string} config.alias - 别名
* @param {string} config.content - 内容
* @returns {Promise<object>} Promise
*
* @example
*
* sendWxRobotMsg({
* webhookUrl: 'xxx',
* chatId: 'xxx',
* content: 'xxx',
* alias: 'xxx',
* }).then(() => {
*
* })
*/
function sendWxRobotMsg(_ref) {
var webhookUrl = _ref.webhookUrl,
chatId = _ref.chatId,
alias = _ref.alias,
content = _ref.content;
return new Promise(function (resolve, reject) {
sendToRobot({
webhookUrl: webhookUrl,
params: {
chatid: chatId,
msgtype: 'text',
text: {
content: content,
mentioned_list: Array.isArray(alias) ? alias : [alias]
}
}
}).then(function (res) {
resolve(res);
})["catch"](function (err) {
reject(err);
});
});
}
/**
* 给机器人发送Markdown消息
* @param {Object} config 配置内容
* @param {string} config.webhookUrl - 钩子链接
* @param {string} config.chatId - 会话id
* @param {string} config.content - 内容
* @param {Array<object>} config.attachments - 附加内容
* @returns {Promise<object>} 请求Promise
* @example
*
* sendWxRobotMarkdown({
* webhookUrl: 'xxx',
* chatId: 'xxx',
* content: 'xxx',
* attachments: []
* }).then(() => {
*
* })
*/
function sendWxRobotMarkdown(_ref2) {
var webhookUrl = _ref2.webhookUrl,
chatId = _ref2.chatId,
content = _ref2.content,
attachments = _ref2.attachments,
_ref2$isV = _ref2.isV2,
isV2 = _ref2$isV === void 0 ? false : _ref2$isV;
var safeContent = truncateMarkdownContent(content);
return new Promise(function (resolve, reject) {
sendToRobot({
webhookUrl: webhookUrl,
params: {
chatid: chatId,
msgtype: isV2 ? 'markdown_v2' : 'markdown',
markdown: isV2 ? undefined : {
content: safeContent,
attachments: attachments,
at_short_name: true
},
markdown_v2: isV2 ? {
content: safeContent
} : undefined
}
}).then(function (res) {
resolve(res);
})["catch"](function (err) {
reject(err);
});
});
}
/**
* 给机器人发送图片
* @param {Object} config 配置参数
* @param {string} config.webhookUrl 钩子链接
* @param {string} config.chatId 会话id
* @param {string} config.content 内容
* @param {string} config.md5Val md5内容
* @returns {Promise<object>} 请求Promise
*
* @example
*
* sendWxRobotImg({
* webhookUrl: 'xxx',
* chatId: 'xxx',
* content: 'xxx',
* md5Val: 'xxx'
* }).then(() => {
*
* })
*/
function sendWxRobotImg(_ref3) {
var webhookUrl = _ref3.webhookUrl,
chatId = _ref3.chatId,
content = _ref3.content,
md5Val = _ref3.md5Val;
return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
return _regeneratorRuntime.wrap(function (_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
return _context.abrupt("return", new Promise(function (resolve, reject) {
sendToRobot({
webhookUrl: webhookUrl,
params: {
chatid: chatId,
msgtype: 'image',
image: {
base64: content,
md5: md5Val
}
}
}).then(function (res) {
resolve(res);
})["catch"](function (err) {
console.log('[sendWxRobotImg] err:\n ', err === null || err === void 0 ? void 0 : err.data);
reject(err);
});
}));
case 1:
case "end":
return _context.stop();
}
}, _callee);
}));
}
export { sendWxRobotImg, sendWxRobotMarkdown, sendWxRobotMsg };