Socket
Book a DemoInstallSign in
Socket

@re-ai/wxkf-store

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@re-ai/wxkf-store

微信小店客服API SDK

latest
npmnpm
Version
0.1.0
Version published
Maintainers
1
Created
Source

微信小店客服 SDK

这是一个用于对接微信小店客服API的Node.js SDK,使用TypeScript开发。

安装

npm install @re-ai/wxkf-store

使用方法

初始化客户端

import { WxStoreKfClient, MessageType } from '@re-ai/wxkf-store';

// 不使用Redis
const client = new WxStoreKfClient({
  appId: 'your_app_id',
  appSecret: 'your_app_secret'
});

// 使用Redis缓存访问令牌
const clientWithRedis = new WxStoreKfClient({
  appId: 'your_app_id',
  appSecret: 'your_app_secret',
  redis: {
    host: 'localhost',
    port: 6379,
    username: 'your_redis_username', // 可选
    password: 'your_redis_password', // 可选
 
  }
});

发送文本消息

async function sendTextMessage() {
  const message = {
    type: MessageType.TEXT,
    open_id: 'user_open_id',
    text: {
      content: '您好,这是一条测试消息'
    }
  };

  try {
    await client.sendMessage(message);
    console.log('消息发送成功');
  } catch (error) {
    console.error('消息发送失败:', error);
  }
}

发送图片消息

async function sendImageMessage() {
  const message = {
    type: MessageType.IMAGE,
    open_id: 'user_open_id',
    image: {
      cos_url: 'https://example.com/image.jpg'
    }
  };

  try {
    await client.sendMessage(message);
    console.log('图片消息发送成功');
  } catch (error) {
    console.error('图片消息发送失败:', error);
  }
}

发送商品分享卡片

async function sendProductShareMessage() {
  const message = {
    type: MessageType.PRODUCT_SHARE,
    open_id: 'user_open_id',
    product_share: {
      product_id: '10000147759678'
    }
  };

  try {
    await client.sendMessage(message);
    console.log('商品分享卡片发送成功');
  } catch (error) {
    console.error('商品分享卡片发送失败:', error);
  }
}

上传多媒体资源

import * as fs from 'fs';
import * as path from 'path';

async function uploadMedia() {
  const filePath = path.join(__dirname, 'test_image.jpg');
  const fileStream = fs.createReadStream(filePath);

  try {
    const cosUrl = await client.uploadMedia({
      open_id: 'user_open_id',
      msg_type: MessageType.IMAGE,
      file: fileStream
    });
    console.log(`多媒体资源上传成功,cos_url: ${cosUrl}`);
  } catch (error) {
    console.error('多媒体资源上传失败:', error);
  }
}

处理客服消息推送

import { ReAIWXStoreKFMessageNotifyHandle } from '@re-ai/wxkf-store';

const options = {
  token: 'your_token',
  encodingAesKey: 'your_encodingAesKey',
  appId: 'your_appId'
};

const messageCallback = (message) => {
  console.log('收到解析后的消息:', message);
  // 在这里添加你对解析后消息的处理逻辑
};

// 在HTTP服务器中处理微信推送
app.post('/wxkf/notify', async (req, res) => {
  const result = await ReAIWXStoreKFMessageNotifyHandle(options, {
    query: req.query,
    body: req.body
  }, messageCallback);
  
  if (result === 'success' || result === '') {
    res.send('success');
  } else {
    res.status(500).send('处理失败');
  }
});

API文档

WxStoreKfClient

客服API客户端类,用于发送消息和管理会话。

构造函数

constructor(options: WxStoreKfOptions)

参数:

  • options.appId: 微信小程序的AppID
  • options.appSecret: 微信小程序的AppSecret

方法

sendMessage

发送客服消息。

async sendMessage(message: BaseMessage): Promise<void>

支持的消息类型:

  • 文本消息 (TextMessage)
  • 图片消息 (ImageMessage)
  • 文件消息 (FileMessage)
  • 商品分享消息 (ProductShareMessage)
  • 订单分享消息 (OrderShareMessage)
uploadMedia

上传多媒体资源。

async uploadMedia(options: UploadMediaOptions): Promise<string>

Keywords

wechat

FAQs

Package last updated on 19 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts