You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

watson-assistant-for-wechat

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

watson-assistant-for-wechat

Helping developers building a chatbot for WeChat powered by Watson Assistant

1.0.2
latest
Source
npmnpm
Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

Watson Assistant for WeChat

codecov

Watson Assistant integration in WeChat for ExpressJS.

Usage

Install

yarn install watson-assistant-for-wechat

OR

npm i watson-assistant-for-wechat

Example

Detail example is avaliable at app.js.

const {wechatWatsonAssistant} = require('watson-assistant-for-wechat');

// Initialize the applicatoin, and mount the endpoint at `/watson`
wechatWatsonAssistant(app, config, '/watson');

app.post('/watson', async (req, res) => {
	const {user, message} = req;
	const {chat} = req.app.locals;
  const storage = req.sessionStore;

  // Retrive sessionId of current WeChat user
  const sessionId = await getSessionId();

  // Post incoming text to Watson Assistant
  const {result: assistantResponse} = await chat(
    {message_type: 'text', text: message.Content},
    sessionId,
    setSessionId
  );

  // Generate response to WeChat server
  const response = reply.text(
    message.ToUserName,
    message.FromUserName,
    assistantResponse.output.generic[0].text
  );
  res.set('Content-Type', 'text/xml');
  res.send(response);
}

Configuration

Below is a list of avaliable configuration.

// Initialize the applicatoin, and mount the endpoint at `/watson`
wechatWatsonAssistant(
  app,
  {
    apiKey: process.env.IBM_CLOUD_IAM_KEY,
    assistantId: process.env.IBM_WATSON_ASSISTANT_ID,
    wechatToken: process.env.WECHAT_TOKEN,
    version: "2020-02-05",
    url: "https://api.us-south.assistant.watson.cloud.ibm.com",
    chatRequestProperty: "locals.chat",
    messageRequestProperty: "message",
    userRequestProperty: "user"
  },
  "/watson"
);

Options

apiKey (required)

IBM Cloud IAM API Key, see IBM Cloud Documentation

assistantId (required)

The ID of the Watson Assistant instance.

Avaliable at Launch assistant -> Top Right (settings) -> API Details

wechatToken (required)

The token set at WeChat server endpoint configuration, see WeChat Documentation

version

Default value 2020-02-05

Watson Assistant API version, see IBM Cloud Documentation

url

Default value https://api.us-south.assistant.watson.cloud.ibm.com

Watson Assistant API endpoint, see IBM Cloud Documentation

chatRequestProperty

Default value locals.chat

The name of property under app where to attach the chat method

const { chat } = req.app.locals;
messageRequestProperty

Default value message

The name of property under req where to attach the incoming message from WeChat server

const { message } = req;
userRequestProperty

Default value user

The name of property under req where to attach the ID of incoming WeChat user

const { user } = req;

Development

Require

  • NodeJS: >=12
  • Yarn

Setup

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

Author

  • IBM Cognitive Class, IBM Digital Business Group

FAQs

Package last updated on 28 Feb 2020

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

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.