Skip to content

Commit

Permalink
update load_from_to calls, and other small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aahnik committed Jun 3, 2021
1 parent 637f9e6 commit 1bc0797
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
4 changes: 2 additions & 2 deletions tgcf/bot/live_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def forward_command_handler(event):
except:
pass
config.CONFIG.forwards.append(forward)
config.from_to = config.load_from_to(config.CONFIG.forwards)
config.from_to = config.load_from_to(event.client, config.CONFIG.forwards)

await event.respond("Success")
config.write_config(config.CONFIG)
Expand Down Expand Up @@ -73,7 +73,7 @@ async def remove_command_handler(event):
print(source_to_remove)
config.CONFIG.forwards = remove_source(source_to_remove, config.CONFIG.forwards)
print(config.CONFIG.forwards)
config.from_to = config.load_from_to(config.CONFIG.forwards)
config.from_to = config.load_from_to(event.client, config.CONFIG.forwards)

await event.respond("Success")
config.write_config(config.CONFIG)
Expand Down
42 changes: 19 additions & 23 deletions tgcf/live.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
from typing import Union

from telethon import events
from telethon import TelegramClient, events, functions, types
from telethon.tl.custom.message import Message

from tgcf import config, const
Expand Down Expand Up @@ -32,25 +32,23 @@ async def new_message_handler(event: Union[Message, events.NewMessage]) -> None:
del st.stored[key]
break

to_send_to = config.from_to.get(chat_id)
dest = config.from_to.get(chat_id)

if to_send_to:

tm = await apply_plugins(message)
if not tm:
return
tm = await apply_plugins(message)
if not tm:
return

if event.is_reply:
r_event = st.DummyEvent(chat_id, event.reply_to_msg_id)
r_event_uid = st.EventUid(r_event)
if event.is_reply:
r_event = st.DummyEvent(chat_id, event.reply_to_msg_id)
r_event_uid = st.EventUid(r_event)

st.stored[event_uid] = {}
for recipient in to_send_to:
if event.is_reply and r_event_uid in st.stored:
tm.reply_to = st.stored.get(r_event_uid).get(recipient)
fwded_msg = await send_message(recipient, tm)
st.stored[event_uid].update({recipient: fwded_msg})
tm.clear()
st.stored[event_uid] = {}
for d in dest:
if event.is_reply and r_event_uid in st.stored:
tm.reply_to = st.stored.get(r_event_uid).get(d)
fwded_msg = await send_message(d, tm)
st.stored[event_uid].update({d: fwded_msg})
tm.clear()


async def edited_message_handler(event) -> None:
Expand Down Expand Up @@ -82,10 +80,10 @@ async def edited_message_handler(event) -> None:
await msg.edit(tm.text)
return

to_send_to = config.from_to.get(event.chat_id)
dest = config.from_to.get(chat_id)

for recipient in to_send_to:
await send_message(recipient, tm)
for d in dest:
await send_message(d, tm)
tm.clear()


Expand All @@ -100,7 +98,7 @@ async def deleted_message_handler(event):
event_uid = st.EventUid(event)
fwded_msgs = st.stored.get(event_uid)
if fwded_msgs:
for msg in fwded_msgs:
for _, msg in fwded_msgs.items():
await msg.delete()
return

Expand All @@ -124,8 +122,6 @@ async def deleted_message_handler(event):

async def start_sync() -> None:
"""Start tgcf live sync."""
# pylint: disable=import-outside-toplevel
from telethon.sync import TelegramClient, functions, types

client = TelegramClient(config.SESSION, config.API_ID, config.API_HASH)
await client.start(bot_token=config.BOT_TOKEN)
Expand Down
18 changes: 11 additions & 7 deletions tgcf/past.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from telethon.tl.custom.message import Message
from telethon.tl.patched import MessageService

from tgcf import config
from tgcf import storage as st
from tgcf.config import API_HASH, API_ID, CONFIG, SESSION, write_config
from tgcf.plugins import apply_plugins
Expand All @@ -22,12 +23,15 @@
async def forward_job() -> None:
"""Forward all existing messages in the concerned chats."""
async with TelegramClient(SESSION, API_ID, API_HASH) as client:
config.from_to = await config.load_from_to(client, config.CONFIG.forwards)
client: TelegramClient
for forward in CONFIG.forwards:
for from_to, forward in zip(config.from_to.items(), config.CONFIG.forwards):
src, dest = from_to
last_id = 0
logging.info(f"Forwarding messages from {forward.source} to {forward.dest}")
forward: config.Forward
logging.info(f"Forwarding messages from {src} to {dest}")
async for message in client.iter_messages(
forward.source, reverse=True, offset_id=forward.offset
src, reverse=True, offset_id=forward.offset
):
message: Message
event = st.DummyEvent(message.chat_id, message.id)
Expand All @@ -48,11 +52,11 @@ async def forward_job() -> None:
message.chat_id, message.reply_to_msg_id
)
r_event_uid = st.EventUid(r_event)
for destination in forward.dest:
for d in dest:
if message.is_reply and r_event_uid in st.stored:
tm.reply_to = st.stored.get(r_event_uid).get(destination)
fwded_msg = await send_message(destination, tm)
st.stored[event_uid].update({fwded_msg.chat_id: fwded_msg.id})
tm.reply_to = st.stored.get(r_event_uid).get(d)
fwded_msg = await send_message(d, tm)
st.stored[event_uid].update({d: fwded_msg.id})
tm.clear()
last_id = message.id
logging.info(f"forwarding message with id = {last_id}")
Expand Down

0 comments on commit 1bc0797

Please sign in to comment.