Skip to content
JavaScript Bot SDK for Dialog messenger
Branch: master
Clone or download
Latest commit c4154d9 Jul 19, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci chore(ci): disable docs publishing Jun 17, 2019
example style: add prettier Jul 19, 2019
src
.babelrc feat: initial commit Oct 18, 2018
.editorconfig test: add initial tests Jun 9, 2019
.gitignore test: add initial tests Jun 9, 2019
LICENSE feat: initial commit Oct 18, 2018
README.md style: add prettier Jul 19, 2019
jest.config.js feat: expose parameters api Jul 17, 2019
package-lock.json style: add prettier Jul 19, 2019
package.json style: add prettier Jul 19, 2019
tsconfig.json style: add prettier Jul 19, 2019

README.md

Dialog JS Bot SDK

Bot SDK for Dialog messenger.

CircleCI codecov

Installation

npm install @dlghq/dialog-bot-sdk

Usage

import dotenv from 'dotenv';
import Bot, { MessageAttachment } from '@dlghq/dialog-bot-sdk';

dotenv.config();

const token = process.env.BOT_TOKEN;
if (typeof token !== 'string') {
  throw new Error('BOT_TOKEN env variable not configured');
}

const bot = new Bot({
  token,
  endpoints: ['https://epm.dlg.im'],
});

bot.updateSubject.subscribe({
  next(update) {
    console.log('update', update);
  },
});

bot
  .onMessage(async (message) => {
    if (message.content.type === 'text') {
      // echo message with reply
      const mid = await bot.sendText(
        message.peer,
        message.content.text,
        MessageAttachment.reply(message.id),
      );

      // reply to self sent message with document
      await bot.sendDocument(
        message.peer,
        __filename,
        MessageAttachment.reply(mid),
      );
    }
  })
  .subscribe({
    error(error) {
      console.error(error);
      process.exit(1);
    },
  });

Mutual authentication

In case your server requires mutual authentication, you can pass credentials like this.

import fs from 'fs';
import Bot from '@dlghq/dialog-bot-sdk';

const bot = new Bot({
  token,
  endpoints: ['https://epm.dlg.im'],
  ssl: {
    rootCerts: fs.readFileSync(path.join(__dirname, 'dialog-root-cert.crt')),
    privateKey: fs.readFileSync(path.join(__dirname, 'client.key')),
    certChain: fs.readFileSync(path.join(__dirname, 'client.crt')),
  },
});

License

Apache 2.0

You can’t perform that action at this time.