簡體   English   中英

使用 Telethon 添加到電報聊天時的問題

[英]The problem when adding to a telegram chat using Telethon

我是python的新手。 我正在嘗試使用 Telethon shell 在聊天中添加和刪除用戶。 我使用從這里獲取的示例代碼https://tl.telethon.dev/methods/messages/add_chat_user.html

我的代碼:

(我在文本中用任意數字替換了 ID 和哈希)

from telethon.sync import TelegramClient
from telethon import functions, types
from telethon.errors import ChatIdInvalidError
from telethon.errors import PeerIdInvalidError
from telethon.errors import UserNotParticipantError
from telethon.tl.functions.messages import DeleteChatUserRequest
from telethon.tl.functions.messages import AddChatUserRequest


api_id = 1234567
api_hash = 'blablablabla123456'
phone = '+123456789'
name = 'testname'

with TelegramClient(name, api_id, api_hash) as client:
    result = client(functions.messages.AddChatUserRequest(
        chat_id=12345678912,
        user_id='username',
        fwd_limit=42
    ))
    print(result.stringify())

但不幸的是,對我來說它不起作用並出現錯誤。

(我在文本中用任意數字替換了 ID 和哈希。)

    Request caused struct.error: argument out of range: AddChatUserRequest(chat_id=123456789, user_id=InputUser(user_id=123456789, access_hash=-123456789789), fwd_limit=42)
Traceback (most recent call last):
  File "d:\python\AddUser\new\from telethon.tl.functions.messages impo.py", line 18, in <module>
    result = client(functions.messages.AddChatUserRequest(
  File "c:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\sync.py", line 39, in syncified
    return loop.run_until_complete(coro)
  File "c:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
    return future.result()
  File "c:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\client\users.py", line 30, in __call__      
    return await self._call(self._sender, request, ordered=ordered)
  File "c:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\client\users.py", line 58, in _call
    future = sender.send(request, ordered=ordered)
  File "c:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\network\mtprotosender.py", line 176, in send
    state = RequestState(request)
  File "c:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\network\requeststate.py", line 17, in __init__
    self.data = bytes(request)
  File "c:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\tl\tlobject.py", line 194, in __bytes__
    return self._bytes()
  File "c:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\telethon\tl\functions\messages.py", line 139, in _bytes
    struct.pack('<i', self.chat_id),
struct.error: argument out of range

電視版。 1.22.0

我將不勝感激任何幫助

謝謝!

我在問題中指出的“參數超出范圍”錯誤是因為我試圖指定一個 40 字節的 Chat_id。 而對於常規聊天,最大 id 為 32 字節。

這是因為您需要指定所謂的“真實”ID。 例如,Telegram 機器人收到的 id 看起來像這樣 -1001234567891,真實的 id 是 1234567891。

您可以使用以下代碼獲取真實ID:

from telethon import utils
real_id, peer_type = utils.resolve_id(-1001234567891)

print(real_id)  # 1234567891
print(peer_type)  # <class 'telethon.tl.types.PeerChannel'>

peer = peer_type(real_id)
print(peer)  # PeerChannel(channel_id=1234567891)

但是,如果您指定真實的 id,則可能會出現 ChatIdInvalidError。 問題是 AddChatUserRequest 方法只能與常規“聊天”一起使用在我的情況下,它是“超級組” - 是一個 Channel.megagroup 屬性設置為 True 的頻道

對於他們需要使用 InviteToChannelRequest 方法,工作的代碼是這樣的:

from telethon.sync import TelegramClient
from telethon import functions, types
    
api_id = 123456
api_hash = '******'
name = 'name'
    
with TelegramClient(name, api_id, api_hash) as client:
    result = client(functions.channels.InviteToChannelRequest(
            channel='channelname',
            users = ['username']
        ))
    print(result.stringify())

然后我創建了一個常規的“聊天”組

對她來說,工作代碼是這樣的:

from telethon.sync import TelegramClient
from telethon import functions, types
    
api_id = 123456
api_hash = '*******'
name = 'name'
      
with TelegramClient(name, api_id, api_hash) as client:
   result = client(functions.messages.AddChatUserRequest(
        chat_id=chatid,
        user_id='username',
        fwd_limit=42
    ))
    print(result.stringify())

采用:

result = await client(functions.messages.AddChatUserRequest(
        chat_id=12345678912,
        user_id='username',
        fwd_limit=42
    ))

代替:

result =  client(functions.messages.AddChatUserRequest(
        chat_id=12345678912,
        user_id='username',
        fwd_limit=42
    ))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM