Webex Node Bot Framework
The webex-node-bot-framework is a Node.js framework designed to simplify the development of bots for Cisco Webex messaging. Currently stable at version 2.5.1, it provides an abstraction layer over the complex Webex For Developers APIs, streamlining tasks such as event registration and REST API calls. Unlike its inspiration, node-flint, this framework is built upon the actively supported webex-jssdk, ensuring compatibility with new Webex features. Developers migrating from node-flint should note that it is not backward compatible, requiring changes. The framework primarily focuses on enabling developers to implement bot interaction logic through 'handlers' for various message and membership events. It features recent enhancements in version 2.5.0, introducing `trigger.command` and `trigger.prompt` for more precise message parsing, and earlier updates in Version 2 for bot access restrictions. The project operates with community support, encouraging contributions and providing detailed migration guides and quickstart blogs for new users.
Common errors
-
WEBEX_BOT_TOKEN environment variable is not set.
cause The bot's access token is missing, which is essential for authentication with Webex APIs.fixSet the `WEBEX_BOT_TOKEN` environment variable with a valid bot access token obtained from the Webex for Developers portal. -
Error: Webhook registration failed: The ngrok tunnel is not publicly accessible.
cause The configured `webhookUrl` (e.g., an ngrok URL) is either invalid, expired, or the ngrok process is not running, preventing Webex from registering a valid webhook endpoint.fixEnsure `ngrok` is running and correctly tunneling to your bot's configured port, and that the `WEBEX_WEBHOOK_URL` environment variable is updated with the current ngrok public URL. Restart your bot application after updating. -
BadRequest: Currently moving room under another team is not supported in Developer API.
cause Attempting to perform an API operation, such as `bot.roomRename`, that is not supported by the current version of the Webex Developer API or the underlying `webex-jssdk`.fixConsult the Webex for Developers API documentation for the specific endpoint (`/v1/rooms`) to ensure the operation is supported and to understand any current limitations. If the operation is critical, consider alternative approaches or check for updates to the Webex SDK or framework.
Warnings
- breaking Migration from the `node-flint` framework is not backward compatible due to a rewrite based on `webex-jssdk` and changes in data handling and API call minimization strategies.
- breaking Version 2 introduced new configuration options (`guideEmails`, `restrictedToEmailDomains`) for restricting bot access. Existing configurations might need updates to align with these new parameters.
- gotcha For webhook-based bots, your `webhookUrl` must be publicly accessible. During local development, this often requires tools like `ngrok` or similar tunneling services. Failure to do so will prevent Webex from sending events to your bot.
- gotcha In group spaces, the bot must be at-mentioned (`@YourBotName`) for `framework.hears()` handlers to be triggered. Messages not explicitly mentioning the bot are generally ignored unless configured otherwise.
Install
-
npm install webex-node-bot-framework -
yarn add webex-node-bot-framework -
pnpm add webex-node-bot-framework
Imports
- Framework
const Framework = require('webex-node-bot-framework');import Framework from 'webex-node-bot-framework';
- Bot
import { Bot } from 'webex-node-bot-framework'; - Trigger
import { Trigger } from 'webex-node-bot-framework';
Quickstart
import Framework from 'webex-node-bot-framework';
import express from 'express';
// Ensure you have WEBEX_BOT_TOKEN and WEBEX_WEBHOOK_URL set in your environment
const WEBEX_BOT_TOKEN = process.env.WEBEX_BOT_TOKEN ?? '';
const WEBEX_WEBHOOK_URL = process.env.WEBEX_WEBHOOK_URL ?? ''; // e.g., ngrok URL or public domain
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 8080;
if (!WEBEX_BOT_TOKEN) {
console.error('WEBEX_BOT_TOKEN environment variable is not set.');
process.exit(1);
}
const config = {
token: WEBEX_BOT_TOKEN,
webhookUrl: WEBEX_WEBHOOK_URL,
port: PORT,
// Optional: Set to true if running behind a proxy like ngrok, or if using websockets
// if webhookUrl is empty, it defaults to websocket mode.
// This example assumes webhookUrl is provided and requires express
};
const app = express();
const framework = new Framework(config);
// Initialize framework and bind to Express app for webhooks
framework.listen(app, () => {
console.log(`Framework listening on port ${PORT}`);
});
// Log when the framework is initialized and ready
framework.on('initialized', () => {
console.log('Framework initialized successfully!');
});
// Log when a new bot instance is spawned in a room
framework.on('spawn', (bot, id, addedBy) => {
if (addedBy) {
bot.say('Hello! I am a simple echo bot. Mention me with "echo [your message]"');
} else {
console.log(`Bot already in space: ${bot.room.title}`);
}
});
// Example: Respond to 'echo' command
framework.hears('echo', (bot, trigger) => {
const messageToEcho = trigger.prompt || 'Nothing to echo!';
bot.say('markdown', `_You said_: ${messageToEcho}`);
}, '**echo [message]** - I will echo back your message.');
// Graceful shutdown
process.on('SIGINT', () => {
console.log('Stopping bot framework...');
framework.stop().then(() => {
console.log('Bot framework stopped.');
process.exit(0);
});
});