Skip to main content

Django Telegram bot base config

Project description

django-tbot-base

Django Telegram bot base config

Installation:

pip install django-tbot-base

Setting up

YourProject/settings.py

# Application definition
INSTALLED_APPS = [
    'tbot_base',
    ...
]

# Add your bot handlers in order of priority
BOT_HANDLERS = [
   'tbot.handlers',
]

YourProject/urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('tbot_base.urls')),  # include webhook url
]
python manage.py makemigrations tbot_base
python manage.py migrate

Usage

Handlers

YourProject/tbot/handlers.py

from telebot import types
from telebot.apihelper import ApiTelegramException

from tbot_base.bot import tbot


@tbot.message_handler(func=lambda message: True)
def text_messages(message: types.Message):
    tbot.send_message(message.from_user.id, 'Hello!')


@tbot.callback_query_handler(func=lambda call: True)
def callback_inline(call: types.CallbackQuery):
    tbot.send_message(call.from_user.id, 'Hello!')

    # remove the "clock" on the inline button
    try:
        tbot.answer_callback_query(callback_query_id=call.id, text='')
    except ApiTelegramException:
        pass

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-tbot-base-1.0.1.tar.gz (4.6 kB view hashes)

Uploaded Source

Built Distribution

django_tbot_base-1.0.1-py3-none-any.whl (6.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page