Socket
Book a DemoInstallSign in
Socket

@dlghq/dialog-bot-sdk

Package Overview
Dependencies
Maintainers
33
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dlghq/dialog-bot-sdk

Dialog Bot SDK

6.0.2
latest
npmnpm
Version published
Weekly downloads
42
Maintainers
33
Weekly downloads
 
Created
Source

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 from '@dlghq/dialog-bot-sdk';
import { flatMap } from 'rxjs/operators';

dotenv.config();

async function run(token: string, endpoint: string) {
  const bot = new Bot({
    token,
    endpoints: [endpoint],
    loggerOptions: {
      name: 'example-bot',
      level: 'trace',
      prettyPrint: true,
    },
  });

  const self = await bot.getSelf();
  bot.logger.info(`I've started, post me something @${self.nick}`);

  bot.updateSubject.subscribe({
    next(update) {
      bot.logger.info(JSON.stringify({ update }, null, 2));
    },
  });

  const messagesHandle = bot.subscribeToMessages().pipe(
    flatMap(async (message) => {
      const author = await bot.forceGetUser(message.senderUserId);
      if (author.isBot) {
        await bot.sendText(message.peer, "Hi, I'm bot too!");
        return;
      }
    }),
  );

  await new Promise((resolve, reject) => {
    messagesHandle.subscribe({
      error: reject,
      complete: resolve,
    });
  });
}

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

const endpoint =
  process.env.BOT_ENDPOINT || 'https://grpc-test.transmit.im:9443';

run(token, endpoint).catch((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

FAQs

Package last updated on 21 Jan 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.