How can I view user reactions on my posts as an admin in a Telegram channel – Python

by
Ali Hasan
llama-cpp-python py-telegram-bot-api python-telegram-bot

Quick Fix: Here is a better version of your code:

import telebot
from telebot import types

bot = telebot.TeleBot("YOUR_BOT_TOKEN")

@bot.message_handler(commands=["start"])
def start(message):
	bot.send_message(message.chat.id, "Code is running")

@bot.message_handler(func=lambda message: True)
def get_reactions(message):
	if message.reactions:
		for reaction in message.reactions:
			user = reaction.user
			print(f"User {user.username} reacted with {reaction.emoji} to my post.")

# Start polling
bot.polling(none_stop=True)

The Problem:

As an admin of a Telegram channel, I would like to view the reactions received on my posts by individual users. Currently, Telegram doesn’t provide this functionality, so I need a solution to implement this feature. The solution should allow me to track which user sent which reaction to my posts while keeping the visibility of this information limited to me as the admin.

The Solutions:

Solution 1: Improved version of code

The provided code had a few issues:

  1. Blocking polling: The while True loop at the end blocked the polling process, preventing the code inside the @bot.message_handler(commands=["start"]) decorator from being reached.
  2. Separate polling and command handling: The polling and the command handler should be in the same block.

Here’s the improved version of the code:

import telebot
from telebot import types

bot = telebot.TeleBot("YOUR_BOT_TOKEN")

@bot.message_handler(commands=["start"])
def start(message):
    bot.send_message(message.chat.id, "Code is running")

@bot.message_handler(func=lambda message: True)
def get_reactions(message):
    if message.reactions:
        for reaction in message.reactions:
            user = reaction.user
            print(f"User {user.username} reacted with {reaction.emoji} to my post.")

# Start polling
bot.polling(none_stop=True)

Changes:

  • The while True loop and the polling are moved into the main part of the script.
  • The @bot.message_handler(func=lambda message: True) decorator is used to handle all messages.
  • The get_reactions function is called for every message and checks if there are any reactions.

Q&A

How can I see it?

You can’t see it from the given code.

How can I view user reactions in a telegram channel?

Use the following code to see reactions of users telegram channel.

What is wrong with the code?

There is a couple of issues with the code. Read fixes in Answer 1.

Video Explanation:

The following video, titled "COMMON DJANGO MISTAKES | Python - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Andrey Ivanov - Python Use my discount link for OKEX crypto exchange ... Telegram канал: https://t.me/pypapyrus Other videos on Python: https ...