Kurina11
Kurina11

Reputation: 105

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.

Upvotes: 2

Views: 16084

Answers (1)

Purya
Purya

Reputation: 133

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)

Upvotes: 3

Related Questions