Git Product home page Git Product logo

telegram-focuszaxid-bot's Introduction

Superb template for building Telegram Bots

It's based on a couple of pretty cool technologies:

src folder has the next structure:

├───index.ts
├───config.ts
├───texts.json
├───controllers/
├───handlers/
├───helpers/
├───init/
├───middlewares/
├───models/
└───scenes/

So, let's learn how to build bots with TS-Bot-Template!

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.

Let's explore our folders

  • 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.

How to create custom handlers, scenes, middlewares etc?

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 {
            // ...
        }
    }
}

You have working bot from the box!

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.

Happy coding!

P.S Comments and logs are in Russian, but you can rewrite it ;)

telegram-focuszaxid-bot's People

Contributors

anna-y-marindrew avatar forcousteau avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.