- Typescript ❤
- Telegraf📡
- MongoDB 🔮
- Mongoose 🐿️
src folder has the next structure:
├───index.ts
├───config.json
├───texts.json
├───controllers/
├───handlers/
├───helpers/
├───init/
├───middlewares/
├───models/
└───scenes/Firstly, you should insert your connection data into config.json
There are dev and prod objects. Using them depends on your NODE_ENV
{
"dev": {
"token": "<TOKEN>",
"dbUrl": "mongodb://127.0.0.1:27017/tsbot",
"port": 80
},
"prod": {
"token": "<TOKEN>",
"dbUrl": "mongodb://127.0.0.1:27017/tsbot",
"port": 8080
}
}At index.ts we configure our bot and initialize Telegraf middlewares, scenes, message handlers and DB connection.
import Bot from './init/bot'
import DB from './init/db'
import Handlers from './init/handlers'
import Middlewares from './init/middlewares'
import Scenes from './init/scenes'
const bot = Bot.configure() // configuring bot
Middlewares.init(bot) // initializing middlewares
Scenes.init(bot) // initializing scenes
Handlers.init(bot) // initializing handlers
DB.connect() // connecting to DBYou can use texts.json to put here texts for your messages.
initfolder keeps modules, that help us init everything we need;middlewaresfolder keeps our custom middlewares;controllersfolder keeps routes files. They represents a class of Message with their own keyboard;handlersfolder keeps message handlers;helpersfolder keeps additional functions you may need during development. You can expand existing files and create new;scenesfolder keeps scenes (dialog scripts);modelsfolder keeps models and schemas for Mongoose.
You should create new file as in example and register it in the relevant file in init folder. Import it and call init function with bot instance as argument.
import * as api from 'telegraf'
import YourHandler from '../handlers/yourHandler'
export default class Handlers {
public static init(bot: api.Telegraf<api.ContextMessageUpdate>): void {
try {
// ...
YourHandler.init(bot)
}
catch {
// ...
}
}
}What about scenes, import it in init/scenes.ts and call stage.register with your scene as argument.
import * as api from 'telegraf'
import YourScene from '../scenes/yourScene'
export default class Scenes {
public static init(bot: api.Telegraf<api.ContextMessageUpdate>): void {
try {
// ...
stage.register(YourScene)
// ...
}
catch {
// ...
}
}
}Put your connection data and use a bot. You have an admin panel inside the bot from the box.
Use /admin command to access it.
Important! Admin must have isAdmin: true in his DB document.
P.S Comments and logs are in Russian, but you can rewrite it ;)