Cycling problem trying to do Telegram bot

30 Views Asked by At
import asyncio
import datetime
import schedule
import telebot
import threading
import time
from pyrogram import Client


user_activity = {}
bot = telebot.TeleBot(TOKEN)



def get_member_ids(api_id, api_hash, bot_token, chat_id):
    with Client('my_session', api_id=api_id, api_hash=api_hash, bot_token=bot_token) as app:
        members = app.get_chat_members(chat_id)
        user_ids = [member.user.id for member in members]
    return user_ids


def check_inactive_users(group_chat_id):
    now = datetime.datetime.now()
    inactive_period = datetime.timedelta(days=7)  # Adjust the period as needed
    flag_all_active = True
    for user_id, last_activity in list(user_activity.items()):
        if now - last_activity > inactive_period:
            try:
                flag_all_active = False
                bot.send_message(group_chat_id, f'User {user_id} is being removed due to inactivity.')
                bot.kick_chat_member(group_chat_id, user_id)
                del user_activity[user_id]
            except telebot.apihelper.ApiException as e:
                print(f"Error removing user {user_id} from group {group_chat_id}: {e}")
    if(flag_all_active): bot.send_message(group_chat_id, 'Everyone has been active! ')


def scheduled_inactivity_check():
    # Check if group_chat_id is not None before using it
    if group_chat_id is not None:
        check_inactive_users(group_chat_id)

def start_scheduler():
    # Schedule the task to run every day
    schedule.every(7).seconds.do(scheduled_inactivity_check)

    # Main thread running schedule
    while True:
        schedule.run_pending()
        time.sleep(1)

# Start the bot
@bot.message_handler(commands=['start'])
def handle_start(message):
    global group_chat_id
    group_chat_id = message.chat.id
    
    # Start the scheduler thread for inactivity check
    scheduler_thread = threading.Thread(target=start_scheduler)
    scheduler_thread.start()

    # Reply to the /start command
    bot.reply_to(message, 'InactivityBot has started. Checking for inactivity now.')
    

# Handler for all messages to update user activity
@bot.message_handler(func=lambda message: True)
def handle_all_messages(message):
    user_id = message.from_user.id
    user_activity[user_id] = datetime.datetime.now()

@bot.message_handler(content_types=['new_chat_members'])
def handle_new_chat_members(message):
    chat_id = message.chat.id
    new_members = message.new_chat_members  # List of new members

    for member in new_members:
        # You can perform actions for each new member
        user_id = member.id

        # Example: Send a welcome message
        welcome_message = f"Welcome, {user_id}, to the group! "
        bot.send_message(chat_id, welcome_message)
        user_activity[user_id] = datetime.datetime.now()

# Function to run bot polling in a separate thread
def bot_polling_thread():
    bot.polling(none_stop=True)

# Start the bot polling in a separate thread
bot_thread = threading.Thread(target=bot_polling_thread)
bot_thread.start()

time.sleep(5)

if(group_chat_id is not None):
    print("we in dis bitch")
    user_ids_initial = get_member_ids(api_id, api_hash, TOKEN, group_chat_id)
    for user_id in user_ids_initial:
        user_activity[user_id] = datetime.datetime.now()

So with Telegram-bot-api I was able to do a check of inactive users by scheduling it. The thing is if the bot is started while members are already in group and they dont send a message , they wont ever get in user_activity dictionary , therefore their timestamps won't be there , and i the wont be able to ban them from group.

So , I tried to to a check members , when bot gets the command /start , so i can fill the dictionary with user_ids and their respective time-stamps. The thing is , with Telebot i wasn't able to find a way to fetch all members user_id's from a group. So , i tried with pyrogram , but mixing 2 libraries is giving me cycling errors. So , the question is , do you know if I can do this with telebot only or is there a good , efficient way to juggle the cycles between pyrogram and Telebot? Keep in mind that for the get_member_ids to work they have to get a chat_id , and the chat_id is only retrieved through telebot /start. (thats whats causing the cycling problem , i guess) One fix I thought about is just storing manually the chat_id variable, but that doesnt feel dynamic enough :D

IF you think you can help me but are confused about my writing , ask me questions. Thanks !

0

There are 0 best solutions below