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
Cookiecutter to create a Google Function. Powered by Poetry, GitHub actions, and Google Cloud Platform

Cookiecutter Google Function Cookiecutter template for a Google Function. Powered by Poetry, and GitHub actions. Quickstart Install the latest Cookiec

Arthur 1 Jan 07, 2022
Launchr is an open source SaaS starter kit, based on Django.

Launchr Launchr is an open source SaaS starter kit. About Launchr is a fully-equipped starter template, ready to start a SaaS web app. It implements t

Jannis Gebauer 183 Jan 06, 2023
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
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 full stack boilerplate for FastAPI

A full stack boilerplate for FastAPI

Tyler M. Kontra 94 Dec 13, 2022
Open-source full-stack seed project that uses a React UI powered by a simple Flask API Server

React Flask Authentication Open-source full-stack seed project that uses a React UI powered by a simple Flask API Server.

App Generator 37 Dec 24, 2022
A Project Template With Python

File Structure . β”œβ”€β”€ LICENSE β”œβ”€β”€ Makefile # commands β”œβ”€β”€ README.md β”œβ”€β”€

Annotation AI 61 Jan 02, 2023
Backend Boilerplate using Django,celery,Redis

Backend Boilerplate using Django,celery,Redis

Daniel Mawioo 2 Sep 14, 2022
A template for some new Python tool or package with a reasonable basic setup.

python-app-template A template with a reasonable basic setup, including: black (formatting) flake8 (linting) mypy (type checking) isort (import sortin

Anton Pirogov 3 Jul 19, 2022
A low dependency and really simple to start project template for Python Projects.

Python Project Template A low dependency and really simple to start project template for Python Projects. HOW TO USE THIS TEMPLATE DO NOT FORK this is

Yurii Dubinka 5 Jan 21, 2022
Basic Docker Compose template application with Flask, Celery, Redis, MySQL, SocketIO, Nginx and Gunicorn.

Nginx / Gunicorn / Flask 🐍 / Celery / SocketIO / MySQL / Redis / Docker 🐳 sample application Basic Docker Compose template application for orchestat

Alex Oarga 8 Aug 06, 2022
Bleeding edge django template focused on code quality and security.

wemake-django-template Bleeding edge django2.2 template focused on code quality and security. Purpose This project is used to scaffold a django projec

wemake.services 1.6k Jan 04, 2023
Boilerplate code for a Python Flask API

MrMat :: Python :: API :: Flask Boilerplate code for a Python Flask API This variant of a Python Flask API is code-first and using native Flask Featur

0 Dec 26, 2021
Flask Boilerplate - Material Kit Design | AppSeed

Flask Boilerplate - Material Kit Design | AppSeed

App Generator 45 Nov 18, 2022
A boilerplate Django project for quickly getting started.

The Definitive Django Learning Platform. Django Project Boilerplate This repository is a boilerplate Django project for quickly getting started. Getti

Le Huynh Long 1 Nov 01, 2021
A Django starter template with a sound foundation.

SOS Django Template SOS Django Tempalate is a Django starter template that has opinionated and good solutions while starting your new Django project.

Eray Erdin 19 Oct 30, 2022
Forkable, Minimal Template for Starknet Projects.

Forkable, Minimal Template for Starknet Projects.

velleity.eth 44 Oct 09, 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
Forkable, Minimal Template for Starknet Projects.

Forkable, Minimal Template for Starknet Projects.

andreas 44 Oct 09, 2022
Cookiecutter-allpurpose-minimal-python - A simple cookiecutter template for general-purpose python projects.

cookiecutter-allpurpose-minimal-python A simple cookiecutter template for general-purpose python projects. To use, run pip install cookiecutter cookie

E. Tolga Ayan 2 Jan 24, 2022