[英]How can I send messages to my private telegram channel with Telethon?
I want to send a message to a private telegram channel using python with Telethon.我想使用带有 Telethon 的 python 向私人电报频道发送消息。
What I tried:我尝试了什么:
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")
But this code gives me this error:但是这段代码给了我这个错误:
RuntimeWarning: coroutine 'UserMethods.get_entity' was never awaited
sendMSG("Channel", "Hello")
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Telethon is an asynchronous library. Telethon 是一个异步库。 This means you need to await almost everything.这意味着您几乎需要等待所有内容。
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.