繁体   English   中英

如何使用 Telethon 向我的频道发送消息

[英]How to send message to my channel using telethon

我试图让我的 python 脚本向我的私人频道发送消息,但我收到此错误。

telethon.errors.rpcerrorlist.ChatWriteForbiddenError:你不能在这个聊天中写信(由SendMessageRequest引起)

我只是该频道的管理员,这是我用于发送消息的代码,当我尝试向自己发送消息时,它工作正常..

from telethon import TelegramClient
from telethon.errors import SessionPasswordNeededError

api_id = 'my api id'
api_hash = 'my api hash'
phone = 'my number'
username = 'my username'

# Create the client and connect
client = TelegramClient(username, api_id, api_hash)
client.start()
print("Client Created")
# Ensure you're authorized
if not client.is_user_authorized():
    client.send_code_request(phone)
    try:
        client.sign_in(phone, input('Enter the code: '))
    except SessionPasswordNeededError:
        client.sign_in(password=input('Password: '))

async def send_mess(message):
    await client.send_message(entity='my channel name', message=message)

while True:
    
    some other code

    if last_message != new_message:
        with client:
            client.loop.run_until_complete(send_mess(message=new_message))
        last_message = new_message

我是否需要更改某些内容才能对我的脚本拥有管理员权限,或者问题出在哪里? 我会为每一个答案感到高兴。 给我一些好的示例代码伙计们:D 这对我来说真的是个大问题。

首先测试您是否可以向另一个频道或用户发送消息。 如果不能,则必须共享所有代码。

然后确保您在这种情况下使用的帐户是频道中的管理员。 如果帐户是管理员,则不可能出现此错误。 还要检查管理员权限并确保启用发布消息。

在此处输入图像描述

之间,使用频道用户名或频道数字 id 而不是“我的频道名称”。 频道数字 id 以-100开头,如-1001212229355 你最好使用数字ID。 要找到这个,只需将您的频道消息之一转发到https://t.me/userinfobot

await client.send_message(entity='my channel name', message=message)

暂无
暂无

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

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