oira666_tg
v1.0.38
Published
tg framework for oira666
Readme
Node Telegram Bot Framework
A Telegram bot framework built on Telegraf with filesystem-based handler routing, persisted sessions, built-in middleware, and TypeScript typings.
It ships built-in features for:
- admin/start-message mailing flows
- optional human-to-human help sessions (
human_help_session/*)
Package output
This package is authored in TypeScript and published as compiled CommonJS from dist/.
- runtime entry:
dist/index.js - type declarations:
dist/index.d.ts
If you are working inside this repository, build before consuming the package locally:
npm install
npm run buildInstallation
npm install oira666_tg telegraftelegraf is a peer dependency and must be installed by the consuming bot project.
Quick start
const path = require('path');
const { Telegraf, Markup } = require('telegraf');
const {
apply_middlewares,
start_builtin_human_help_workers,
} = require('oira666_tg');
const db = require('./db');
const bot = new Telegraf(process.env.BOT_TOKEN);
await apply_middlewares(bot, {
db,
Markup,
handlers_path: path.resolve(__dirname, './dist/handlers'),
resources_path: path.resolve(__dirname, './resources'),
allowed_user_commands: ['start', 'help'],
mailing: {
enabled: true,
},
human_help: {
enabled: true,
support_phone: '8 800 551-62-60',
operators: {
roles: ['admin'],
},
session: {
working_hours: {
timezone: 'Europe/Moscow',
start_hour: 9,
end_hour: 19,
},
},
workers: {
enabled: true,
auto_start: false,
},
},
});
start_builtin_human_help_workers(bot);Built-in mailing feature
Before enabling built-in mailing, create the mailing tables (mailing_queue, mailing_autoschedule, mailing_autoschedule_queue). See docs/setup.md.
Config block
options.mailing supports these top-level sections:
enabledadmin.enabledschedule.enabledautoschedule.enabledstart_messages.enabledstart_messages.send_immediate_rulesstart_messages.enqueue_delayed_rulesworkers.enabledworkers.auto_startworkers.schedule_cronworkers.autoschedule_cronworkers.due_batch_sizeaudiencesartifacts.pathsend.pause_everysend.pause_mstimezone
Defaults are merged automatically. If mailing.artifacts.path is omitted and resources_path is set, artifacts default to:
<resources_path>/autoschedule/artifactsConsumer override caveat
Consumer handlers are resolved before framework built-ins. If your bot previously implemented its own mailing flow or admin stats handler, those local handlers will shadow the package handlers.
When migrating to the built-in mailing/stats features, archive/remove local admin/mailing and admin/stats handlers and old /start or cron wiring if you want only framework mailing behavior and no duplicate sends/UI.
/start semantics
The start-message pipeline runs before the resolved /start handler, but only when the resolved handler is:
type === 'command'name === 'start'
Important runtime behavior:
- it runs on each real
/startcommand, not just the first one - zero-delay autoschedule rules are sent immediately on
/start - delayed autoschedule rules are enqueued on
/start - redirects or text buttons that eventually lead to
startdo not trigger the pipeline - the built-in
starthandler is a no-op, so/startcan still trigger mailing even if the consumer does not provide its ownstarthandler
Workers
Built-in mailing workers auto-start inside apply_middlewares() when:
mailing.enabled === truemailing.workers.enabled === truemailing.workers.auto_start === true
You can also manage them manually:
const {
start_builtin_mailing_workers,
stop_builtin_mailing_workers,
} = require('oira666_tg');
start_builtin_mailing_workers(bot);
stop_builtin_mailing_workers(bot);The worker bootstrap helper is duplicate-safe for the same bot/config signature.
Built-in human-help feature
The framework can provide a reusable human-help flow under the built-in handler namespace:
human_help_session/get_human_helphuman_help_session/start_sessionhuman_help_session/first_questionhuman_help_session/request_operatorshuman_help_session/first_answerhuman_help_session/on_messagehuman_help_session/on_waiting_messagehuman_help_session/finish_sessionhuman_help_session/no_commandshuman_help_session/no_questionshuman_help_session/cleanup_stale_sessions
The feature is disabled by default. Consumers keep control of entrypoints like /help, /start help, and menu buttons by redirecting into that namespace.
Config block
options.human_help supports these sections:
enabledhandlers.enabledintercept_active_messagessupport_phoneoperators.rolesoperators.resolveoperators.format_display_namerequests.send_immediatelyrequests.resend_pending_via_workersrequests.waiting_notice_delay_msrequests.operator_button_textsession.stop_wordsession.inactivity_timeout_mssession.stale_waiting_timeout_minutessession.stale_active_timeout_minutessession.working_hours.timezonesession.working_hours.start_hoursession.working_hours.end_hourworkers.enabledworkers.auto_startworkers.request_operators_cronworkers.cleanup_cron
Defaults are merged automatically. Recommended opt-in stance:
human_help: {
enabled: true,
workers: {
enabled: true,
auto_start: false,
},
}Runtime behavior
When enabled, the framework inserts two human-help hooks into the middleware chain:
- a context binder that loads
ctx.human_help - a late session-state guard that:
- replaces previously resolved handlers with
human_help_session/on_messagefor active dialogs - replaces only unresolved/unhandled plain messages with
human_help_session/on_waiting_messagewhile a client is still waiting for an operator
- replaces previously resolved handlers with
This preserves consumer-first routing while still letting waiting/active support dialogs avoid the generic unhandled-message fallback.
Workers
Human-help workers are separate from mailing workers and are also duplicate-safe.
const {
start_builtin_human_help_workers,
stop_builtin_human_help_workers,
} = require('oira666_tg');
start_builtin_human_help_workers(bot);
stop_builtin_human_help_workers(bot);Recommended production stance:
- keep
auto_start: false - explicitly start workers in exactly one process
Migration note for existing bots
If a consumer bot already had app-local human-help code:
- keep local
/help,/start help, and menu-button entrypoints if desired - migrate your prefixed
human_session.operator_idcolumn toNULL DEFAULT NULLwhen practical; the framework now falls back to legacy0writes when a live schema still rejectsNULL, but nullable remains the intended schema - remove local
human_help_session/*handlers once parity is confirmed - remove local human-help middleware once
human_help.enabledis on - remove duplicate human-help cron jobs if framework workers are used
Handler path note for TypeScript consumers
Handler discovery is runtime JS-based. If your bot handlers are authored in TypeScript, compile them first and point handlers_path at the compiled JavaScript output directory.
Supported handler file shapes under handlers_path:
name.jsname/index.jsname/main.js
Exported TypeScript types
The package exports runtime helpers and public types, including:
BotContextFrameworkOptionsRuntimeConfigHandlerDescriptorDatabaseAdapterMailingFeatureOptionsHumanHelpFeatureOptionsHumanHelpOperatorResolver
Repository validation commands
npm run typecheck
npm test -- --runInBand
npm run lint
npm run build
npm pack --dry-run