UNPKG

t-comm

Version:

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

242 lines (235 loc) 8.93 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _regeneratorRuntime = require('@babel/runtime/regenerator'); var tslib_es6 = require('../tslib.es6-0d92ef81.js'); var wecomRobot_base = require('./base.js'); var wecomRobot_sendImg = require('./send-img.js'); require('./helper.js'); require('axios'); require('../nodejs/path.js'); require('../node-img/img.js'); require('../nodejs/crypto.js'); require('../nodejs/fs.js'); require('../nodejs/request.js'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime); function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } var SEND_TO_ALL = 'ALL'; /** * 根据chatId批量发送机器人消息 * - 如果传入的chatId不是数组,会转为数组 * - chatId 不能为空,要群发的话,可以设为 ALL * @param {Function} sendFn 机器人发送消息的方法,如 sendWxRobotBase64Img * @param {Array<string> | string} chatId 会话Id * @param {object} args 发送消息的其他参数 * @ignore */ function batchSendRobotByChatId(sendFn, chatId) { var args = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() { var _iterator, _step, id, _t; return _regeneratorRuntime__default["default"].wrap(function (_context) { while (1) switch (_context.prev = _context.next) { case 0: if (chatId) { _context.next = 1; break; } console.error('Error: chatId 不能为空'); return _context.abrupt("return"); case 1: if (!Array.isArray(chatId)) { chatId = [chatId]; } if (!(chatId.indexOf(SEND_TO_ALL) > -1)) { _context.next = 2; break; } return _context.abrupt("return", sendFn(Object.assign(Object.assign({}, args || {}), { chatId: undefined }))); case 2: _iterator = _createForOfIteratorHelper(chatId); _context.prev = 3; _iterator.s(); case 4: if ((_step = _iterator.n()).done) { _context.next = 6; break; } id = _step.value; _context.next = 5; return sendFn(Object.assign(Object.assign({}, args || {}), { chatId: id })); case 5: _context.next = 4; break; case 6: _context.next = 8; break; case 7: _context.prev = 7; _t = _context["catch"](3); _iterator.e(_t); case 8: _context.prev = 8; _iterator.f(); return _context.finish(8); case 9: case "end": return _context.stop(); } }, _callee, null, [[3, 7, 8, 9]]); })); } /** * 批量发送企业微信机器人base64图片 * - chatId 支持字符串或字符串数组,传 `'ALL'` 或 `['ALL']` 会发送给所有人 * @param {object} config 配置信息 * @param {string} config.img base64图片 * @param {string | Array<string>} config.chatId 会话Id,支持单个字符串、字符串数组、`'ALL'` 或 `['ALL']`(发送给所有人) * @param {string} config.webhookUrl webhook地址 * @returns {Promise<object>} 请求Promise * @example * * // 发送给单个会话 * batchSendWxRobotBase64Img({ * img: 'xxx', * chatId: 'xxx', * webhookUrl: 'xxx', * }); * * // 发送给多个会话 * batchSendWxRobotBase64Img({ * img: 'xxx', * chatId: ['chatId1', 'chatId2'], * webhookUrl: 'xxx', * }); * * // 发送给所有人 * batchSendWxRobotBase64Img({ * img: 'xxx', * chatId: 'ALL', // 或 ['ALL'] * webhookUrl: 'xxx', * }); * */ function batchSendWxRobotBase64Img(_ref) { var img = _ref.img, chatId = _ref.chatId, webhookUrl = _ref.webhookUrl; return batchSendRobotByChatId(wecomRobot_sendImg.sendWxRobotBase64Img, chatId, { img: img, webhookUrl: webhookUrl }); } /** * 批量发送企业微信机器人文本消息 * - chatId 支持字符串或字符串数组,传 `'ALL'` 或 `['ALL']` 会发送给所有人 * @param {object} config 配置信息 * @param {string} config.content 消息内容 * @param {string | Array<string>} config.alias 被@的用户别名,支持单个字符串或字符串数组 * @param {string | Array<string>} config.chatId 会话Id,支持单个字符串、字符串数组、`'ALL'` 或 `['ALL']`(发送给所有人) * @param {string} config.webhookUrl webhook地址 * @returns {Promise<object>} 请求Promise * @example * * // 发送给单个会话 * batchSendWxRobotMsg({ * content: '消息内容', * alias: 'user1', * chatId: 'xxx', * webhookUrl: 'xxx', * }); * * // 发送给多个会话并@多个用户 * batchSendWxRobotMsg({ * content: '消息内容', * alias: ['user1', 'user2'], * chatId: ['chatId1', 'chatId2'], * webhookUrl: 'xxx', * }); * * // 发送给所有人 * batchSendWxRobotMsg({ * content: '消息内容', * alias: 'user1', * chatId: 'ALL', // 或 ['ALL'] * webhookUrl: 'xxx', * }); * */ function batchSendWxRobotMsg(_ref2) { var content = _ref2.content, alias = _ref2.alias, chatId = _ref2.chatId, webhookUrl = _ref2.webhookUrl; return batchSendRobotByChatId(wecomRobot_base.sendWxRobotMsg, chatId, { content: content, alias: alias, webhookUrl: webhookUrl }); } /** * 批量发送企业微信机器人Markdown消息(最常用) * - chatId 支持字符串或字符串数组,传 `'ALL'` 或 `['ALL']` 会发送给所有人 * - 支持 Markdown V2 格式,通过 isV2 参数控制 * @param {object} config 配置信息 * @param {string} config.content Markdown消息内容 * @param {Array<object>} [config.attachments] 附加内容 * @param {string | Array<string>} config.chatId 会话Id,支持单个字符串、字符串数组、`'ALL'` 或 `['ALL']`(发送给所有人) * @param {string} config.webhookUrl webhook地址 * @param {boolean} [config.isV2=false] 是否使用 Markdown V2 格式 * @returns {Promise<object>} 请求Promise * @example * * // 发送给单个会话 * batchSendWxRobotMarkdown({ * content: '## 标题\n内容', * chatId: 'xxx', * webhookUrl: 'xxx', * }); * * // 发送给多个会话 * batchSendWxRobotMarkdown({ * content: '## 标题\n内容', * chatId: ['chatId1', 'chatId2'], * webhookUrl: 'xxx', * }); * * // 发送给所有人 * batchSendWxRobotMarkdown({ * content: '## 标题\n内容', * chatId: 'ALL', // 或 ['ALL'] * webhookUrl: 'xxx', * }); * * // 使用 Markdown V2 格式 * batchSendWxRobotMarkdown({ * content: '## 标题\n内容', * chatId: 'xxx', * webhookUrl: 'xxx', * isV2: true, * }); * */ function batchSendWxRobotMarkdown(_ref3) { var content = _ref3.content, attachments = _ref3.attachments, chatId = _ref3.chatId, webhookUrl = _ref3.webhookUrl, _ref3$isV = _ref3.isV2, isV2 = _ref3$isV === void 0 ? false : _ref3$isV; return batchSendRobotByChatId(wecomRobot_base.sendWxRobotMarkdown, chatId, { content: content, attachments: attachments, webhookUrl: webhookUrl, isV2: isV2 }); } exports.batchSendWxRobotBase64Img = batchSendWxRobotBase64Img; exports.batchSendWxRobotMarkdown = batchSendWxRobotMarkdown; exports.batchSendWxRobotMsg = batchSendWxRobotMsg;