Home > Software engineering >  How to stop and restart telegram bot
How to stop and restart telegram bot

Time:03-02

I have a translator bot in telegram group. It translates all messages that every member types in the group.

But sometimes it's annoying when loads of texts are uploaded so I want to pause some time and restart by typing some order like

/start /end

Do you think it's going to be possible to do? I want some hints

text=update.message.text.lower()

def handler(update, context):
if "/start" in text:
    try:
        if text_src==lang_type[0]: 
            a = 'ko : '   translator.translate(user_text, dest=lang_type[0]).text   '\n'   'en : '   translator.translate(user_text, dest=lang_type[1]).text   '\n'   'es : '  translator.translate(user_text, dest=lang_type[2]).text
            telegram.Bot(TelegramToken).send_message(user_id, reply_to_message_id=update.message.message_id, text=a) 
            # telegram.Bot(TelegramToken).send_message(user_id,translator.translate(user_text, dest=lang_type[2]).text) 
            # telegram.Bot(TelegramToken).send_message(user_id,translator.translate(user_text, dest=lang_type[3]).text) 

        elif text_src==lang_type[1]:
            b = 'en : '   translator.translate(user_text, dest=lang_type[1]).text   '\n'   'ko : '   translator.translate(user_text, dest=lang_type[0]).text   '\n'   'es : '   translator.translate(user_text, dest=lang_type[2]).text
            telegram.Bot(TelegramToken).send_message(user_id, reply_to_message_id=update.message.message_id, text=b)
            # telegram.Bot(TelegramToken).send_message(user_id,translator.translate(user_text, dest=lang_type[2]).text) 
            # telegram.Bot(TelegramToken).send_message(user_id,translator.translate(user_text, dest=lang_type[3]).text)

        elif text_src==lang_type[2]: 
            c = 'es : '   translator.translate(user_text, dest=lang_type[2]).text   '\n'   'ko : '   translator.translate(user_text, dest=lang_type[0]).text   '\n'   'en : '   translator.translate(user_text, dest=lang_type[1]).text
            telegram.Bot(TelegramToken).send_message(user_id, reply_to_message_id=update.message.message_id, text=c)
            # telegram.Bot(TelegramToken).send_message(user_id,translator.translate(user_text, dest=lang_type[1]).text)
            # telegram.Bot(TelegramToken).send_message(user_id,translator.translate(user_text, dest=lang_type[3]).text)

    except:
        pass

if "/end" in text:
    pass

I don't think this will work and actually doesn't. I'll be appreciated if you fix some

Cheers!

CodePudding user response:

You could use global variable to control when it can translate and when it can't do it - ie, paused. And use message /start to set it False, and /end to set it True.

Not tested code - I'm not sure if module runs all in one thread
(multiprocessing would need to keep it in file or database).

Code needs also to check user ID so only admin could do this.

paused = False  # created outside functions 

def handler(update, context):
    global paused   # inform function to use external variable instead of local variable
    
    text = update.message.text.lower().strip()
    
    if text == '/start':
        paused = False
        return 

    if text == '/end':
        paused = True
        return        
        
    if not paused:
        try:
            if text_src==lang_type[0]: 
                a = 'ko : '   translator.translate(user_text, dest=lang_type[0]).text   '\n'   'en : '   translator.translate(user_text, dest=lang_type[1]).text   '\n'   'es : '  translator.translate(user_text, dest=lang_type[2]).text
                telegram.Bot(TelegramToken).send_message(user_id, reply_to_message_id=update.message.message_id, text=a) 
                # telegram.Bot(TelegramToken).send_message(user_id,translator.translate(user_text, dest=lang_type[2]).text) 
                # telegram.Bot(TelegramToken).send_message(user_id,translator.translate(user_text, dest=lang_type[3]).text) 
    
            elif text_src==lang_type[1]:
                b = 'en : '   translator.translate(user_text, dest=lang_type[1]).text   '\n'   'ko : '   translator.translate(user_text, dest=lang_type[0]).text   '\n'   'es : '   translator.translate(user_text, dest=lang_type[2]).text
                telegram.Bot(TelegramToken).send_message(user_id, reply_to_message_id=update.message.message_id, text=b)
                # telegram.Bot(TelegramToken).send_message(user_id,translator.translate(user_text, dest=lang_type[2]).text) 
                # telegram.Bot(TelegramToken).send_message(user_id,translator.translate(user_text, dest=lang_type[3]).text)
    
            elif text_src==lang_type[2]: 
                c = 'es : '   translator.translate(user_text, dest=lang_type[2]).text   '\n'   'ko : '   translator.translate(user_text, dest=lang_type[0]).text   '\n'   'en : '   translator.translate(user_text, dest=lang_type[1]).text
                telegram.Bot(TelegramToken).send_message(user_id, reply_to_message_id=update.message.message_id, text=c)
                # telegram.Bot(TelegramToken).send_message(user_id,translator.translate(user_text, dest=lang_type[1]).text)
                # telegram.Bot(TelegramToken).send_message(user_id,translator.translate(user_text, dest=lang_type[3]).text)
    
        except Exception as ex:
            print('Exception:', ex)
  • Related