0

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 ???

Sajjad
  • 41
  • 1
  • 4
  • 13
  • 1
    Store chat_id (this identifies a user) and his registration date to database and before sending out any message check if 30 days have passed. If 30 days have passed do not send any message (or "You can not use this bot anymore"). – newsha Sep 13 '17 at 10:16
  • @newsha My English language is not good and I can not read your commants that you said, please correct my code – – Sajjad Sep 13 '17 at 12:05

1 Answers1

1

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.

nostradamus
  • 712
  • 12
  • 24
91DarioDev
  • 1,612
  • 1
  • 14
  • 31
  • @91DarioDevI do not understand exactly what to do. Please, if possible, correct the code. Thank you so much. – Sajjad Sep 14 '17 at 09:17
  • Two helpful answers (1 answer, 1 comment) which perfectly describe how you can solve your problem. What else can the community do if you just do not understand anything at all?! Please elaborate here or in the question what exactly your problem is... – nostradamus Sep 19 '17 at 10:56