this post was submitted on 03 Jul 2023
1 points (100.0% liked)

Telegram Bots

41 readers
0 users here now

founded 1 year ago
MODERATORS
 
This is an automated archive made by the Lemmit Bot.

The original was posted on /r/telegrambots by /u/Riwil on 2023-06-27 17:19:00+00:00.


Hello guys, I need help.

I need a code for a Python bot that does the following:

It should check within a Telegram group if there are 2 or more messages that contain a line saying (Original problem ID: x), where X will vary and will be an integer, not a fixed value.

If it detects that there is more than 1 message with that content and the same X value, it should delete the messages with the same number.

The bot should run approximately once every hour.

The bot is executed in Python 3 on Ubuntu.

I have this code already, but it doesn't work and gives me an error on line 52.

import time
from telegram import Bot, Update
from telegram.ext import Updater, CommandHandler

# Bot token
TOKEN = 'YOUR_TOKEN'

# Group ID
GROUP_ID = 'Group_ID'

# Dictionary to store messages with the same ID
messages = {}

# Function to handle the /start command
def start(update: Update, context) -> None:
    context.bot.send_message(
        chat_id=update.effective_chat.id,
        text='Hello! I am the duplicate message removal bot.'
    )

# Function to check and remove duplicate messages
def check_duplicates(context) -> None:
    global messages

    # Get messages from the group
    updates = context.bot.get_chat(GROUP_ID).get('updates')

    # Reset the messages dictionary on each execution
    messages = {}

    # Iterate over the messages
    for update in updates:
        message = update.get('message')
        text = message.get('text')

        # Check if the message contains the line "Original problem ID: x"
        if text and 'Original problem ID:' in text:
            # Extract the ID number
            id_number = int(text.split(':')[-1].strip())

            # Check if there are already messages with the same ID
            if id_number in messages:
                # If there are more than one message with the same ID, remove them
                if messages[id_number] != message['message_id']:
                    context.bot.delete_message(chat_id=GROUP_ID, message_id=message['message_id'])
            else:
                # Store the message ID
                messages[id_number] = message['message_id']

# Create the bot
bot = Bot(token=TOKEN)
updater = Updater(bot=bot)

# Add the handler for the /start command
updater.dispatcher.add_handler(CommandHandler('start', start))

# Get the update queue from the updater
update_queue = updater.update_queue

# Schedule the execution of the duplicate check every hour
updater.job_queue.run_repeating(check_duplicates, interval=3600, first=0)

# Start the bot
updater.start_polling()

# Keep the bot running until manually stopped
updater.idle()

If someone knows what I'm doing wrong, please let me know. Thank you.

no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here