mirror of
				https://github.com/cdryzun/tg_bot_collections.git
				synced 2025-11-04 16:56:43 +08:00 
			
		
		
		
	
							
								
								
									
										39
									
								
								handlers/github.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								handlers/github.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,39 @@
 | 
			
		||||
import subprocess
 | 
			
		||||
 | 
			
		||||
from telebot import TeleBot
 | 
			
		||||
from telebot.types import Message
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def github_poster_handler(message: Message, bot: TeleBot):
 | 
			
		||||
    """github poster: /github <github_user_name> [<start>-<end>]"""
 | 
			
		||||
    reply_message = bot.reply_to(message, "Generating poster please wait:")
 | 
			
		||||
    m = message.text.strip()
 | 
			
		||||
    message_list = m.split(",")
 | 
			
		||||
    name = message_list[0].strip()
 | 
			
		||||
    cmd_list = ["github_poster", "github", "--github_user_name", name, "--me", name]
 | 
			
		||||
    if len(message_list) > 1:
 | 
			
		||||
        years = message_list[1]
 | 
			
		||||
        cmd_list.append("--year")
 | 
			
		||||
        cmd_list.append(years.strip())
 | 
			
		||||
    r = subprocess.check_output(cmd_list).decode("utf-8")
 | 
			
		||||
    try:
 | 
			
		||||
        if "done" in r:
 | 
			
		||||
            # TODO windows path
 | 
			
		||||
            r = subprocess.check_output(
 | 
			
		||||
                ["cairosvg", "OUT_FOLDER/github.svg", "-o", f"github_{name}.png"]
 | 
			
		||||
            ).decode("utf-8")
 | 
			
		||||
            with open(f"github_{name}.png", "rb") as photo:
 | 
			
		||||
                bot.send_photo(
 | 
			
		||||
                    message.chat.id, photo, reply_to_message_id=message.message_id
 | 
			
		||||
                )
 | 
			
		||||
    finally:
 | 
			
		||||
        bot.delete_message(reply_message.chat.id, reply_message.message_id)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def register(bot: TeleBot) -> None:
 | 
			
		||||
    bot.register_message_handler(
 | 
			
		||||
        github_poster_handler, commands=["github"], pass_bot=True
 | 
			
		||||
    )
 | 
			
		||||
    bot.register_message_handler(
 | 
			
		||||
        github_poster_handler, regexp="^github:", pass_bot=True
 | 
			
		||||
    )
 | 
			
		||||
		Reference in New Issue
	
	Block a user