How to create a telegram bot that only works for 30 days per user and then stops there?

155 Views Asked by At

I built a telegram bot with a Python-Telegram-bot module, and now I want to set it up to work only 30 days, that is, when the user sends the /start to the bot, the bot will stop for 30 days.my codes:

# -*- coding: utf-8 -*-
from telegram.ext import Updater, MessageHandler, Filters, CommandHandler
import re                                                                                                                          


def delete_method(bot, update):
    if not update.message.text:
        print("it does not contain text")
        return

    mlist=['hello', 'by', 'world'] 


    for i in mlist:
        if re.search(i, update.message.text):
            bot.delete_message(chat_id=update.message.chat_id, message_id=update.message.message_id)

def start_method(bot, update):
    bot.send_message(chat_id=update.message.chat_id, "This bot only works for 30 days")

def main():
    updater = Updater(token = 'TOKEN')
    dispatcher = updater.dispatcher
    dispatcher.add_handler(MessageHandler(Filters.all, delete_method))

    start_command = CommandHandler('start', start_method)
    dispatcher.add_handler(start_command)

    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()
# for exit
# updater.idle()

What should I do or what should I add to my codes so that the bot stops working after 30 days for each user ???

1

There are 1 best solutions below

2
On

The first time a user starts the bot, you need to log a timestamp in a database along with his telegram ID.

For example you create a table with INT primary key and timestamp for the first usage.

For any "/start" command the bot receives, you insert the ID and the timestamp into the table - if it is already present you do nothing.

Anytime a message arrives to your bot, you check if the 30 days have passed or not.