I’m trying to get messages from test_channel_1
and forward them to out_channel
to no avail.
I get the error message
telethon.errors.rpcerrorlist.UsernameNotOccupiedError: The username is not in use by anyone else yet (caused by ResolveUsernameRequest)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:UsersabcAppDataLocalProgramsPythonPython310libsite-packagestelethonclientupdates.py", line 467, in _dispatch_update
await callback(event)
File "c:Source CodePythonSingapore Jobs Channel Telegramforward_channel_messages.py", line 32, in my_event_handler
await client.send_message(out_channel, event.message)
File "C:UsersabcAppDataLocalProgramsPythonPython310libsite-packagestelethonclientmessages.py", line 810, in send_message
entity = await self.get_input_entity(entity)
File "C:UsersabcAppDataLocalProgramsPythonPython310libsite-packagestelethonclientusers.py", line 437, in get_input_entity
await self._get_entity_from_string(peer))
File "C:UsersabcAppDataLocalProgramsPythonPython310libsite-packagestelethonclientusers.py", line 556, in _get_entity_from_string
raise ValueError('No user has "{}" as username'
ValueError: No user has "out_channel" as username
Here’s my code
from telethon import TelegramClient, events
from dotenv import load_dotenv
load_dotenv()
import os
api_id = os.getenv('API_ID')
api_hash = os.getenv('API_HASH')
PHONE = os.getenv('PHONE')
USERNAME = os.getenv('USERNAME')
client = TelegramClient('session1', api_id, api_hash)
test_channel_1 = 'https://t.me/testchannel_notreal'
out_channel ='https://t.me/outchannel_notreal'
watch_channels = [test_channel_1]
@client.on(events.NewMessage(chats = watch_channels))
async def my_event_handler(event):
me = await client.get_me()
print(str(me))
print(str(event.message.text))
if me:
await client.send_message(out_channel, event.message)
# if to_chat:
# await client.send_message(to_chat, event.message)
# last_id = event.message.id
# database[event.chat_id] = last_id
# print('forwarding message with id = %s', last_id)
# logging.info('forwarding message with id = %s', last_id)
client.start()
# TODO: do `iter_messages` initial or while script was not running
print('forward_channel_messages.py has started! ...')
client.run_until_disconnected()