npm.io
0.2.0 • Published 1h ago

@bitbasti/chat-adapter-webex

Licence
MIT
Version
0.2.0
Deps
0
Size
205 kB
Vulns
0
Weekly
0
Stars
1

@bitbasti/chat-adapter-webex

npm version npm downloads CI

Community Webex adapter for Chat SDK.

Installation

npm install chat @bitbasti/chat-adapter-webex

Usage

import { Chat } from "chat";
import { createWebexAdapter } from "@bitbasti/chat-adapter-webex";

const bot = new Chat({
  userName: "mybot",
  adapters: {
    webex: createWebexAdapter({
      botToken: process.env.WEBEX_BOT_TOKEN!,
      webhookSecret: process.env.WEBEX_WEBHOOK_SECRET,
    }),
  },
});

bot.onNewMention(async (thread, message) => {
  await thread.post(`Hello from Webex! You said: ${message.text}`);
});

Environment variables

Variable Required Description
WEBEX_BOT_TOKEN Yes Bot access token from the Webex Developer Portal
WEBEX_WEBHOOK_SECRET Recommended Shared secret for webhook signature verification
WEBEX_BASE_URL No Override the Webex API base URL (default: https://webexapis.com/v1)
WEBEX_BOT_USERNAME No Override the bot display name

Configuration

Option Type Default Description
botToken string WEBEX_BOT_TOKEN Bot access token
webhookSecret string WEBEX_WEBHOOK_SECRET Shared secret for verifying webhook signatures
baseUrl string "https://webexapis.com/v1" Webex API base URL
userName string "webex-bot" Bot display name (overridden by the bot's Webex profile on initialization)
botUserId string Auto-detected via /people/me Webex person ID of the bot (skips the /people/me call if provided)
logger Logger ConsoleLogger Custom logger instance

Platform setup

  1. Go to the Webex Developer Portal and sign in.
  2. Navigate to My Webex Apps → Create a New App → Create a Bot.
  3. Fill in the bot name, username, and icon, then click Add Bot.
  4. Copy the Bot Access Token — this is your WEBEX_BOT_TOKEN. Store it securely; it is only shown once.
  5. Create a webhook pointing to your server:
    • Target URL: https://your-domain.com/api/webhooks/webex
    • Resource: messages | Event: created
    • Resource: attachmentActions | Event: created
    • Secret: a random string — this is your WEBEX_WEBHOOK_SECRET
  6. Add the bot to a Webex space and mention it to verify the integration.

See the Webex Webhooks Guide for details on webhook registration.

Features

  • Mentions and DMs
  • Rich text (bold, italic, code, links) via Markdown
  • Adaptive Cards (buttons, selects, radio selects, fields, sections)
  • Modals (form cards with submit/close actions)
  • File uploads (single file per message)
  • Thread support (parentId-based threading)
  • Webhook signature verification (HMAC-SHA1)
Errors

Webex API and validation failures throw the adapter's error classes, exported from this package: ValidationError, AuthenticationError, PermissionError, ResourceNotFoundError, AdapterRateLimitError (with retryAfter) and NetworkError. They extend AdapterError and carry the same name and code values as the classes in @chat-adapter/shared. Branch on error.code (for example "RATE_LIMITED") if you mix adapters. Unsupported features (addReaction, removeReaction) throw chat's NotImplementedError instead.

Limitations
  • Reactions are not supported by Webex bot tokens — addReaction / removeReaction throw NotImplementedError
  • Typing indicators are not available in the Webex Messaging API
  • Only one file upload per message is supported
  • Cards and file uploads cannot be combined in the same message
  • Messages with files can't be edited, and Webex allows at most 10 edits per message. When Webex rejects an edit with a 400, editMessage throws ValidationError with Webex's reason. Text and card messages can be edited (including text ⇄ card), but Webex clients don't send button or form submits from a card that was edited (see #9), so post interactive cards as new messages
  • Modal { action: "update" } responses post the updated form as a new card in the same thread (same viewId) and then delete the old one, so the form stays submittable

Development

npm ci
npm run ci              # lint, typecheck, tests with coverage, build, publint
npm run test:unit       # fast unit tests in src/
npm run test:integration
scripts/consumer-compat.sh minimum   # packed package + chat@<peer minimum>, npm resolves the rest
scripts/consumer-compat.sh latest    # same against the newest chat on npm

Integration tests in test/integration/ run the real Chat runtime and adapter against a stateful in-memory fake of the Webex REST API (fake-webex-api.ts), installed as the global fetch. Webhooks are signed exactly like Webex signs them, so the tests cover signature checks, message hydration, threading, DMs, cards, modals, file uploads, pagination and error mapping without network access.

The fake is kept honest by recordings of the real Webex API in test/integration/fixtures/webex/: webex-recorded.test.ts replays every recorded request against the fake (same status and error message as real Webex) and feeds the recorded webhooks through the adapter. The recordings hold no tokens or signatures, and IDs and emails are replaced by placeholders. To record them again with a test bot and a test space:

WEBEX_BOT_TOKEN=… WEBEX_ROOM_ID=… node scripts/record-webex-fixtures.mjs

Add --webhooks <public URL> (for example an ngrok tunnel to --port, default 8787) to also record the interactive scenarios, which ask a person in the space to act in a Webex client: mention the bot (webhook-mention), submit a card (webhook-card-submit), then press Send on the edited card and on its replacement (edited-card-submit).

consumer-compat.sh installs the packed tarball into an empty project like a user would and runs the same integration suite against the built package. The Compat workflow runs it weekly, on dependency changes and on demand, and opens an issue when a scheduled run fails, so new chat releases are noticed early without making unrelated PRs fail.

End-to-end tests against real Webex

npm run test:e2e checks the released behaviour end to end before a release. Nothing is mocked: it builds the package, runs the built dist/ inside a real Chat, registers real webhooks that reach a local server through a public tunnel, and plays the human side with a real Webex user.

cp .env.example .env    # fill in WEBEX_BOT_TOKEN and WEBEX_USER_TOKEN
npm run test:e2e

You need:

  • a test bot token (WEBEX_BOT_TOKEN);
  • a token of a real Webex user who is not the bot (WEBEX_USER_TOKEN); a personal access token from developer.webex.com works for 12 hours;
  • a public URL: either ngrok installed and authenticated (the suite starts it), or WEBEX_E2E_PUBLIC_URL pointing at WEBEX_E2E_PORT on this machine.

The suite creates a group space with the bot and the user (or uses WEBEX_E2E_ROOM_ID), registers messages and attachmentActions webhooks with a random secret, and removes the webhooks afterwards. A space it created is deleted. An existing WEBEX_E2E_ROOM_ID space is never deleted; instead the suite deletes the messages the bot or the user posted there (or in their DM) since the run started, so use a space nobody else posts in as either account. It looks at the newest 200 messages of the space and one page of the DM, so a very long run can leave older messages behind. WEBEX_E2E_KEEP=1 keeps the messages (and a created space) for inspection.

File Covers
messaging.e2e.ts identity discovery, mentions, thread replies, subscriptions, own-message filtering, markdown / emoji / AST / card posts, edits (text card, thread replies, the 10-edit limit), deletes, streaming, typing, reactions
direct-messages.e2e.ts DMs to the bot, replies, openDM, editing and deleting in a DM, DM history
cards-and-modals.e2e.ts button and select actions, modal open / submit / validation errors / update / cancel
files.e2e.ts bot uploads, user uploads fetched by the bot, edit and multi-file rejections
history.e2e.ts fetchMessage, thread and channel pages in both directions, listThreads, fetchThread, fetchChannelInfo
errors-security.e2e.ts webhook signature checks on the public endpoint, auth and not-found errors
full-journey.e2e.ts one conversation end to end: formatted mention, subscribe, card, button, modal (errors, submit), streamed reply, subscribed follow-up, edit, history, unsubscribe, DM hand-off with a file, delete

In group spaces, a bot receives only the messages that mention it and can list only those, so every message the user writes there mentions the bot.

License

MIT

Keywords