npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

watsonworkspace-bot

v2.0.7

Published

An unofficial IBM Watson Workspace bot kit

Downloads

24

Readme

IBM Watson Workspace Bot Framework

This project is a framework for chatbot development on Watson Workspace. It is written in Javascript and Node.js.

Developers can contribute chatbot behavior by simply listening to and respond to specific Workspace events.

bot.on('message-focus:ActionRequest:Schedule', (body, annotation) => {
  logger.info(`Checking calendars based on scheduling event phrase '${annotation.phrase}'`)
})

Chatbot setup and event listening (webhooks) are handled by the bot framework. Developers choose which events and at what level to listen. In the code above, a developer could listen for the message-focus or message-focus:ActionRequest or message-focus:ActionRequest:Schedule event. For more information on the available events, see the Annotations, Focus, and Action Fulfillment documentation.

The following are a few combinations.

bot.on('message-created', (message) => ...
bot.on('message-annotation-added', (message, annotation) => ...
bot.on('message-focus', (message, annotation) => ...
bot.on('message-focus:ActionRequest', (message, annotation) => ...
bot.on('message-focus:ActionRequest:Schedule', (message, annotation) => ...
bot.on('message-focus:Question', (message, annotation) => ...
bot.on('message-focus:Commitment', (message, annotation) => ...
bot.on('actionSelected', (message, annotation) => ...
bot.on('actionSelected:someActionId', (message, annotation) => ...

Slash commands are also handled by the event emitter. The params argument is an array of parameters sent to the slash command by the user.

bot.on(`actionSelected:/mycommand`, (message, annotation, params) => ...

To build your bot, create a separate project. Then add the necessary require statements and begin listening to events to add your own behavior.

// creates a bot server with a single bot
const botFramework = require('watsonworkspace-bot')
botFramework.level('debug')
botFramework.startServer()

const bot = botFramework.create() // bot settings defined by process.env
bot.authenticate()

bot.on('message-annotation-added', (message, annotation) => {
  // do something awesome using watsonworkspace-sdk
})

Bot Paths

The Bot Framework runs on Express. By creating a bot, several paths will be mounted on Express to handle webhooks and OAuth.

The bot's root path is /<appId> where appId corresponds to your application's ID you received upon registration with Watson Work Services. For example, https://myapp.mybluemix.net/1023c56a-6751-4f70-8331-ad1cfc5ee800. The path that is used for webhooks in the Listen to Events page on Watson Work Services is https://myapp.mybluemix.net`/1023c56a-6751-4f70-8331-ad1cfc5ee800/webhook.

Two mounts are provided for OAuth: /<appId>/oauth and /<appId>/callback. These respectively handle triggering the OAuth flow and the resulting callback from Watson Work Services. To utilize OAuth, you must update the Run as a User page from your app on Watson Work Services page. An example OAuth2 redirect URI is https://myapp.mybluemix.net/1023c56a-6751-4f70-8331-ad1cfc5ee800/callback. To trigger the OAuth flow, redirect the user's browser to https://myapp.mybluemix.net/1023c56a-6751-4f70-8331-ad1cfc5ee800/oauth.

Acting on Behalf of a User

After the OAuth flow, the bot will be presented with an access token for the user. This token will be stored in an in-memory registry for the bot.

To use the token and act on behalf of the user, a bot can utilize the asUser function with standard SDK functions.

bot.asUser('<userId>').addMember(spaceId, memberId)

Once the framework authenticates the user, it will need to redirect the browser back to your application. To do this, create an environment variable called OAUTH_SUCCESS_PAGE, with a value of a valid URI to your application. For example:

OAUTH_SUCCESS_PAGE=/oauthSuccess

For testing locally, create an environment variable called OAUTH_REDIRECT_URI which points to the hostname of your application. For example, if you are using ngrok:

OAUTH_REDIRECT_URI=bd205ce9.ngrok.io

HTTPS

To utilize SSL during local development, you can start the Bot Framework using HTTPS.

const fs = require('fs')

botFramework.startServer({
  key: fs.readFileSync('key.pem'),
  cert: fs.readFileSync('cert.pem')
})

The key and cert files are created using OpenSSL for example.

openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 365 -out cert.pem

On a production server such as Bluemix (IBM Cloud), you can simply use the botFramework.startServer() function. HTTPS will be handled by the web layer of Bluemix.

Note It is possible to not start the framework with HTTPS and still use SSL-related features like OAuth. To do that, see a project like watsonworkspace-starter. It uses ngrok to provide a public, hosted SSL endpoint, which is required when registering your app in Watson Work Services. The ngrok tunnel then communicates locally to the bot framework running over standard HTTP.

Local Development

nodemon

Nodemon is used for development. As you make changes to Javascript code, nodemon will automatically reload the bot with the latest changes. The ngrok tunnel is loaded separately. You do not need to restart the tunnel. Simply make changes to your source code and allow nodemon to reload the chatbot automatically.

dotenv

.Env is used to store environment variables used by the bot: application IDs, secrets, etc. When doing local development, create a .env file in your project's folder with the following:

NODE_ENV=development
APP_ID=<your appId>
APP_SECRET=<your appSecret>
WEBHOOK_SECRET=<your webhookSecret>
BOT_NAME=<your botName>
PORT=<non-conflicting port>

Later when using Bluemix or similar PaaS solutions, you can edit the runtime variables to create the same property-value pairs.

ngrok

Watson Workspace uses webhooks as an event-driven means to exchange information with your chatbot. This requires your chatbot to be listening on a public server. Rather than writing code and deploying to a public server during development, this starter uses ngrok automatically.

Simply execute the npm run-script dev command. This will programmatically create a connection to a public domain using ngrok. A message will appear that indicates the URL you should use in your webhook.

Use 'https://cdf9d82f.ngrok.io/a7cfbdac-cdab-3d6f-ae13-0654b6b8e880' as your webhook URL in Watson Workspace

winston

Winston is the preferred logger.

Production Deployment

When moving your chatbot into production, you will need to edit your Webhook URL.

  1. Re-visit the Listen to events page.
  2. Select the more icon (three vertical dots) and then Edit.
  3. Update your Webhook URL to the productions server.