繁体   English   中英

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

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

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

我尝试了什么:

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"))

暂无
暂无

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

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