Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
levtomer committed Feb 10, 2019
0 parents commit 8d87927
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.pyc
venv
.vscode
__pycache__
*.exe
config.yml
*.session
*.session-journal
13 changes: 13 additions & 0 deletions config.yml-sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
api_id: 123456 # This has to be an integer.
api_hash: 'abcdefg' # Long 32 characters hash identifier.
session_name: 'forwardgram' # Session name.

# The channel names that you'd like to forward messages from.
# The user running the client must have these channels present on it's dialogs.
input_channel_names:
- 'Channel A'
- 'Channel B'

# The output channel name that the messages will be forwarded to.
# The user running the client must be that channel admin, and have the channel present on it's dialogs.
output_channel_name: 'Deals Combined'
39 changes: 39 additions & 0 deletions forwardgram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from telethon import TelegramClient, events, sync
from telethon.tl.types import InputChannel
import yaml
import sys
import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

def start(config):
client = TelegramClient(config["session_name"],
config["api_id"],
config["api_hash"])
client.start()
input_channels_entities = []
for d in client.iter_dialogs():
if d.name in config["input_channel_names"]:
input_channels_entities.append(InputChannel(d.entity.id, d.entity.access_hash))
if d.name == config["output_channel_name"]:
output_channel_entity = InputChannel(d.entity.id, d.entity.access_hash)

if output_channel_entity is None:
logger.error(f"Could not find the channel \"{config['output_channel_name']}\" in the user's dialogs")
sys.exit(1)
print(f"Listening on {len(input_channels_entities)} channels. Forwarding messages to {config['output_channel_name']}")
@client.on(events.NewMessage(chats=input_channels_entities))
async def handler(event):
e = await client.get_entity(event.message.to_id)
await client.send_message(output_channel_entity, f"\"{e.title}\" Said:\n{event.message.message}")

client.run_until_disconnected()

if __name__ == "__main__":
if len(sys.argv) < 2:
print(f"Usage: {sys.argv[0]} CONFIG_PATH")
sys.exit(1)
with open(sys.argv[1], 'rb') as f:
config = yaml.load(f)
start(config)
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
telethon
pyyaml

0 comments on commit 8d87927

Please sign in to comment.