- Jh123x: Blog, Code, Fun and everything in between./
- My Blog Posts and Stories/
- Telegram Notification Channel/
Telegram Notification Channel
Table of Contents
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:
- 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. - 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 #
- Visit here
- Type in
/newbot
- Type in the name of the new bot (This does not have to be unique)
- Type in the handle for the new bot (This will have to be unique)
- 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:
pre-init
: Before the model is createdpost-init
: Right after the model is createdpre-save
: Before the model is savedpost-save
: After the model is savedpre-delete
: Before the model is deletedpost-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?
- When the blog is created and published
- 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.
requests.get('https://api.telegram.org/bot{token}/sendMessage?chat_id={chat_id}&parse_mode=Markdown&text={message}')
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.