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 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
Backend Boilerplate using Django,celery,Redis

Backend Boilerplate using Django,celery,Redis

Daniel Mawioo 2 Sep 14, 2022
Get a Django app up and running in dev, test, and production with best practices in 10 minutes

Django template for Docker + Heroku This is how I set up Django projects to get up and running as quick as possible. In includes a few neat things: De

Ben Firshman 30 Oct 13, 2022
Forkable, Minimal Template for Starknet Projects.

Forkable, Minimal Template for Starknet Projects.

velleity.eth 44 Oct 09, 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
This is a FastAPI, React, MongoDB stack Boilerplate. It's as glorious as a highland coo.

Coo - F.A.R.M stack BoilerPlate F.A.R.M - FastAPI, React, MongoDB This boilerplate utilizes FastAPI to build a REST API, MongoDB for data storage, and

Peter Waters 2 Feb 06, 2022
A command-line utility that creates projects from cookiecutters (project templates), e.g. Python package projects, VueJS projects.

Cookiecutter A command-line utility that creates projects from cookiecutters (project templates), e.g. creating a Python package project from a Python

18.7k Jan 08, 2023
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
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
Python example making use of best practice file structure and multithreading.

Python example making use of best practice file structure and multithreading.

Bob 1 Oct 13, 2021
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
A Boilerplate repo for Scientific Python Open Science projects

A Boilerplate repo for Scientific Python Open Science projects Installation Clone this repo If you need a fresh python environment, run $ conda env cr

Vincent Choqueuse 2 Dec 23, 2021
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
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
This is the starter for the Flask React project.

Flask React Project This is the starter for the Flask React project. Getting started Clone this repository (only this branch) git clone https://github

Jami Travers 5 May 25, 2022
Django Boilerplate - Material Kit Design | AppSeed

Django Boilerplate - Material Kit Design | AppSeed

App Generator 45 Dec 23, 2022
A template repository implementing HTML5 Boilerplate 8.0 in Sanic using the Domonic framework.

sanic-domonic-h5bp A template repository implementing HTML5 Boilerplate 8.0 in Sanic using the Domonic framework. If you need frontend interactivity,

PyXY 3 Dec 12, 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
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
Flask Boilerplate - Material Kit Design | AppSeed

Flask Boilerplate - Material Kit Design | AppSeed

App Generator 45 Nov 18, 2022