简体   繁体   English

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

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

I'm trying to make my python script send messages to my private channel but I get this error.我试图让我的 python 脚本向我的私人频道发送消息,但我收到此错误。

telethon.errors.rpcerrorlist.ChatWriteForbiddenError: You can't write in this chat (caused by SendMessageRequest) telethon.errors.rpcerrorlist.ChatWriteForbiddenError:你不能在这个聊天中写信(由SendMessageRequest引起)

I'm only admin in that channel and here is code what I use for sending messages, when I try to send message to myself it works fine..我只是该频道的管理员,这是我用于发送消息的代码,当我尝试向自己发送消息时,它工作正常..

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

Do I need to change something to have admin rights on my script or where is the problem?我是否需要更改某些内容才能对我的脚本拥有管理员权限,或者问题出在哪里? I'll be glad for every answer.我会为每一个答案感到高兴。 Gimme some good sample code guys:D this is really big problem for me now.给我一些好的示例代码伙计们:D 这对我来说真的是个大问题。

At first test that you can send message to another channel or user.首先测试您是否可以向另一个频道或用户发送消息。 if you can't you must share all the code.如果不能,则必须共享所有代码。

then make sure that account you are using at this case is admin in the channel.然后确保您在这种情况下使用的帐户是频道中的管理员。 it's impossible you getting this error if the account is admin.如果帐户是管理员,则不可能出现此错误。 also check admin rights and make sure post message is enable.还要检查管理员权限并确保启用发布消息。

在此处输入图像描述

between, use channel username or channel numeric id instead 'my channel name'.之间,使用频道用户名或频道数字 id 而不是“我的频道名称”。 channel numeric id starts with -100 like -1001212229355 .频道数字 id 以-100开头,如-1001212229355 you better to use numeric id.你最好使用数字ID。 to find this just forward one of your channel messages to https://t.me/userinfobot .要找到这个,只需将您的频道消息之一转发到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