Code Monkey home page Code Monkey logo

aiogram_telegram_web_bot's Introduction

Powerful aiogram template

Telegram Bot API template, with aiogram, peewee and docker

Navigation


Getting started

Simple use template

$ git clone https://github.com/netbriler/aiogram-peewee-template <your project name>
$ cd <your project name>
$ pip install -r requirements.txt

# initialize database
$ pw_migrate migrate --database $(python _get_database_url.py) --directory ./migrations
# or if you have make you can simply type 
$ make db_upgrade

# run pooling
$ python app.py
# or webhook
$ python webhook.py

Configure environment variables

Copy file .env.dist and rename it to .env

$ cp .env.dist .env

Than configure variables

$ vim .env
# or 
$ nano .env

Bot settings:

ADMINS - administrators ids divided by ,

# example
ADMINS=12345678,12345677,12345676
# or one admin
ADMINS=12345678

BOT_TOKEN - bot token from @TopDev

# example
BOT_TOKEN=123452345243:Asdfasdfasf

RATE_LIMIT - throttling rate limit (anti-spam)

# example
RATE_LIMIT=0.5 # seconds

TELEGRAM_TEST_SERVER - enable telegram test server

# example
TELEGRAM_TEST_SERVER=true # enabled
TELEGRAM_TEST_SERVER=false # disabled

Database

Sqlite by default but if you want to use postgres you can configurate it

# Dababase postgres
DATABASE_USER=<some username>
DATABASE_PASS=<some password>
DATABASE_HOST=localhost
DATABASE_PORT=5432

DATABASE_NAME=<some database name>

Redis

By default the bot can be run without radish, but if you want you can configurate it

# example
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_DB=5

Webhook

If you are using pooling leave them blank

# example
WEBHOOK_PORT=8080                                # run webhook on port
WEBHOOK_HOST=https://example.com         # webhook domain must be https
WEBHOOK_PATH=/path/to/webhook            # some custom path to webhook where telegram will send updates

Docker

Start bot

# grant execution rights
$ chmod +x ./bin/entrypoint.sh

$ docker-compose up -d --force-recreate
# or if you have make you can simply type 
$ make run
# or only make
$ make 

if you are using webhook you should uncomment ports line in docker-compose.yml

Manage bot container

$ docker-compose exec bot /bin/bash
# or if you have make you can simply type 
$ make exec

View bot logs

$ docker-compose logs -f bot
# or if you have make you can simply type 
$ make logs

Restart bot

$ docker-compose restart bot
# or if you have make you can simply type 
$ make restart

Rebuild bot

$ docker-compose build --no-cache
# or if you have make you can simply type 
$ make build

Stop bot

$ docker-compose stop
# or if you have make you can simply type 
$ make stop

Database postgres

Manage postgres via psql

$ docker-compose exec postgres psql -U postgres postgres
# or if you have make you can simply type 
$ make psql

Migrations

Create revision
$ pw_migrate create --auto --database $(python _get_database_url.py) --directory ./migrations "<some revision message>"
# or if you have make you can simply type 
$ make db_revision "<some revision message>"
Upgrade database
$ pw_migrate migrate --database $(python _get_database_url.py) --directory ./migrations
# or if you have make you can simply type 
$ make db_upgrade

you can get more information on peewee_migrate

Backup and restore

Dump database
$ mkdir -p data/backups/postgres && docker-compose exec -T postgres pg_dump -U postgres postgres --no-owner | gzip -9 > data/backups/postgres/backup-$(shell date +%Y-%m-%d_%H-%M-%S).sql.gz
# or if you have make you can simply type 
$ make pg_dump
Restore database
$ mkdir -p data/backups/postgres && bash ./bin/pg_restore.sh
# or if you have make you can simply type 
$ make pg_restore

I18n locales

Create locales

first you should extract all messages from bot

$ pybabel extract --input-dirs=. -o data/locales/bot.pot --project=bot
# or if you have make you can simply type 
$ make pybabel_extract

then init languages

$ pybabel init -i data/locales/bot.pot -d data/locales -D bot -l <language code>
# or if you have make you can simply type 
$ make pybabel_init <language code>

finaly translate messages in /data/locales/<language code>/LC_MESSAGES/bot.po and compile translations

$ pybabel compile -d data/locales -D bot --statistics
# or if you have make you can simply type 
$ make pybabel_compile

Update locales

to add new messages to already existing translations you should extract again and then write this command

$ pybabel update -i data/locales/bot.pot -d data/locales -D bot
# or if you have make you can simply type 
$ make pybabel_update

and finaly translate and compile again

Bot structure

├───bin                 # some bath scripts for docker
├───bot
│   ├───filters         # some aiogram filters
│   ├───handlers
│   │   ├───errors      # error handlers
│   │   └───users       # message handlers
│   ├───keyboards
│   │   ├───default     # aiogram markups
│   │   └───inline      # aiogram inline markups
│   ├───middlewares     # aiogram middlewares
│   └───states          # aiogram states
├───data
│   ├───backups         # database backups
│   │   └───postgres
│   ├───locales         # i18n locales
│   └───logs            # bot logs
├───models              # database models
├───services            # database services
└───utils               # some helpful things

Auto deploy

Automatically deploy bot to your server by ssh using github actions

Configure secrets

you should configure github actions secrets on https://github.com/<username>/<repo>/settings/secrets/actions

HOST - ssh server

PORT - ssh server port, by default 22

USERNAME - ssh username

KEY - private ssh key

PROJECT_PATH - path to your cloned repository

aiogram_telegram_web_bot's People

Contributors

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