Last active
March 23, 2022 17:29
-
-
Save ak1ra-komj/08d489d8bb204ac1d8f75b35271cedf5 to your computer and use it in GitHub Desktop.
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
# coding: utf-8 | |
import re | |
import json | |
import logging | |
from telethon import TelegramClient, events | |
def init_logger(name, level=logging.INFO): | |
format = '[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s' | |
logging.basicConfig(format=format, level=level) | |
return logging.getLogger(name) | |
def init_client(session="sessions/anon", config="config.json"): | |
with open(config) as f: | |
c = json.loads(f.read()) | |
client = TelegramClient( | |
session=session, | |
api_id=c.get("api_id"), | |
api_hash=c.get("api_hash"), | |
proxy=c.get("proxy") | |
) | |
return client | |
def get_count_handler(count_type="hex"): | |
chats = settings.get(count_type).get("chats") | |
pattern = settings.get(count_type).get("pattern") | |
# helper function | |
def raw_text_plus(raw_text, count_type, step=1): | |
matched = re.search(pattern, raw_text).groups()[0] | |
if count_type == "hex": | |
return hex(eval("0x" + matched.removeprefix("0x")) + step) | |
else: | |
return str(int(matched) + step) | |
# callback function | |
async def count_handler(event:events.NewMessage.Event): | |
raw_text = event.message.raw_text.lower() | |
message = raw_text_plus(raw_text, count_type) | |
await event.respond(message) | |
event = events.NewMessage(chats, incoming=True, pattern=pattern) | |
return count_handler, event | |
settings = { | |
"int": { | |
"chats": ["@CountToOneMillion"], | |
"pattern": r"(?i)([1-9][0-9]*)", | |
}, | |
"hex": { | |
"chats": ["@CountTo0xffffffff"], | |
"pattern": r"(?i)((0x)?[0-9a-f]+)" | |
} | |
} | |
logger = init_logger(__name__) | |
client = init_client() | |
# hex | |
hex_handler, hex_event = get_count_handler(count_type="hex") | |
client.add_event_handler(hex_handler, hex_event) | |
# int | |
int_handler, int_event = get_count_handler(count_type="int") | |
client.add_event_handler(int_handler, int_event) | |
client.start() | |
client.run_until_disconnected() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment