Bottender Documentation

repository·master·Indexed 26 days ago

https://github.com/yoctol/bottender

A declarative chatbot framework designed to simplify the complexity of building conversational user interfaces across multiple messaging channels, including LINE, Slack, Telegram, and Messenger.

Tokens
146K
Snippets
542
Records
806
Agent score
87%

What's inside Bottender

  1. Understand the Bottender Context object

    master
    The Bottender context object encapsulates essential methods and properties for developing chatbots across different platforms. It provides access to the session, client, event, and state, allowing for platform-agnostic development while still providing access to platform-specific features.
  2. Detect and handle Slack text message events

    master

    In a Bottender Slack application, you can identify if an incoming event is a text message by checking the context.event.isText boolean property. To access the actual string content of the message, use context.event.text.

    async function App(context) {
      if (context.event.isText) {
        // handling the text message event
        await context.sendText(`received the text message: ${context.event.text}`);
      }
    }
  3. Configure Slack Event Subscriptions (Webhooks)

    master

    To receive messages from Slack, you must configure the Event Subscriptions in the Slack Developer Console.

    1. Start your Bottender server:
      • Production: npm start
      • Development: npm run dev (this automatically starts ngrok and provides a webhook URL).
    2. Copy the slack webhook URL from your console (e.g., https://xxxx.ngrok.io/webhooks/slack).
    3. In the Slack Developer Console, go to Event Subscriptions.
    4. Toggle Enable Events to on.
    5. Paste your URL into the Request URL field. Wait for the Verified status.
    6. Under Subscribe to bot events, add the following common events:
      • message.im: Direct messages.
      • message.groups: Private channel messages.
      • message.channels: Public channel messages.
      • message.mpim: Multi-party direct message messages.
    7. Click Save Changes.
    8. Important: If prompted, reinstall your app to the workspace to apply the new permissions.
    # in production mode
    npm start
    
    # or in development mode
    npm run dev
  4. Get a LineClient instance

    master

    You can obtain a LineClient instance in two ways:

    1. Using getClient: Import getClient from bottender and pass 'line' as the argument.
    2. From the context object: If you are inside an action, the context.client property is automatically a LineClient instance if the platform is line.

    Use this instance to interact with the LINE Messaging API (e.g., sending text messages).

    const { getClient } = require('bottender');
    
    // Method 1: Using getClient
    const client = getClient('line');
    await client.pushText(USER_ID, 'Hello!');
    
    // Method 2: Using context in an action
    async function MyAction(context) {
      if (context.platform === 'line') {
        await context.client.pushText(USER_ID, 'Hello!');
      }
    }
  5. Run the Telegram Hello World example

    master

    To run the Telegram Hello World example, download the example directory, install dependencies, and configure your environment variables. You must provide a TELEGRAM_ACCESS_TOKEN in a .env file.

    1. Download and enter the directory:
    curl https://codeload.github.com/Yoctol/bottender/tar.gz/master | tar -xz --strip=2 bottender-master/examples/telegram-hello-world
    cd telegram-hello-world
    1. Install dependencies:
    npm install
    1. Configure .env with your TELEGRAM_ACCESS_TOKEN.
    2. Start the development server:
    npm run dev

    Running npm run dev starts a server at http://localhost:5000 and outputs a webhook URL (e.g., https://xxxxxxxx.ngrok.io/webhooks/telegram) to the terminal.

    curl https://codeload.github.com/Yoctol/bottender/tar.gz/master | tar -xz --strip=2 bottender-master/examples/telegram-hello-world
    cd telegram-hello-world
    npm install
    npm run dev
  6. Enable the Slack channel in Bottender

    master

    You can enable Slack in Bottender using a new project or an existing one.

    New Projects

    Use the create-bottender-app CLI and select the slack option during setup:

    npx create-bottender-app my-app

    Existing Projects

    Update your bottender.config.js to include the slack channel configuration. Ensure enabled is set to true.

    module.exports = {
      channels: {
        slack: {
          enabled: true,
          path: '/webhooks/slack',
          accessToken: process.env.SLACK_ACCESS_TOKEN,
          signingSecret: process.env.SLACK_SIGNING_SECRET,
          // verificationToken: process.env.SLACK_VERIFICATION_TOKEN, // deprecated, use signingSecret
        },
      },
    };
  7. Migrate an existing LINE SDK bot project to Bottender

    master

    If you are moving an existing project from the @line/bot-sdk to Bottender, follow these steps:

    1. Replace dependencies: Uninstall @line/bot-sdk and install bottender@next.
    2. Configure Bottender: Create or edit a bottender.config.js file to define your webhook path and credentials.
    3. Implement bot logic: Rewrite your event handling logic in index.js using the Bottender context object.
    4. Set environment variables: Create a .env file containing your LINE_ACCESS_TOKEN and LINE_CHANNEL_SECRET.
    5. Start the bot: Run the project using the Bottender CLI.
    # 1. Replace dependencies
    npm install bottender@next
    npm uninstall @line/bot-sdk
    
    # OR using yarn
    yarn add bottender@next
    yarn remove @line/bot-sdk
    
    # 5. Start the bot
    npx bottender start
    // 2. bottender.config.js
    module.exports = {
      enabled: true,
      path: '/webhooks/line',
      accessToken: process.env.LINE_ACCESS_TOKEN,
      channelSecret: process.env.LINE_CHANNEL_SECRET,
    };
    // 3. index.js
    module.exports = function App(context) {
      await context.sendText(context.event.text);
    };
    # 4. .env
    LINE_ACCESS_TOKEN=
    LINE_CHANNEL_SECRET=
  8. Handle LINE Payload Events

    master

    Payload events are triggered by postback buttons on template, imagemap, flex messages, rich menus, or quick replies. Identify them using context.event.isPayload and access the data via context.event.payload.

    async function App(context) {
      if (context.event.isPayload) {
        // handling the payload event
        await context.sendText(`received the payload: ${context.event.payload}`);
      }
    }
  9. Configure LINE channel in an existing Bottender app

    master

    To enable LINE in an existing project, update your bottender.config.js file. You must set channels.line.enabled to true.

    By default, the server listens for LINE webhooks on /webhooks/line, but you can customize this using the path property. It is recommended to use environment variables for accessToken and channelSecret to keep credentials secure.

    module.exports = {
      channels: {
        line: {
          enabled: true,
          path: '/webhooks/line',
          accessToken: process.env.LINE_ACCESS_TOKEN,
          channelSecret: process.env.LINE_CHANNEL_SECRET,
        },
      },
    };