1.0.0 • Published 6 years ago

wechat-watson-conversation v1.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

wechat-watson-conversation

Watson Conversation integration in wechat for ExpressJS.

Usage

Install

yarn add wechat-watson-conversation

OR

npm i wechat-watson-conversation

Example

Detail example is also avaliable here.

const { wechatWatconConversation } = requir ('wechat-watson-conversation');

const ENV = process.env;
const config = {
  wechat: {
    token: ENV.WECHAT_TOKEN,
    encodingAESKey: ENV.WECHAT_ENCODING_AES_KEY,
  },
  watsonConversation: {
    workspace_id: ENV.WATSON_WORKSPACE_ID,
    username: ENV.WATSON_USERNAME,
    password: ENV.WATSON_PASSWORD,
  },
};

wechatWatconConversation(app, config, '/path');

app.post('/path', async (req, res) => {
  const { message, chatContext } = req; // Retrieve incoming message, and previous chat context is exist.
  const { chat } = req.app.locals; // Watson conversation message method

  const { output: { text }, context } = await chat(message.Content, chatContext);
  const storage = req.sessionStore;
  storage.set(req.user, context);

  const response = reply.text(message.ToUserName, message.FromUserName, text[0]);
  res.set('Content-Type', 'text/xml');
  res.send(response);
})

Modules

The library also exposes each individual middleware if you prefer more flexbility.

Development

Require

Setup

  • cp .env.example .env
  • yarn install
  • yarn dev

Author

  • IBM Cognitive Class, IBM Digital Business Group