Messaging API client for WeChat
npm i --save messaging-api-wechat
or
yarn add messaging-api-wechat
const { WechatClient } = require('messaging-api-wechat');
// get appId, appSecret from「微信公众平台-开发-基本配置」page
const client = WechatClient.connect(appId, appSecret);
All methods return a Promise.
Send API - Official Docs
发送文本消息
Param | Type | Description |
---|---|---|
userId | String |
user ID of the recipient. |
text | String |
Text of the message to be sent. |
Example:
client.sendText(USER_ID, 'Hello!');
发送图片消息
Example:
client.sendImage(USER_ID, 'MEDIA_ID');
发送语音消息
Example:
client.sendVoice(USER_ID, 'MEDIA_ID');
发送视频消息
Example:
client.sendVideo(USER_ID, {
media_id: 'MEDIA_ID',
thumb_media_id: 'THUMB_MEDIA_ID',
title: 'VIDEO_TITLE',
description: 'VIDEO_DESCRIPTION',
});
发送音乐消息
Example:
client.sendMusic(USER_ID, {
musicurl: 'MUSIC_URL',
hqmusicurl: 'HQ_MUSIC_URL',
thumb_media_id: 'THUMB_MEDIA_ID',
title: 'MUSIC_TITLE',
description: 'MUSIC_DESCRIPTION',
});
发送图文消息(点击跳转到外链)
Example:
client.sendNews(USER_ID, {
articles: [
{
title: 'Happy Day',
description: 'Is Really A Happy Day',
url: 'URL',
picurl: 'PIC_URL',
},
{
title: 'Happy Day',
description: 'Is Really A Happy Day',
url: 'URL',
picurl: 'PIC_URL',
},
],
});
发送图文消息(点击跳转到图文消息页面)
Example:
client.sendMPNews(USER_ID, 'MEDIA_ID');
发送卡券
Example:
client.sendWXCard(USER_ID, '123dsdajkasd231jhksad');
发送小程序卡片
Example:
client.sendMiniProgramPage(USER_ID, {
title: 'title',
appid: 'appid',
pagepath: 'pagepath',
thumb_media_id: 'thumb_media_id',
});
Media API - Official Docs
多媒体文件上传接口
Example:
const fs = require('fs');
const buffer = fs.readFileSync('test.jpg');
client.uploadMedia('image', buffer).then(media => {
console.log(media);
// {
// type: 'image',
// media_id: 'MEDIA_ID',
// created_at: 123456789
// }
});
下载多媒体文件接口
Example:
client.getMedia(MEDIA_ID).then(media => {
console.log(media);
// {
// video_url: "..."
// }
});
To avoid sending requests to real WeChat server, specify origin
option when constructing your client:
const { WechatClient } = require('messaging-api-wechat');
const client = WechatClient.connect({
appId: APP_ID,
appSecret: APP_SECRET,
origin: 'https://mydummytestserver.com',
});
Warning: Don't do this on production server.