DEV Community

loading...

How to send messages to all users that interact with a Telegram bot

PrinceMatthew
Just another guy that loves coding 🔥
Originally published at Medium on ・3 min read

Photo by Shahadat Rahman on Unsplash

I have recently coded my first Telegram bot and I have been searching for a way to announce the users that interact with it about the new features that are being added to the bot. It seemed like the best solution for my problem was to store every user’s chat id and to send the update message via Telegram’s API.

Searching on the internet, there were few articles, talking only about the way of sending a simple message to a SINGLE user by running a simple Python script, so I decided to do it myself.

I started my little project from a piece of code found on Medium (https://medium.com/@ManHay_Hong/how-to-create-a-telegram-bot-and-send-messages-with-python-4cf314d9fa3e). The problem is that you need to know the chat ids of all the users that you want to message. The bot needed a database.

My Telegram bot is hosted on Heroku, a really good platform for hobbyists and small teams, so I had to choose between Heroku PostgreSQL and Heroku Redis (two add-ons offered by the platform as a way to store data in a DB). The reason I stuck with Redis is obvious: the bot only needed to set a key (with the name as the user name of the Telegram user interacting with it) and its value (which in our case would be the chat id).

The plan is simple: whenever a user starts the bot, it would get its user name and its chat id and would save them in the Redis DB. Then, when we would want to send the update message to the users, it would only be enough to get the values of all the keys and send the announcement to every one of them.

Please note that, if a user sends the /start command twice, the bot won’t save the key twice, so that the script will not send the message twice to the same user.

Note: This bot was coded in Python 3.6.9, using the python_telegram_bot library.

First of all, install the Redis library by running the command :

pip3 install redis

Import the Redis library in your bot.py code, and then add after the imports:

r = redis.from_url(os.get.environ(“REDIS_URL”))

#if your bot is hosted on Heroku and you installed the Heroku Redis #add-on

r = redis.from_url(“YOUR_REDIS_DB_URL”) #general use

In the start function, these should be added:

user_id = update.message.from_user.id (gets user’s id)

user_name = update.message.from_user.name (gets user name)

r.set (user_name, user_id)

For the Python script that is used to deliver the messages, you also need to install “requests” by running:

pip3 install requests

After you add all of these, you can send your message to those users by executing this:

Note: The message_bot.txt should be the file containing the message (it can have emojis 😀). It also has to be in the same folder as the .py file

Note: Replace with your Telegram bot token and “YOUR_REDIS_DB_URL” with your DB url.

We get all keys in the Redis DB, and we get all of their values. After requesting the Telegram API to send the message to the user, we will get the response in a JSON format, so we can see if there were any errors.

I hope you found what you were looking for when reading this article.

Discussion (0)