如何使用 Telethon 将消息发送到我的私人电报频道?

How can I send messages to my private telegram channel with Telethon?

我想使用 python 和 Telethon 向私人电报频道发送消息。

我尝试了什么:

from telethon import TelegramClient
from telethon.tl.types import Channel

client = TelegramClient('fx', api id, "API hash")
client.start()


def sendMSG(channel, msg):
    entity = client.get_entity(channel)
    client.send_message(entity = entity,message=msg)


sendMSG("Channel Name", "Hello")

但是这段代码给我这个错误:

RuntimeWarning: coroutine 'UserMethods.get_entity' was never awaited
  sendMSG("Channel", "Hello")
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

Telethon 是一个异步库。这意味着您几乎需要等待所有事情。

import asyncio

async def sendMSG(channel, msg):
    entity = client.get_entity(channel)
    await client.send_message(entity = entity,message=msg)


asyncio.run(sendMSG("Channel Name", "Hello"))