-
-
Save Zulcom/2537aa7638ba81010c721049a4c577c3 to your computer and use it in GitHub Desktop.
Telegram (private) chat scrapper / forward by keywords
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
CHAT_ID_1=-1001179526170 | |
CHAT_ID_2=-1001332882905 | |
CHAT_ID_FORWARD= | |
API_ID= Obtain here: http://my.telegram.org/apps/ | |
API_HASH=Obtain here: http://my.telegram.org/apps/ | |
API_PHONE=+8882222222222 | |
API_DB_ENC=Generate any hash to encrypt DB and paste here | |
API_LIB_PATH=path to tdjson lib (if you wish build yourself: https://github.com/tdlib/td?tab=readme-ov-file#building) |
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
from telegram.client import Telegram | |
import os | |
from dotenv import load_dotenv | |
import signal | |
import sys | |
load_dotenv() | |
# ID чатов, которые нужно мониторить | |
CHAT_ID_1 = str(os.getenv("CHAT_ID_1")) | |
CHAT_ID_2 = str(os.getenv("CHAT_ID_2")) | |
FORWARD_CHAT_ID =str(os.getenv("CHAT_ID_FORWARD")) | |
# Строки для поиска | |
KEYWORDS = ["key1", "word2"] | |
tg = Telegram(api_id=os.getenv("API_ID"), | |
api_hash=os.getenv("API_HASH"), | |
phone=os.getenv("API_PHONE"), | |
database_encryption_key=os.getenv("API_DB_ENC"), | |
library_path=os.getenv("API_LIB_PATH") | |
) | |
tg.login() | |
def update_keywords(new_keywords: str): | |
global KEYWORDS | |
KEYWORDS = [word.strip().lower() for word in new_keywords.split(",")] | |
tg.send_message( | |
chat_id=FORWARD_CHAT_ID, | |
text='Keywords updated: ' + ' ; '.join(KEYWORDS), | |
) | |
def new_message_handler(update): | |
message_content = update['message']['content'].get('text', {}) | |
message_caption_content = update['message']['content'].get('caption', {}) | |
message_text = message_content.get('text', '').lower() | |
message_caption = message_caption_content.get('text','').lower() | |
if len(message_caption) > 0: | |
message_text = str(message_text) + message_caption | |
chat_id = str(update['message']['chat_id']) | |
message_id = update['message']['id'] | |
if chat_id == FORWARD_CHAT_ID: | |
if message_text.startswith("!слова:"): | |
new_keywords = message_text.replace("!слова:", "").strip() | |
update_keywords(new_keywords) | |
if chat_id in [CHAT_ID_1, CHAT_ID_2]: | |
if any(keyword in message_text.lower() for keyword in KEYWORDS): | |
data = { | |
'chat_id': FORWARD_CHAT_ID, | |
'from_chat_id': chat_id, | |
'message_ids': [message_id], | |
} | |
tg.call_method(method_name='forwardMessages', params=data, block=False) | |
else: | |
print('message',update['message'],message_text) | |
if __name__ == "__main__": | |
tg.add_message_handler(new_message_handler) | |
tg.idle() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment