Last active
July 16, 2024 00:46
-
-
Save savithruml/8b44298c4cfbbbdeebfb378331e9f859 to your computer and use it in GitHub Desktop.
Listens for events on the H1b-visa-slots telegram channel & notifies you (audio and/or msg forwarding)
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
#!/usr/bin/env python3 | |
# Author: SAVITHRU LOKANATH | |
# Login to Telegram on web browser https://web.telegram.org | |
# Install tesseract-ocr: apt-get install tesseract-ocr && pip install pytesseract | |
# Install Telethon lib: pip install telethon | |
# Install Text-to-Speech lib if using Windows: pip install pyttsx3 | |
# To run, increase speaker volume to 100% & in your cmd prompt: python3 listener_visa_slots.py | |
import logging | |
import os | |
import platform | |
import cv2 as cv | |
import pytesseract as tes | |
from telethon import TelegramClient, events | |
# Use your own values. Visit https://my.telegram.org/apps to create API id & key | |
_API_ID: int = <> | |
_API_KEY: str = '' | |
# This is the source channelId of the channel you want to filter notifications | |
# 1371184682 is the channelId for https://t.me/H1B_H4_Visa_Dropbox_slots | |
_SRC_CHANNEL_ID: str = '' or '1371184682' | |
# OPTIONAL: Create a telegram channel & paste its URL below | |
# The media messages from above channel will be sent to below channel if enabled | |
_TARGET_CHANNEL_URL: str = '' | |
_SESSION_NAME: str = '' or 'visa' | |
client = TelegramClient(_SESSION_NAME, _API_ID, _API_KEY) | |
async def ocr(path): | |
img = cv.imread(path) | |
data = tes.image_to_string(img) | |
if "2024" in str(data.lower()): | |
return True | |
@client.on(events.NewMessage) | |
async def my_event_handler(event): | |
send_message: bool = False | |
try: | |
peer = event.original_update.message.peer_id | |
if hasattr(peer, 'chat_id'): | |
if str(peer.chat_id) == _SRC_CHANNEL_ID: | |
media = event.original_update.message.media | |
if media: | |
if hasattr(media, 'photo'): | |
send_message = True | |
path = f"images/{media.photo.id}" | |
download_path = await client.download_media(media, path) | |
response = await ocr(download_path) | |
logging.info(f'Screenshot uploaded, id: {media.photo.id}') | |
elif hasattr(media, 'document'): | |
if media.document.mime_type == 'image/jpeg': | |
send_message = True | |
path = f"images/{media.document.id}" | |
download_path = await client.download_media(media,file=f"images/{media.document.id}") | |
response = await ocr(download_path) | |
logging.info(f'Document uploaded, id: {media.document.id}') | |
if send_message and response: | |
message = 'Important message: Screenshot uploaded, visa slots might be available' | |
if platform.system() == 'Darwin': | |
os.system(f'say {message}') | |
elif platform.system() == 'Windows': | |
import pyttsx3 | |
engine = pyttsx3.init() | |
engine.say(message) | |
engine.runAndWait() | |
if _TARGET_CHANNEL_URL: | |
entity = await client.get_entity(_TARGET_CHANNEL_URL) | |
await client.forward_messages(entity, event.message) | |
except Exception as e: | |
logging.error(e) | |
with client.start(): | |
logging.basicConfig(format='[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s', | |
level=logging.INFO) | |
logging.info('Listening to events...') | |
client.run_until_disconnected() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment