Skip to content

Latest commit

 

History

History
 
 

messaging-api-wechat

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

messaging-api-wechat

Messaging API client for WeChat

Table of Contents

Installation

npm i --save messaging-api-wechat

or

yarn add messaging-api-wechat

Usage

Initialize

const { WechatClient } = require('messaging-api-wechat');

// get appId, appSecret from「微信公众平台-开发-基本配置」page
const client = WechatClient.connect(appId, appSecret);

API Reference

All methods return a Promise.


Send API - Official Docs

sendText(userId, text)

发送文本消息

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!');

sendImage(userId, mediaId)

发送图片消息

Example:

client.sendImage(USER_ID, 'MEDIA_ID');

sendVoice(userId, mediaId)

发送语音消息

Example:

client.sendVoice(USER_ID, 'MEDIA_ID');

sendVideo(userId, video)

发送视频消息

Example:

client.sendVideo(USER_ID, {
  media_id: 'MEDIA_ID',
  thumb_media_id: 'THUMB_MEDIA_ID',
  title: 'VIDEO_TITLE',
  description: 'VIDEO_DESCRIPTION',
});

sendMusic(userId, music)

发送音乐消息

Example:

client.sendMusic(USER_ID, {
  musicurl: 'MUSIC_URL',
  hqmusicurl: 'HQ_MUSIC_URL',
  thumb_media_id: 'THUMB_MEDIA_ID',
  title: 'MUSIC_TITLE',
  description: 'MUSIC_DESCRIPTION',
});

sendNews(userId, news)

发送图文消息(点击跳转到外链)

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',
    },
  ],
});

sendMPNews(userId, mediaId)

发送图文消息(点击跳转到图文消息页面)

Example:

client.sendMPNews(USER_ID, 'MEDIA_ID');

sendWXCard(userId, cardId)

发送卡券

Example:

client.sendWXCard(USER_ID, '123dsdajkasd231jhksad');

sendMiniProgramPage(userId, miniProgramPage)

发送小程序卡片

Example:

client.sendMiniProgramPage(USER_ID, {
  title: 'title',
  appid: 'appid',
  pagepath: 'pagepath',
  thumb_media_id: 'thumb_media_id',
});

Media API - Official Docs

uploadMedia(type, media)

多媒体文件上传接口

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
  // }
});

getMedia(mediaiD)

下载多媒体文件接口

Example:

client.getMedia(MEDIA_ID).then(media => {
  console.log(media);
  // {
  //   video_url: "..."
  // }
});

Test

Point requests to your dummy server

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.