Skip to main content

Analytics

Installation

PNPm

pnpm add @tlgr/analytics

yarn

yarn add @tlgr/analytics

npm

npm install @tlgr/analytics

Usage

Usage is simple as much as possible.

Example below creates command start and reply with text Hello world.

Example:

example.ts
import { Telegraf } from "telegraf";
import { Analytics } from "@tlgr/analytics";

const bot = new Telegraf("<API TOKEN>");

const analytics = new Analytics(bot, Plugin1, Plugin2, ...etc);

bot.start((ctx) => {
// send "hello world" when user promts "/start"
ctx.reply("Hello world");
});

// simple usage
// analytics.enable();
// bot.launch();

// advanced usage
analytics.enable(true); // starts bot. Better use this snippet since it fires additional event "bot:launch"

// some time after
//analytics.disable();
// bot.stop();

// or similar but in one line:
analytics.disable(true); // true = stop bot

Plugins

Currently only 3 plugins available:

  • stream
  • console
  • file
  • server

Stream

Plugin which accepts writable stream and write result to this stream.

Custom plugins

  1. import plugin:
// filename: custom-plugin.ts
import type { Plugin } from '@tlgr/analytics/plugins';

class MyPlugin implements Plugin {
name = 'custom plugin';

constructor(){
http.initSession();
}

listener(event: string, param: any) {
console.log(event, param);
if(event === 'my custom event') {
http.post('http://server.com', param)
}
}
end(){
http.closeSession();
}
}

  1. use custom plugin in your code
import { Telegraf } from 'telegraf';
import Analytics from './analytics';

import {MyPlugin} from './custom-plugin';

const TOKEN = '';
const bot = new Telegraf(TOKEN);

const analytics = new Analytics(bot, new MyPlugin());

bot.start(ctx => {
// sends hello world when user sends "/start" command
bot.telegram.sendMessage(ctx.chat.id, 'Hello world');
});

analytics.enable(true);