简体   繁体   English

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

[英]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.

相关问题 如何使用 Telethon 获取电报私人频道 ID - How to get a telegram private channel id with telethon 如何在Telethon中向Telegram频道发送消息 - How to send message to Telegram channel in Telethon 我如何向我的联系人发送消息 whit Telethon API python 电报 - how i can send message to my contact whit telethon API python telegram 如何使用 Telethon 获取特定私人频道的更新? - How can I get updates for a specific private channel using telethon? 在 Telethon 中:当我不确定我的群组/频道是私人还是公共时,如何验证 ChatAction 是否仅来自我的群组/频道? - In Telethon: How can I verify If ChatAction was only from my group/channel, when I am not sure If my group/channel is private or public? 如何使用 Telethon 将照片发送给电报用户 - How can I send photo to telegram user with telethon 如何使用 Telethon 库从频道接收消息? - How can I receive messages from a channel using the Telethon library? 如何使用 Telethon 让所有用户进入电报私人频道? - How to get all users in a telegram private channel using telethon? 如何在 Telegram with Telethon 上创建具有使用限制的邀请到私人频道? - How do I create invites with usage limit to a private channel on Telegram with Telethon? 如何使用 Telethon 从电报频道中的消息中刮取微笑 - How to scrape smiles from a messages in telegram channel with telethon
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM