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
Mad-cookiecutter - Cookiecutter templates for MaD projects

MaD Cookiecutter Templates A set of templates that can be used to quickly get st

Machine Learning and Data Analytics Lab FAU 1 Jan 10, 2022
Cookiecutter Django is a framework for jumpstarting production-ready Django projects quickly.

Cookiecutter Django Powered by Cookiecutter, Cookiecutter Django is a framework for jumpstarting production-ready Django projects quickly. Documentati

Daniel Roy Greenfeld 10k Jan 1, 2023
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 7, 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
Cookiecutter Flask OpenAPI is a template for jumpstarting production-ready Flask projects quickly.

Cookiecutter Flask OpenAPI is a template for jumpstarting production-ready Flask projects quickly. It has a well organized and scalable structure. It uses API design first

Roger Camargo 3 Nov 17, 2022
Django sample app with users including social auth via Django-AllAuth

demo-allauth-bootstrap Simple, out-of-the-box Django all-auth demo app A "brochure" or visitor (no login required) area A members-only (login required

Andrew E 215 Dec 20, 2022
Django Webpack starter template for using Webpack 5 with Django 3.1 & Bootstrap 4. Yes, it can hot-reload.

Django Webpack Starter Hello fellow human. The repo uses Python 3.9.* Django 3.1.* Webpack 5.4.* Bootstrap 4.5.* Pipenv If you have any questions twe

Ganesh Khade 56 Nov 28, 2022
Vue + Django with no compromises. Django Templates and Vue SFCs in harmony without sacrificing the full power of either.

Cookiecutter Vue Django Vue + Django with no compromise. Cookiecutter Vue Django is a framework for jumpstarting production-ready Django + Vue project

Mike Hoolehan 122 Dec 22, 2022
CRUD app to create and save code snippets, Flask/Python restful Api/backend and React/Typescript frontend

MS3 Cheat-Hub A cheatsheet hub. An app that organizes your code snippets into collections of cheat sheets and allows you to view, like and save others

Joem Elias Sanez 21 Dec 28, 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 7, 2022
A boilerplate for Django web applications

Django Hackathon Starter A boilerplate application for Django web applications. If you've attented hackathons, you already know how much time can be w

David Leonard 1.6k Dec 31, 2022
A Django project skeleton that is modern and cutting edge.

{% comment "This comment section will be deleted in the generated project" %} Edge A Fantastic Django project starter. Features Ready Bootstrap-themed

Arun Ravindran 827 Dec 15, 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 4, 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 8, 2023
A framework for launching new Django Rest Framework projects quickly.

DRFx A framework for launching new Django Rest Framework projects quickly. Comes with a custom user model, login/logout/signup, social authentication

William Vincent 400 Dec 29, 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
A project to get you started with Docker and Django.

Docker Django tl;dr $ git clone [email protected]:erroneousboat/docker-django.git $ docker-compose up Now you can access the application at https://local

JP Bruins Slot 176 Dec 29, 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 8, 2023
Django project/application starter for lazybones :)

Django Project Starter Template My custom project starter for Django! I’ll try to support every upcoming Django releases as much as I can! Requirement

Uğur Özyılmazel 40 Jul 16, 2022