Telethon SendReactionRequest报错:预期TLObject却得到其他类型
解决Telethon SendReactionRequest报错:"a TLObject was expected but found something else"
问题原因
报错核心是SendReactionRequest的reaction参数要求传入TLObject类型,你直接传了字符串格式的表情'👍',不符合接口参数规范;另外直接用频道用户名作为peer参数,也可能存在类型不匹配问题。
修复步骤
- 导入ReactionEmoji类型:从
telethon.tl.types中导入ReactionEmoji,这是Telethon定义的表情反应对应的TLObject。 - 替换reaction参数值:把
reaction='👍'替换为reaction=ReactionEmoji('👍'),传入符合要求的TLObject对象。 - 确保peer参数类型正确:通过
client.get_input_entity()方法,将频道用户名转换为Telethon可识别的InputPeer对象。
修改后的完整代码
import asyncio from telethon.sync import TelegramClient from telethon.tl.functions.messages import SendReactionRequest from telethon.tl.types import ReactionEmoji async def send_reaction(client, chat_id, message_id): try: # 获取正确的peer对象 peer = await client.get_input_entity(chat_id) await client(SendReactionRequest( peer=peer, msg_id=message_id, big=True, add_to_recent=True, reaction=ReactionEmoji('👍') )) print('Reaction sent successfully') except Exception as e: print("Error sending reaction:", str(e)) async def main(phone): # 替换为你的API ID和Hash APIID = 123456 API_HASH = 'your_api_hash_here' async with TelegramClient(f'sessions/{phone}', APIID, API_HASH) as client: if not await client.is_user_authorized(): print(f'{phone} needs to be authenticated by OTP') return chat_id = 'ViewTestChannel12' message_id = 9 await send_reaction(client, chat_id, message_id) with open('phone.csv', 'r') as file: phones = file.read().splitlines() async def run_main(): for phone in phones: await main(phone) asyncio.run(run_main())
关键修改说明
- 新增
from telethon.tl.types import ReactionEmoji导入语句,提供接口要求的TLObject类型。 - 用
await client.get_input_entity(chat_id)获取频道对应的InputPeer对象,解决peer参数类型问题。 - 将
reaction='👍'改为reaction=ReactionEmoji('👍'),传入符合规范的TLObject实例。
内容的提问来源于stack exchange,提问作者Muhammad HAMMAD ASGHAR
相关产品推荐
相关产品推荐

