【问题标题】:How to send message to my channel using telethon如何使用 Telethon 向我的频道发送消息
【发布时间】:2021-04-11 18:08:33
【问题描述】:

我正在尝试让我的 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 这对我来说真的是个大问题。

【问题讨论】:

  • 三重检查这是您期望的聊天,并且是您期望的聊天机器人帐户

标签: python telegram telegram-bot python-telegram-bot telethon


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    • 2018-12-09
    • 2021-01-01
    相关资源
    最近更新 更多