DEV Community

Cover image for I Built a dj_telegram_bot pkg to create Telegram Bot with Django
#!z@nko...;
#!z@nko...;

Posted on • Edited on • Originally published at zankoan.com

I Built a dj_telegram_bot pkg to create Telegram Bot with Django

Hey everyone, especially those of you who are reading these posts and sticking around with me!

For a few years now I've been writing Telegram bots on top of the Django codebase I already had lying around, but at some point I thought β€” why not just turn this into a proper package? It'd be easier to install and you could get moving on your work way faster.

So that's how dj_telegram_bot came to be β€” forked from a friend's project, and I pushed it everywhere it needed to go


First stop: Why, why, and... why?

A question that probably crosses everyone's mind:

  • Why should I use this?
  • What's actually the point?
  • What does it even have over the alternatives?

Well, here are a few advantages, and there will be more in future versions.

1 - Simplicity and convenience: if you've worked with Django before, you can build a bot here faster than a "hello world" project 😁 Django's admin panel and ORM handle a ton of the work for you.

2 - Custom admin panel: there's a section of the admin panel built specifically for this library β€” you can easily add message text and create InlineKeyboardMarkup or ReplyKeyboardMarkup for them.

3 - Type-safe and ready to go: Telegram types are fully validated with pydantic, so you get helpful error hints right in your editor, plus there are some built-in defaults like a sponsor channel and bot status settings.

4 - Flexibility: you can very easily send monitoring, logging, and notifications from your Django site over to Telegram.


Second stop: how do you actually write a bot with it?

Since it's structured as a Django app, to work with it you need to build a Django project first; then add the library to INSTALLED_APPS, You'll also need your own Django app (e.g., created with django-admin startapp bot).

You can find the installation steps and more details either on the library's PyPI page or in the GitHub README!

Here are a few screenshots of how to build a simple bot (there's also a demo video on GitHub).


See how quick it is? This example is literally just copy-pasted from another bot of mine

The message-adding section in the admin panel
django admin panel to add msg and keyboards

Sample bot code
Sample telegram bot code to show options and get the link from user

Final bot output
telegram bot with dynamic keyboard and msgs


In the example above, I realized changing the "step" is a bit annoying... it'd be better to have a service for the user with an update-step method and so on.

Oh, and the docs are still at version one β€” if I get the time, adding something with mkdocs would be nice.

If you found this interesting or useful, I'd really appreciate a star on the GitHub ⭐️

If you'd like, you're welcome to contribute to the docs!!

Thanks for sticking with me and reading all the way through, hope you're doing well. Cheers ❀️

https://github.com/zankoAn/dj_telegram_bot

Top comments (0)