Skip to content

jansenicus/python-telegram-bot-template

Repository files navigation

Python Telegram Bot Template

Modular customizable python-telegram-bot template to speed-up development process.

Features

  • Easy configuration with a yaml file

  • Clear separation between Development Bot vs Production Bot

  • Automated help text based on docstring

  • Simplicity and sophistication met in one handlers directory

  • Ready to deploy as a docker container

Easy configuration with a yaml file

configuration.yaml

prod:
  - botname: My Production Bot
    username: my_production_bot
    token: 
      1234567890:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi
    about:
      "this is some short text about my bot"
    description:
      "This is a description of what my bot can do which customer will use"

Clear separation between Development Bot vs Production Bot

configuration.yaml

dev:
  - botname: Development Bot
    username: my_development_bot
    token: 
      1234567890:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi
    about:
      "this is some short text about development bot  "
    description:
      "This is a description of what my development bot can do with testing"

choose the mode

mode: dev

or

mode: prod

Simplicity and sophistication met in one place

  • handlers directory: as a placeholder for all command handlers
  • index.py file: to register all the function members
  • sample command handlers
    • echo.py file: /echo command handler
    • start.py file: /start command handler
    • hello.py file: /hello command handler
    • help.py file: /help command handler
    • whoami.py file: /whoami command handler

Automated help text based on docstring

  • help.py will generate help text using docstring from all the function members
  • writing help as you code
    def hello(update, context):
    """
    /hello
    just say hello and reply
    """
    update.message.reply_text(
        'Hi {}, how are you?'.format(update.message.from_user.first_name))
  • will produce help text
    /hello
    just say hello and reply
    

Ready to deploy as a docker container

Just run

docker-compose up --build

Steps

1. Preparation

  • prepare two bot accounts from @botfather
  • one bot will be used for development
  • another bot for production

2. Clone This Repository

  git clone https://github.com/jansenicus/python-telegram-bot-template

3. Edit configuration file

  • edit configuration-sample.yaml and save it as configuration.yaml
mode: prod
prod:
- botname: My Production Bot
 username: my_production_bot
 token: 
   1234567890:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi
 about:
   "this is some short text about my bot"
 description:
   "This is a description of what my bot can do which customer will use"

4. Run Directly in local machine

Make sure you are in the bot directory to install all the requirements and then run the telegram bot

  cd bot
  pipenv install -r requirements.txt
  pipenv run python main.py

5. Build and Run Docker container as a Service

Build Docker container

    docker build -t python-telegram-bot -f Dockerfile .

Run Docker container as a Service

    docker run -it --workdir /home python-telegram-bot pipenv run python main.py

Build and run in one go

You could also build and run in one go for the first time

    docker-compose up -d

or you could also force build every you want to apply change into the image

    docker-compose up -d --build

Command Handlers for Further Development

All command handlers are put in one directory handlers and registered in one file index.py. The result is a convenient way to import all command handlers in one import call:

from handlers.index import index

Read More...

About

This is a python-telegram-bot-template repository, ready to deploy as a container

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published