Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.9k views
in Technique[技术] by (71.8m points)

python - 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.

telethon.errors.rpcerrorlist.ChatWriteForbiddenError: You can't write in this chat (caused by 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

enter image description here

between, use channel username or channel numeric id instead 'my channel name'. channel numeric id starts with -100 like -1001212229355. you better to use numeric id. to find this just forward one of your channel messages to https://t.me/userinfobot.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...