-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
levtomer
committed
Feb 10, 2019
0 parents
commit 8d87927
Showing
4 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
*.pyc | ||
venv | ||
.vscode | ||
__pycache__ | ||
*.exe | ||
config.yml | ||
*.session | ||
*.session-journal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
telethon | ||
pyyaml |