简体   繁体   English

Telethon:无法以编程方式启动 session

[英]Telethon: can't start a session programatically

I am trying to start a TelegramClient session inside a function.我正在尝试在 function 中启动TelegramClient session。

there are no errors, but the decorator for events.NewMessage doesn't seem to act...没有错误,但events.NewMessage的装饰器似乎没有行动......

here is the code I use:这是我使用的代码:

async def runClient(sessionName):
  client = getClientBySessionName(sessionName) # this is constructed with TelegramClient from telethon.sync

  await client.connect()
  me = await client.get_me()
  print(me.first_name)
  await client.disconnect()
  if not me:
    return
    
  await client.start()

  @client.on(events.NewMessage)
  async def nmh(event):
    await newMessageHandler(event, client)

async def newMessageHandler(event, client):
  print(event.raw_text)

and to make sure that I have created the TelegramClient right, I have tested other methods on it, and it works fine并确保我创建了正确的 TelegramClient,我已经测试了其他方法,它工作正常

problem:问题:

when new messages accrue, nothing is logged当新消息出现时,不会记录任何内容

Did you try something like this?你尝试过这样的事情吗?

@client.on(events.NewMessage(chats=user_input_channel))
    async def newMessageListener(event):
        ...

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

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