繁体   English   中英

使用电报 API 和 telethon 从电报频道获取所有最近的用户或参与者

[英]Get all recent users or participants from a telegram channel with telegram API and telethon

我正在使用 peewee 将参与者存储在电报频道中。 我如何只获得新参与者,即以前没有添加的参与者?

也许我们可以抵消时间? 或被那些已经在数据库中的人抵消?

不太确定如何在 GetParticipantsRequest 中执行偏移

from telethon import TelegramClient
from telethon.tl.functions.channels import GetParticipantsRequest
from telethon.tl.types import ChannelParticipantsSearch
from time import sleep
from schema import channel_users as cudb
from datetime import datetime
import json
from dotenv import load_dotenv
load_dotenv()
import os

api_id = os.getenv('API_ID')
api_hash = os.getenv('API_HASH')
PHONE = os.getenv('PHONE')
USERNAME = os.getenv('USERNAME')


# Remember to use your own values from my.telegram.org!

client = TelegramClient('anon', api_id, api_hash)

async def main():
    # Getting information about yourself
    me = await client.get_me()
    my_channel = 'https://t.me/some_channel_url'
    offset = 0
    limit = 100
    all_participants = []

    while True:
        participants = await client(GetParticipantsRequest(
            my_channel, ChannelParticipantsSearch(''), offset, limit,
            hash=0
        ))
        if not participants.users:
            break
        all_participants.extend(participants.users)
        offset += len(participants.users)

    all_user_details = []
    for participant in all_participants:

        now = datetime.now()
        date_added = now.strftime("%d/%m/%Y, %H:%M:%S")
        channel_user_id, created = cudb.get_or_create(
            id = participant.id,
            defaults = {'first_name' : participant.first_name,
                        'last_name'  : participant.last_name,
                        'username'   : participant.username,
                        'phone'      : participant.phone,
                        'is_bot'     : participant.bot,
                        'date_added' : date_added}
        )

        if (created):
            print(f'successfully created channel_usersID = {channel_user_id}')
        else:
            print(f'did not create anything, user information found in channel_usersID {channel_user_id}')

with client:
    client.loop.run_until_complete(main())

好的,我已经用这个解决了 问题是 - 现在试图弄清楚每次新用户加入时如何更新

while True:
    participants = await client(GetParticipantsRequest(
        my_channel, ChannelParticipantsSearch(''), offset, limit,
        hash=0
    ))
    number_of_participants = len(participants.users)
    print(f'{len(participants.users)} length')
    max_cudb = cudb.select(fn.MAX(cudb.channel_usersID)).scalar()
    if max_cudb == len(participants.users):
        print('id is same as number of participants in group, hence nothing new')
        break
    if not participants.users:
        break

    # calculate the difference between number of participants and last user added to DB
    number_to_add = number_of_participants - max_cudb

    # adds missing users chronologically from oldest to most recent
    print(f'number_to_add = {number_to_add}')
    for i in range(number_to_add-1,-1,-1):
        print(f'i =  {i}')
        participant = participants.users[i]
        now = datetime.now()
        date_added = now.strftime("%d/%m/%Y, %H:%M:%S")
        channel_user_id, created = cudb.get_or_create(
            id = participant.id,
            defaults = {'first_name' : participant.first_name,
                        'last_name'  : participant.last_name,
                        'username'   : participant.username,
                        'phone'      : participant.phone,
                        'is_bot'     : participant.bot,
                        'date_added' : date_added}
        )

        # Prints status of DB addition
        if (created):
            print(f'successfully created channel_usersID = {channel_user_id}')
        else:
            print(f'did not create anything, user information found in channel_usersID {channel_user_id}')

https://docs.telethon.dev/en/stable/quick-references/events-reference.html?highlight=chataction#chataction在这里您是聊天操作的文档,您只需确保过滤事件即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM