Skip to main content
  1. My Blog Posts and Stories/

Telegram Notification Channel

··549 words·3 mins

New feature added #

Blog post notifications

What happened #

As I just bought the domain for 10 years, I was wondering what kind of features I should add to the blog. This is when I come to an awesome suggestion by one of my friends. Creating a notification channel for notifying users when a new blog post is created!

The idea was so awesome that I have decided to work on it further. This is the fruits of its labour. It has been designed as a telegram bot.

Click here to join the channel and stay tuned to when new blog posts are updated.

Hopefully this serves as a form of motivation for me to work on more blog posts over the coming months/years. The information below is how I managed to do it.

How did I do it? #

The main steps are as follows:

  1. Get a bot token from @botfather. Follow the instructions from the screen and botfather will issue a token that looks like somethinghere:somethinghere. This will be the token we will be using.
  2. Create a hook on Django model creation and create the function to prompt the bot to send a message to the channel.

Getting a bot token #

  1. Visit here
  2. Type in /newbot
  3. Type in the name of the new bot (This does not have to be unique)
  4. Type in the handle for the new bot (This will have to be unique)
  5. Copy you token somewhere and go to the next step

Django model hooks #

While I am researching the problem, I was stuck at the part where the creation of the model requires me to call a function to notify the users. This reminded me of react hooks where 1 component calls the update of other components.

This prompted me to google django hooks. Which lead me to what is called django signals.

There are the following hooks below:

  1. pre-init: Before the model is created
  2. post-init: Right after the model is created
  3. pre-save: Before the model is saved
  4. post-save: After the model is saved
  5. pre-delete: Before the model is deleted
  6. post-delete: After the model is deleted

Based on the hook that we want to use, we can import it from django.db.models.

In this case I only wanted to post to telegram once it is published so we can link the function we want to call with the hooks using the following:

from django.db.models import post_save

# Signature(sender, model_instance, **kwargs)
post_save.connect(send_to_telegram, sender=Post)

By adding all the relevant information into the send_to_telegram function, I can call a function when a blog is created. Now all that is left is to implement the function.

Implementing the telegram function #

This part requires a little bit of thinking…..

What is the condition that I want to post to telegram?

  1. When the blog is created and published
  2. When the blog is changed from unpublished to published

With that and my trusty Telegram API

I can send a message to telegram with the following:

# The information is redacted here.
## Please do not hardcode credentials and put them in a config / environment variables.

So with that I am done xD. All in a day’s work

See you guys on my next blog post! Maybe more of you guys will read my blog because of the telegram channel update.