Skip to content

引入

ts
import {
  MARKDOWN_CONTENT_MAX_BYTES,
  getUtf8ByteLength,
  sliceByUtf8Bytes,
  truncateMarkdownContent,
  sendWxRobotMsg,
  sendWxRobotMarkdown,
  sendWxRobotImg,
  batchSendWxRobotBase64Img,
  batchSendWxRobotMsg,
  batchSendWxRobotMarkdown,
  getWxWorkAddChatLink,
  getWxWorkMessageLink,
  sendWxRobotBase64Img
} from 't-comm';

// 不支持 tree-shaking 的项目
import {
  MARKDOWN_CONTENT_MAX_BYTES,
  getUtf8ByteLength,
  sliceByUtf8Bytes,
  truncateMarkdownContent,
  sendWxRobotMsg,
  sendWxRobotMarkdown,
  sendWxRobotImg,
  batchSendWxRobotBase64Img,
  batchSendWxRobotMsg,
  batchSendWxRobotMarkdown,
  getWxWorkAddChatLink,
  getWxWorkMessageLink,
  sendWxRobotBase64Img
} from 't-comm/lib/wecom-robot/index';

// 只支持 ESM 的项目
import {
  MARKDOWN_CONTENT_MAX_BYTES,
  getUtf8ByteLength,
  sliceByUtf8Bytes,
  truncateMarkdownContent,
  sendWxRobotMsg,
  sendWxRobotMarkdown,
  sendWxRobotImg,
  batchSendWxRobotBase64Img,
  batchSendWxRobotMsg,
  batchSendWxRobotMarkdown,
  getWxWorkAddChatLink,
  getWxWorkMessageLink,
  sendWxRobotBase64Img
} from 't-comm/es/wecom-robot/index';

MARKDOWN_CONTENT_MAX_BYTES

描述:企业微信机器人 markdown 消息内容最大长度(单位:字节,UTF-8 编码) 官方文档:https://developer.work.weixin.qq.com/document/path/91770 预留 256 字节安全余量,避免边界情况(如服务端对 JSON 包体的额外限制)

参数

getUtf8ByteLength()

描述:计算字符串的 UTF-8 字节长度

参数

getUtf8ByteLength~textEncoder

用于计算 UTF-8 字节长度的编码器(浏览器 / Node 16+ 均内置)

Kind: inner constant of getUtf8ByteLength

sliceByUtf8Bytes()

描述:按 UTF-8 字节长度安全裁剪字符串,确保不会把一个多字节字符截成半截

参数

truncateMarkdownContent(content)

描述:当 markdown 内容超出最大字节长度时进行裁剪,并在结尾追加说明

参数

参数名描述
content原始 markdown 内容

返回: 裁剪后的 markdown 内容

sendWxRobotMsg(config)

描述:给机器人发送普通消息

参数

参数名类型描述
configObject配置内容
config.webhookUrlstring钩子链接
config.chatIdstring会话id
config.aliasstring别名
config.contentstring内容

返回: Promise.<object>

Promise

示例

typescript
sendWxRobotMsg({
  webhookUrl: 'xxx',
  chatId: 'xxx',
  content: 'xxx',
  alias: 'xxx',
}).then(() => {

})

sendWxRobotMarkdown(config)

描述:给机器人发送Markdown消息

参数

参数名类型描述
configObject配置内容
config.webhookUrlstring钩子链接
config.chatIdstring会话id
config.contentstring内容
config.attachmentsArray<object>附加内容

返回: Promise.<object>

请求Promise

示例

typescript
sendWxRobotMarkdown({
  webhookUrl: 'xxx',
  chatId: 'xxx',
  content: 'xxx',
  attachments: []
}).then(() => {

})

sendWxRobotImg(config)

描述:给机器人发送图片

参数

参数名类型描述
configObject配置参数
config.webhookUrlstring钩子链接
config.chatIdstring会话id
config.contentstring内容
config.md5Valstringmd5内容

返回: Promise.<object>

请求Promise

示例

typescript
sendWxRobotImg({
  webhookUrl: 'xxx',
  chatId: 'xxx',
  content: 'xxx',
  md5Val: 'xxx'
}).then(() => {

})

batchSendWxRobotBase64Img(config)

描述:批量发送企业微信机器人base64图片

  • chatId 支持字符串或字符串数组,传 'ALL'['ALL'] 会发送给所有人

参数

参数名类型描述
configobject配置信息
config.imgstringbase64图片
config.chatIdstring | Array<string>会话Id,支持单个字符串、字符串数组、'ALL'['ALL'](发送给所有人)
config.webhookUrlstringwebhook地址

返回: Promise.<object>

请求Promise

示例

typescript
// 发送给单个会话
batchSendWxRobotBase64Img({
  img: 'xxx',
  chatId: 'xxx',
  webhookUrl: 'xxx',
});

// 发送给多个会话
batchSendWxRobotBase64Img({
  img: 'xxx',
  chatId: ['chatId1', 'chatId2'],
  webhookUrl: 'xxx',
});

// 发送给所有人
batchSendWxRobotBase64Img({
  img: 'xxx',
  chatId: 'ALL', // 或 ['ALL']
  webhookUrl: 'xxx',
});

batchSendWxRobotMsg(config)

描述:批量发送企业微信机器人文本消息

  • chatId 支持字符串或字符串数组,传 'ALL'['ALL'] 会发送给所有人

参数

参数名类型描述
configobject配置信息
config.contentstring消息内容
config.aliasstring | Array<string>被@的用户别名,支持单个字符串或字符串数组
config.chatIdstring | Array<string>会话Id,支持单个字符串、字符串数组、'ALL'['ALL'](发送给所有人)
config.webhookUrlstringwebhook地址

返回: Promise.<object>

请求Promise

示例

typescript
// 发送给单个会话
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',
});

batchSendWxRobotMarkdown(config)

描述:批量发送企业微信机器人Markdown消息(最常用)

  • chatId 支持字符串或字符串数组,传 'ALL'['ALL'] 会发送给所有人
  • 支持 Markdown V2 格式,通过 isV2 参数控制

参数

参数名类型默认值描述
configobject配置信息
config.contentstringMarkdown消息内容
[config.attachments]Array<object>附加内容
config.chatIdstring | Array<string>会话Id,支持单个字符串、字符串数组、'ALL'['ALL'](发送给所有人)
config.webhookUrlstringwebhook地址
[config.isV2]booleanfalse是否使用 Markdown V2 格式

返回: Promise.<object>

请求Promise

示例

typescript
// 发送给单个会话
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,
});

See: 企业微信自助入群工具https://nops.woa.com/pigeon/v1/tools/add_chat?chatId=群会话ID
参数

示例

ts
getWxWorkAddChatLink('xxx-chat-id');
// 'https://nops.woa.com/pigeon/v1/tools/add_chat?chatId=xxx-chat-id'

参数

示例

ts
getWxWorkMessageLink('novlan1')
// => https://github.com/novlan1

sendWxRobotBase64Img(config)

描述:发送企业微信机器人base64图片,其实就是先保存到本地,然后生成md5,最后发送

参数

参数名类型描述
configobject配置信息
config.imgstringbase64图片
config.chatIdstring会话Id
config.webhookUrlstringwebhook地址

返回: Promise.<object>

请求Promise

示例

typescript
sendWxRobotBase64Img({
  img: 'xxx',
  chatId: 'xxx',
  webhookUrl: 'xxx',
}).then(() => {

})