Webex Node Bot Framework

2.5.1 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

Sets up a basic Webex bot that listens for 'echo' commands and responds by echoing the user's input. This demonstrates framework initialization, event handling, and basic messaging with Express for webhooks.

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);
  });
});

view raw JSON →