diff --git a/README.md b/README.md index f664ecb..1c2ce7e 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # tg_bot_collections Collections of yihong0618's telegram bot -for my channel: https://t.me/hyi0618 +for yihong0618's channel: https://t.me/hyi0618 ## Bot -> poster @@ -17,5 +17,16 @@ for my channel: https://t.me/hyi0618 1. pip install -r requirements.txt 2. Get tg token, ask Google or ChatGPT, need get it from [BotFather](https://t.me/BotFather) -3. python hy_tg.py ${tg_token} +3. python tg.py ${tg_token} + + +## Contribution + +- Any issue reports or PRs are welcome. +- Any other bot type like slack/discord welcome +- Before PR, use `pip install -U black` then `black .` first + +## Appreciation + +- Thank you, that's enough. Just enjoy it. diff --git a/hy_tg.py b/tg.py similarity index 83% rename from hy_tg.py rename to tg.py index 8bb95d1..cb1c089 100644 --- a/hy_tg.py +++ b/tg.py @@ -145,7 +145,6 @@ def main(): styles_list = list(STYLES.keys()) style = random.choice(styles_list) try: - # TODO why this memory leak? with SpooledTemporaryFile(max_size=MAX_IN_MEMORY) as out_image: draw_pretty_map(location, style, out_image) # tg can only send image less than 10MB @@ -160,7 +159,35 @@ def main(): traceback.print_exc() bot.reply_to(message, "Something wrong please check") bot.delete_message(reply_message.chat.id, reply_message.message_id) - # we need this, fuck it + gc.collect() + + @bot.message_handler(content_types=["location", "venue"]) + def map_location_handler(message: Message): + # TODO refactor the function + reply_message = bot.reply_to( + message, + "Generating pretty map using location now, may take some time please wait:", + ) + location = "{0}, {1}".format( + message.location.latitude, message.location.longitude + ) + styles_list = list(STYLES.keys()) + style = random.choice(styles_list) + try: + with SpooledTemporaryFile(max_size=MAX_IN_MEMORY) as out_image: + draw_pretty_map(location, style, out_image) + # tg can only send image less than 10MB + with open("map_out.jpg", "wb") as f: # for debug + shutil.copyfileobj(out_image, f) + out_image.seek(0) + bot.send_photo( + message.chat.id, out_image, reply_to_message_id=message.message_id + ) + + except Exception: + traceback.print_exc() + bot.reply_to(message, "Something wrong please check") + bot.delete_message(reply_message.chat.id, reply_message.message_id) gc.collect() # Start bot