Queer European MD passionate about IT

Asyncronous python framework for Telegram bot development.

Davte 773fe1fdb1 web should not be imported from aiohttp; use aiohttp.web instead 4 anni fa
davtelepot 773fe1fdb1 web should not be imported from aiohttp; use aiohttp.web instead 4 anni fa
examples 48b7e1e4a2 Updated examples 4 anni fa
.gitignore 4f827587ee Mutable defaults removed 4 anni fa
LICENSE 4b04d10f78 First commit 6 anni fa
MANIFEST.in e8cf7cd421 Version 13 exposed secrets 5 anni fa
README.md 925c03b1ed Information updated 5 anni fa
merge_and_push.sh 6c88412a54 Update requirements 5 anni fa
requirements.txt f6cc57e70b Semplified dependencies 5 anni fa
setup.py edb2201773 Package updates checker implemented 4 anni fa
update_package.sh e481623a75 Document handlers implemented 4 anni fa

README.md

davtelepot

This project conveniently mirrors the Telegram bot API with the class Bot.

Please note that Python3.5+ is needed to run async code.

Check requirements.txt for third party dependencies.

Check out help(Bot) for detailed information.

Project folders

davtelepot/data folder

  • config.py contains configuration settings (e.g. certificate path, local_host, port etc.)
  • passwords.py contains secret information to be git-ignored (e.g. bot tokens)
  • *.db files are SQLite databases used by bots
  • *.log: log files (store log_file_name and errors_file_name in data/config.py module)

examples folder

This folder contains full-commented and ready-to-run examples for simple davtelepot.bot Telegram bots.

Usage

import sys

from davtelepot.bot import Bot

from data.passwords import my_token, my_other_token

long_polling_bot = Bot(token=my_token, database_url='my_db')
webhook_bot = Bot(token=my_other_token, hostname='example.com',
                  certificate='path/to/certificate.pem',
                  database_url='my_other_db')

@long_polling_bot.command('/foo')
async def foo_command(bot, update, user_record):
    return "Bar!"

@webhook_bot.command('/bar')
async def bar_command(bot, update, user_record):
    return "Foo!"

exit_state = Bot.run(
    local_host='127.0.0.5',
    port=8552
)
sys.exit(exit_state)

Check out help(Bot) for detailed information.

Webhook additional information

To run a bot in webhook modality, you have to provide a hostname and certificate at bot instantiation and a local_host and port when calling Bot.run method.

  • Telegram will send POST requests at https://{hostname}/webhook/{tokens}/ using certificate for encryption
  • aiohttp.web.Application server will listen on http://{local_host}:{port} for updates

It is therefore required a reverse proxy passing incoming requests to local_host.

Example of nginx reverse proxy serving this purpose

server {
  listen 8553 ssl;
  listen [::]:8553 ssl;

  server_name example.com www.example.com;

  location /telegram/ {
     proxy_pass http://127.0.0.5:8552/;
  }

  ssl_certificate /path/to/fullchain.pem;
  ssl_certificate_key /path/to/privkey.pem;
}

Example of python configuration file in this situation

# File data/config.py, gitignored and imported in main script
hostname = "https://www.example.com:8553/telegram"
certificate = "/path/to/fullchain.pem"
local_host = "127.0.0.5"
port = 8552

# Main script
from data.config import hostname, certificate, local_host, port
from data.passwords import bot_token
from davtelepot.bot import Bot

my_bot = Bot(
  token=bot_token,
  hostname=hostname,
  certificate=certificate
)

# ...

Bot.run(
  local_host=local_host,
  port=port
)