- Typescript ❤
- Telegraf📡
- MongoDB 🔮
- Mongoose 🐿️
src
folder has the next structure:
├───index.ts
├───config.ts
├───texts.json
├───controllers/
├───handlers/
├───helpers/
├───init/
├───middlewares/
├───models/
└───scenes/
Firstly, you should insert your connection data into config.ts
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 main = async () => {
await DB.connect() // connecting DB
const bot = await Bot.configure() // configuring bot
Middlewares.init(bot) // initializing middlewares
Scenes.init(bot) // initializing scenes
Handlers.init(bot) // initializing handlers
}
main()
You can use texts.json
to put here texts for your messages.
init
folder keeps modules, that help us init everything we need;middlewares
folder keeps our custom middlewares;controllers
folder keeps routes files. They represents a class of Message with their own keyboard;handlers
folder keeps message handlers;helpers
folder keeps additional functions you may need during development. You can expand existing files and create new;scenes
folder keeps scenes (dialog scripts);models
folder 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 ;)