Skip to main content

A simple app to develop Telegram bot with Django

Project description

https://badge.fury.io/py/django-telegrambot.png https://travis-ci.org/JungDev/django-telegrambot.png?branch=master

A simple app to develop Telegram bots with Django

Documentation

The full documentation is at https://django-telegrambot.readthedocs.org.

Quickstart

Install django-telegrambot:

pip install django-telegrambot

Configure your installation

Add django_telegrambot in INSTALLED_APPS

#settings.py
INSTALLED_APPS = (
    ...
    'django_telegrambot',
    ...
)

And set your bots:

#settings.py
#Django Telegram Bot settings
TELEGRAM_BOT_TOKENS = ('BOT_1_token','BOT_2_token',)
TELEGRAM_WEBHOOK_SITE = 'https://mysite.it'
TELEGRAM_WEBHOOK_BASE = '/baseurl'
#TELEGRAM_WEBHOOK_CERTIFICATE = 'cert.pem' #If your site use self-signed certificate, must be set with location of your public key certificate. (More info at https://core.telegram.org/bots/self-signed )

Include in your urls.py the django_telegrambot.urls using the same value of TELEGRAM_WEBHOOK_BASE

#urls.py
urlpatterns = [
    ...
    url(r'^baseurl/', include('django_telegrambot.urls')),
    ...
]

Then use it in a project creating a module telegrambot.py in your app

#myapp/telegrambot.py
# Example code for telegrambot.py module
from telegram.ext import CommandHandler, MessageHandler, Filters
from django_telegrambot.apps import DjangoTelegramBot

import logging
logger = logging.getLogger(__name__)


# Define a few command handlers. These usually take the two arguments bot and
# update. Error handlers also receive the raised TelegramError object in error.
def start(bot, update):
    bot.sendMessage(update.message.chat_id, text='Hi!')


def help(bot, update):
    bot.sendMessage(update.message.chat_id, text='Help!')


def echo(bot, update):
    bot.sendMessage(update.message.chat_id, text=update.message.text)


def error(bot, update, error):
    logger.warn('Update "%s" caused error "%s"' % (update, error))


def main():
    logger.info("Loading handlers for telegram bot")

    # Default dispatcher (this is related to the first bot in settings.TELEGRAM_BOT_TOKENS)
    dp = DjangoTelegramBot.dispatcher
    # To get Dispatcher related to a specific bot
    # dp = DjangoTelegramBot.getDispatcher('BOT_n_token')     #get by bot token
    # dp = DjangoTelegramBot.getDispatcher('BOT_n_username')  #get by bot username

    # on different commands - answer in Telegram
    dp.add_handler(CommandHandler("start", start))
    dp.add_handler(CommandHandler("help", help))

    # on noncommand i.e message - echo the message on Telegram
    dp.add_handler(MessageHandler([Filters.text], echo))

    # log all errors
    dp.add_error_handler(error)

    # log all errors
    dp.addErrorHandler(error)

Features

  • Multiple bots

Contributing

Patches and bug reports are welcome, just please keep the style consistent with the original source.

Running Tests

Does the code actually work?

source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install -r requirements-test.txt
(myenv) $ python runtests.py

Credits

Required package: * Python Telegram Bot

Tools used in rendering this package:

History

0.2.5 (2017-03-06)

  • Fix compatibility with python-telegram-bot 5.1

0.2.4 (2016-10-04)

  • Fix compatibility with Django 1.10

0.2.3 (2016-07-30)

  • Fix default dispatcher and bot

0.2.2 (2016-07-27)

  • Fix multi workers

0.2.1 (2016-07-24)

  • Update for python-telegram-bot release v5.0

0.2.0 (2016-04-27)

  • Update for python-telegram-bot release v4.0.1

0.1.8 (2016-03-22)

  • Update for deprecation in python-telegram-bot release v3.4

0.1.5 (2016-01-28)

  • Fix compatibility.

0.1.4 (2016-01-28)

  • Fix compatibility.

0.1.3 (2016-01-28)

  • Fix setting certificate.

  • Add method DjangoTelegramBot.getBot(); get bot instance by token or username.

0.1.2 (2016-01-26)

  • First release on PyPI.

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-telegrambot-0.2.5.tar.gz (8.8 kB view hashes)

Uploaded Source

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