[Fix] parse source and target chat id

This commit is contained in:
2024-07-03 21:18:40 +07:00
parent 39ff6ab1c6
commit 6a5e65d2f5
4 changed files with 27 additions and 21 deletions
+1 -1
View File
@@ -2,4 +2,4 @@ PHONE=+1234567890
API_ID=123456789
API_HASH=0123456789abcdef0123456789abcdef
SOURCE_GROUP_ID=-123456789
DESTINATION_GROUP_ID=-123456789
TARGET_GROUP_ID=-123456789
+1 -1
View File
@@ -161,4 +161,4 @@ cython_debug/
.idea/
*.session
*.csv
*.session-journal
+2 -2
View File
@@ -7,5 +7,5 @@ load_dotenv()
phone = os.getenv('PHONE')
api_id = os.getenv('API_ID')
api_hash = os.getenv('API_HASH')
source_group_id = os.getenv('SOURCE_GROUP_ID')
destination_group_id = os.getenv('DESTINATION_GROUP_ID')
source_group_id = int(os.getenv('SOURCE_GROUP_ID'))
target_group_id = int(os.getenv('TARGET_GROUP_ID'))
+23 -17
View File
@@ -1,26 +1,32 @@
import getpass
import asyncio
from telethon import TelegramClient
from telethon import events
from telethon.errors import SessionPasswordNeededError
from telethon.sync import TelegramClient
from config import *
# client = TelegramClient(phone, api_id, api_hash)
client = TelegramClient(phone, api_id, api_hash)
# client.connect()
# if not client.is_user_authorized():
# client.send_code_request(phone)
# try:
# client.sign_in(code=input('Enter code: '))
# except SessionPasswordNeededError:
# client.sign_in(password=getpass.getpass())
# client.start(phone)
@client.on(events.NewMessage(chats=[source_group_id]))
async def forward_message(event):
try:
# Forward the message to the target group
await client.forward_messages(target_group_id, event.message)
except Exception as e:
print(f"Exception on forward_message: {str(e)}")
with TelegramClient('session_name', api_id, api_hash) as client:
@client.on(events.NewMessage(chats=source_group_id))
async def handler(event):
await client.forward_messages(destination_group_id, event.message)
client.run_until_disconnected()
async def main():
while True:
try:
print("Starting...")
await client.start(phone)
print("Bot is running.")
await client.run_until_disconnected()
except Exception as e:
print(f"Exception on main: {str(e)}. Restarting...")
if __name__ == '__main__':
asyncio.run(main())