A simple cookiecutter to create Python Telegram bots, wrapped with Django.

Overview

PTB Django cookiecutter

PTB Django Cookiecutter

A simple cookiecutter to create Python Telegram bots, wrapped with Django.

Latest release Documentation Status Chat on Telegram

All Contributors

Based on this cool projects

What's inside

  • Django app with dev and prod environments.
  • Model to store bot users data.
  • Command to run the bot.
  • Admin web interface to see the bot data.
  • Authentication mechanism.
  • Some example callbacks.
  • Small engine to wrap the bot callbacks.
  • Ready for deployment using Docker via docker-compose.

Quickstart

Install the latest Cookiecutter if you haven't installed it yet:

pip install -U cookiecutter

Create your bot using this cookiecutter:

cookiecutter gh:lugodev/ptb-django-cookiecutter

This will clone the cookiecutter and launch a wizard to help you customize your new bot.

Installation

Bot engine folder structure 🤖

Once you have generated your new bot, you will get this folders structure:

  • src/bot: The bot source code.
    • core:
      • authentication.py: The authentication mechanism.
      • callbacks.py: Your callbacks here.
      • commands.py: Your commands here.
      • constants.py: Your conversation states, defined as constants.
      • conversation.py: Your conversation callbacks.
      • engine.py: The bot engine.
      • messages.py: The message filter callbacks.
      • models.py: Your bot models, defined as Django model classes.
      • renderers.py: Methods to render your messages.

Install your bot dependencies 📦

You have two primary options to install de bot Python dependencies: pip or Poetry. You can also use pipenv, virtualenvwrapper or another package managers. We recommend Poetry.

  1. Install dependencies using Poetry:

    curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
    cd src/bot
    poetry install
    

Tip (optional): Create these aliases on on your .bashrc or .zshrc, like this

```
alias poetry="python3 -m poetry"
alias django="poetry run ./dev.py"
alias djr="django runserver"
alias djm="django makemigrations && django migrate"
alias djmr="djm && djr"
```
  1. Or install your dependencies using pip.

    pip3 install -r requirements.txt
    

Initialize your bot environment variables

Place your env vars to the .env file (never push this file to the repo):

SECRET_KEY=your django random secret key
TELEGRAM_TOKEN=your bot token

Migrate database

This cookiecutter uses the Django ORM. First you must to make the model's migrations and then migrate them to database:

python3 src/bot/dev.py makemigrations
python3 src/bot/dev.py migrate

Start the bot

Start the bot (using the previously suggested alias, optional):

django runbot

Or using Poetry:

python3 -m poetry run src/bot/dev.py runbot

Or using Python:

python3 src/bot/dev.py runbot

Deploy your bot 🚀

Clone your repo to the server, and create this folder structure:

  • codebase: The repo itself, the source code.
  • storage: The place to store the DB and other persistant files.

Create and fill the ./codebase/.env file with the environment vars.

Then, deploy, using docker-compose:

cd codebase
docker-compose up --build -d

Bots created with this cookiecutter

None yet. Want to be the first? Submit your bot if it's open source.

Related videos

Crea tu bot de Telegram usando esta plantilla - PTB  + Django + Cookiecutter

Contributors

Thanks goes to these wonderful people (emoji key):


Carlos Lugones

Reinier Hernández

This project follows the all-contributors specification. Contributions of any kind welcome!

Comments
  • no command line interface raise an error

    no command line interface raise an error

    • Repo version used: lastest
    • Python version: 3.8
    • Operating System: Ubuntu 20.0.4.2

    Description

    Creating a project with no command line interface raise an error

    Output

    Select command_line_interface:
    1 - Click
    2 - Argparse
    3 - No command-line interface
    Choose from 1, 2, 3 [1]: 3
    create_author_file [y]: 
    Select open_source_license:
    1 - MIT license
    Choose from 1 [1]: 
    Traceback (most recent call last):
      File "/tmp/tmpm9jc6wfl.py", line 19, in <module>
        remove_file(cli_file)
      File "/tmp/tmpm9jc6wfl.py", line 8, in remove_file
        os.remove(os.path.join(PROJECT_DIRECTORY, filepath))
    FileNotFoundError: [Errno 2] No such file or directory: '/home/ragnarok/Projects/python/telegrambots/apimania_telegram_bot/apimania_telegram_bot/cli.py'
    ERROR: Stopping generation because post_gen_project hook script didn't exit successfully
    Hook script failed (exit status: 1)
    
    bug 
    opened by ragnarok22 4
  • error create bot using cookiecutter

    error create bot using cookiecutter

    • Repo version used:
    • Python version: 3.7.9
    • Operating System: macOS

    Description

    Me aparece este error cuando intento crear el bot con cookiecutter:

    What I Did

    Traceback (most recent call last):
      File "/Library/Frameworks/Python.framework/Versions/3.7/bin/cookiecutter", line 8, in <module>
        sys.exit(main())
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/click/core.py", line 829, in __call__
        return self.main(*args, **kwargs)
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/click/core.py", line 782, in main
        rv = self.invoke(ctx)
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/click/core.py", line 1066, in invoke
        return ctx.invoke(self.callback, **ctx.params)
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/click/core.py", line 610, in invoke
        return callback(*args, **kwargs)
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cookiecutter/cli.py", line 152, in main
        skip_if_file_exists=skip_if_file_exists,
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cookiecutter/main.py", line 74, in cookiecutter
        directory=directory,
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cookiecutter/repository.py", line 114, in determine_repo_dir
        no_input=no_input,
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cookiecutter/vcs.py", line 104, in clone
        stderr=subprocess.STDOUT,
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 411, in check_output
        **kwargs).stdout
      File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 512, in run
        output=stdout, stderr=stderr)
    subprocess.CalledProcessError: Command '['git', 'clone', 'https://github.com/lugodev/ptb-django-cookiecutter.git']' returned non-zero exit status 1.
    
    opened by diegocostares 2
  • Include python_telegram_bot_django_persistence for persistence engine

    Include python_telegram_bot_django_persistence for persistence engine

    I am an author of python-telegram-bot-django-persistence, and I think, that it would be beneficial to use it in your project template.

    This package enables you to use Django ORM for persistent data storage, so your bot does not require any infrastructure, only DB, that you already have.

    enhancement 
    opened by GamePad64 1
  • New project structure

    New project structure

    New project structure. More clean and easy to maintain. Run the following commands:

    cookiecutter ptb-django-cookiecutter
    cd myproject_name
    touch .env  # put in this file SECRET_KEY and TELEGRAM_TOKEN
    python3 manage.py makemigrations  # create the migrations for models
    python3 manage.py migrate  # insert the migrations into database
    python3 manage.py runbot  # exceture the bot!
    
    opened by ragnarok22 1
  • add database documentation and fix dev.py route

    add database documentation and fix dev.py route

    before running the bot for the first time you must carry out the migrations. The dev.py file is in the src/bot folder, not in the root of the project.

    opened by ragnarok22 0
Releases(v0.1.1)
Owner
Carlos Lugones
Startups maker. Podcaster, writer and criptoenthusiast. Teaching what I learn in my path, while boosting others' growth.
Carlos Lugones
A full stack boilerplate for FastAPI

A full stack boilerplate for FastAPI

Tyler M. Kontra 94 Dec 13, 2022
Generic python project template

generic-python-project-template generic-python-project-template STEPS - STEP 01- Create a repository by using template repository STEP 02- Clone the n

SUNNY BHAVEEN CHANDRA 3 Oct 03, 2022
A python starter package to be used as a template for creating your own python packages.

Python Starter Package This is a basic python starter package to be used as a template for creating your own python packages. Github repo: https://git

Mystic 1 Apr 04, 2022
Code Kata Python Template

Code Kata Python Template This is the code kata template for python created by Aula de Software Libre de la Universidad de Córdoba Step 1. Use this re

Sergio Gómez 2 Nov 30, 2021
NHS Theme for Streamlit applications

NHS Streamlit App Template Deployment (local) The tool has been built using Stre

nhs.pycom 3 Nov 07, 2022
Project template layout for Django 3.0+

Django 3.0+ project template This is a simple Django 3.0+ project template with my preferred setup. Most Django project templates make way too many as

José Padilla 649 Dec 30, 2022
Backend Boilerplate using Django,celery,Redis

Backend Boilerplate using Django,celery,Redis

Daniel Mawioo 2 Sep 14, 2022
Template repository to build PyTorch projects from source on any version of PyTorch/CUDA/cuDNN.

Template repository to build PyTorch projects from source on any version of PyTorch/CUDA/cuDNN.

Joonhyung Lee/이준형 651 Dec 12, 2022
A project to get you started with Docker and Django.

Docker Django tl;dr $ git clone [email protected]:erroneousboat/docker-django.git $ d

JP Bruins Slot 176 Dec 29, 2022
A Project Template With Python

File Structure . ├── LICENSE ├── Makefile # commands ├── README.md ├──

Annotation AI 61 Jan 02, 2023
Django starter project with 🔋

A batteries-included Django starter project. For a production-ready version see the book Django for Professionals. 🚀 Features Django 3.1 & Python 3.8

William Vincent 1.5k Jan 08, 2023
A simple cookiecutter to create Python Telegram bots, wrapped with Django.

PTB Django cookiecutter A simple cookiecutter to create Python Telegram bots, wrapped with Django. Based on this cool projects python-telegram-bot (PT

Carlos Lugones 20 Nov 12, 2022
A cookiecutter template for python scripts

cookiecutter-py-script A cookiecutter template for python scripts Prerequisites Git Usage pip install cookiecutter

Zeheng Li 1 Dec 14, 2022
Starter project for python based lambda project.

Serverless Python Starter Starter project for python based lambda project. Features FastAPI - Frontend dev with Hot Reload API Gateway Integration (+r

4 Feb 22, 2022
Template for creating ds simple projects

ds-project-template Template for creating ds simple projects Requirements pyenv python==3.9.4 Setup For this purpose you use following commands: pytho

1 Dec 17, 2021
The starter repository for submissions to the GeneDisco challenge for optimized experimental design in genetic perturbation experiments

GeneDisco ICLR-22 Challenge Starter Repository The starter repository for submissions to the GeneDisco challenge for optimized experimental design in

22 Dec 06, 2022
Flask Boilerplate - Material Kit Design | AppSeed

Flask Boilerplate - Material Kit Design | AppSeed

App Generator 45 Nov 18, 2022
Boilerplate for starting a python project

Python Project Boilerplate Simple boilerplate for starting a python proect. Using the repo Follow following steps to install client on server Create a

Prajwal Dahal 1 Nov 19, 2021
Template for creating PyPI project

template-for-creating-pypi-project Template for creating PyPI project Hello there! This is a template for creating a PyPI project. Fork or clone this

4 Apr 25, 2022
This is a boilerplate for a basic backend app using Python, Django and SQLite, as developed after tutorials with Programming with Mosh

This is a boilerplate for a basic backend app using Python, Django and SQLite, as developed after tutorials with Programming with Mosh

Gustavo Catala Sverdrup 1 Jan 07, 2022