UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

69 lines (62 loc) 2.09 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var axios = require('axios'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios); var WECOM_ROBOT_PREFIX = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key='; function getRobotWebhookUrl(key) { if (key.startsWith('http')) { return key; } return "".concat(WECOM_ROBOT_PREFIX).concat(key); } /** * 给机器人发消息的基本方法 * @private * @param {object} config 输入信息 * @param {string} config.webhookUrl webhook地址 * @param {object} config.params 详细参数 * @returns Promise */ function sendToRobot(_a) { var webhookUrl = _a.webhookUrl, params = _a.params; return new Promise(function (resolve, reject) { if (!webhookUrl) { reject(new Error('缺少 webhookUrl!')); return; } webhookUrl = parsePrefixChatId(webhookUrl); webhookUrl = getRobotWebhookUrl(webhookUrl); if (params.chatid) { params.chatid = parsePrefixChatId(params.chatid); } axios__default["default"].post(webhookUrl, params).then(function (res) { var _a; if (((_a = res === null || res === void 0 ? void 0 : res.data) === null || _a === void 0 ? void 0 : _a.errcode) !== 0) { console.log('[sendToRobot] err: \n', res === null || res === void 0 ? void 0 : res.data); reject(res); } else { resolve(res); } })["catch"](function (err) { console.log('[sendToRobot] err: \n', err === null || err === void 0 ? void 0 : err.data); reject(err); }); }); } /** * 解析chatId,允许chatId头部带有其他前缀 * 比如T_COMM_UPDATE___xxx,前缀可以用来表明chatId来自哪里 * @ignore * */ function parsePrefixChatId(chatId) { var SEPARATOR = '___'; if (chatId.indexOf(SEPARATOR) < 0) { return chatId; } return chatId.split(SEPARATOR)[1]; } exports.getRobotWebhookUrl = getRobotWebhookUrl; exports.sendToRobot = sendToRobot;