I wrote a function on pyrogram that parses the history of a chat. The history is filtered by participant id, if a specific user wrote to the chat, the message will be forwarded to me. (We need to know: the name of the group, the id of the participant, the number of days for which we make a selection.) it works, code:
module_1
import configparser
from datetime import datetime, timedelta
from pyrogram import Client
config = configparser.ConfigParser()
config.read("config.ini")
api_id = int(config['Telegram']['api_id'])
api_hash = config['Telegram']['api_hash']
app = Client("my_account", api_id, api_hash)
# To test the function, separately from the bot!
# The data must come from the bot.
# id = input('"Enter user_id, for example: 33721 : "')
# group = input("enter group name: ")
# day = int(input("Enter number of days: "))
async def pars(day, group):
async with app:
try:
async for message in app.get_chat_history(group):
try:
if message.date < datetime.now() - timedelta(days=day):
break
else:
data = str(message.date)
user_id = str(message.from_user.id)
user_name = str(message.from_user.username)
txt = str(message.text)
kortej = data, user_id, user_name, txt
print(kortej) # All msg print in console for test
if id in kortej:
stroka = '-'.join(kortej)
await app.send_message('me', stroka) #if id was find in all hystori
except AttributeError:
print("Either it's a channel and not a chat, or it's not a text message.")
pass
except:
print("It looks like the chat doesn't exist or can't be accessed")
app.run(pars(day, group)
#module_2
import logging
from aiogram import Bot, Dispatcher, types
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters import Text
from aiogram.dispatcher.filters.state import State, StatesGroup
from aiogram.utils import executor
from auth_data import token
# Configure logging
logging.basicConfig(level=logging.INFO)
bot = Bot(token=token)
# For example use simple MemoryStorage for Dispatcher.
storage = MemoryStorage()
dp = Dispatcher(bot, storage=storage)
class Form(StatesGroup):
user_id = State()
group = State()
day = State()
@dp.message_handler(commands=['start', 'help'])
async def send_welcome(message: types.Message):
"""
This handler will be called when user sends `/start` or `/help` command
"""
# set state
await Form.user_id.set()
await message.reply(f"Enter user_id, for example: 33721")
# You can use state '*' if you need to handle all states
@dp.message_handler(state='*', commands='cancel')
@dp.message_handler(Text(equals='cancel', ignore_case=True), state='*')
async def cancel_handler(message: types.Message, state: FSMContext):
"""
Allow user to cancel any action
"""
current_state = await state.get_state()
if current_state is None:
return
logging.info('Cancelling state %r', current_state)
# Cancel state and inform user about it
await state.finish()
# And remove keyboard (just in case)
await message.reply('Cancelled.', reply_markup=types.ReplyKeyboardRemove())
@dp.message_handler(state=Form.user_id)
async def process_file_name(message: types.Message, state: FSMContext):
async with state.proxy() as data:
(data['user_id']) = message.text
await Form.next()
await message.reply("Enter the name of the group, for example: magazeta_chat")
@dp.message_handler(state=Form.group)
async def process_file_name(message: types.Message, state: FSMContext):
async with state.proxy() as data:
data['group'] = message.text
await Form.next()
await message.reply("The number of days for which you need to select messages, for example: 3")
@dp.message_handler(state=Form.day)
async def process_file_name(message: types.Message, state: FSMContext):
async with state.proxy() as data:
data['day'] = message.text
await message.bot.send_message(message.chat.id, text="Data received!")
print(data['user_id']) # These variables need to be passed to the pyrogram application!
print(data['group']) # These variables need to be passed to the pyrogram application!
print(data['day']) # These variables need to be passed to the pyrogram application!
await state.finish()
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True)
It is assumed that there is a JSON file that contains the user id, chats in which this user is a member. It is possible to fix the bot to simply accept any data; the essence of the question is how to combine all this?