R
R
r1dddy4sv2021-03-21 19:25:03
Python
r1dddy4sv, 2021-03-21 19:25:03

Sending TelegramBotAPI messages on a timer while keeping receiving messages?

I have a normal bot that accepts and responds to messages, but one of the bot's functions is to send a message at a specific time set by the user.
So, I have no idea how to do this, the whole problem is the need to constantly maintain the usual work of the bot, so each user can create several such messages.

I use mysql on heroku, this is how I tried to solve this problem:

g=datetime(year=int(message.text[0:4]), day=int(message.text[5:7]), month=int(message.text[8:10]),hour=int(message.text[11:13]), minute=int(message.text[14:16]),second=0 , tzinfo=pytz.timezone('Europe/Moscow'))
        try:
            conn = create_connection_mysql_db('***', 
                                            '***', 
                                            '***',
                                            '***')
            cursor = conn.cursor()
            cursor.execute(f'''UPDATE posts SET time = '{message.text}' WHERE id ={int(message.chat.id)}''')

            create_table_query = f'''SELECT post FROM posts WHERE id ={int(message.chat.id)} AND time = "{message.text}"'''
            cursor.execute(create_table_query)
            query_result = cursor.fetchall()
            f=',,, '.join(str(x) for x in query_result)
            conn.commit()
        except Error as error:
            print(error)
        finally:
            cursor.close()
            conn.close() 
        try:
            conn = create_connection_mysql_db('***', 
                                            '***', 
                                            '***',
                                            '***')
            cursor = conn.cursor()
            create_table_query = f'''
                                UPDATE alls
                                SET state = 'gl'
                                WHERE id = {message.chat.id};'''
            cursor.execute(create_table_query)
            conn.commit()
        except Error as error:
            print(error)
        finally:
            cursor.close()
            conn.close() 
        bot.send_message(message.chat.id,'Хорошо, отправлю тебе сообщение в установленное время!')

        for p in f.split(',,, '):
            t=False
            while t==False:
                if g < pytz.utc.localize(datetime.utcnow()).astimezone(pytz.timezone('Europe/Moscow')):
                    bot.send_message(message.chat.id,p[2:-3])
                    try:
                        conn = create_connection_mysql_db('***', 
                                                        '***', 
                                                        '***',
                                                        '***')
                        cursor = conn.cursor()
                        create_table_query = f'''DELETE FROM posts WHERE id = {int(message.chat.id)} AND post = "{p[2:-3]}"'''
                        cursor.execute(create_table_query)
                        conn.commit()
                    except Error as error:
                        print(error)
                    finally:
                        cursor.close()
                        conn.close() 
                    t=True

But all this does not work correctly, and after two messages on the timer, the bot stops receiving messages at all

Help
Thank you!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question