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

与世无争的帅哥 提交于 2019-12-12 04:38:35

问题


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:


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.



来源:https://stackoverflow.com/questions/46194007/how-to-create-a-telegram-bot-that-only-works-for-30-days-per-user-and-then-stops

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!