skip to Main Content

i want to make a bot which gets all the messages in a group(there are only 2 people in my group, and the bot), stores them inside a file, then delete all the messages older than 2 days, everyday at a certain time. I was successfully able to get all the messages and store in the file, but i am not sure how to delete messages older than 2 days.

This is my code till now:

from telegram.ext import Updater, MessageHandler, Filters
import schedule
import time
from datetime import datetime

#schedule.clear()

#Created an event handler
updater = Updater(token='')

# add a new event handler
dispatcher = updater.dispatcher

def job1(bot, update):
    #print(update.message.message_id)
    #bot.get_updates()[-1].message.chat_id
    #print(update.message.text)
    #print(update.message.from_user['username'])
    message_text = update.message.text
    if update.message.from_user['username'] == 'myusername':
        #print('person1')
        name = 'person1'
    else:
        #print('person2')
        name = 'person2'
    line = name + ',' + message_text + 'n'
    #print(line)
    f = open("chat.csv", "a")
    f.write(line)

def job2():
    print('works')

schedule.every().day.at('00:00').do(job2)

dispatcher.add_handler(MessageHandler(Filters.text, job1))

# start polling
updater.start_polling()

while 1:
    schedule.run_pending()
    time.sleep(1)

# # lets the  program end in terminal using ctrl+c
updater.idle()

I want the program to do the deleting of old messages inside the job2 function, but not sure how to. It works at the expected time everyday.
(also any suggestions to make the code better?)
Thanks!

2

Answers


  1. try by storing the messages id with thire date.
    you can use python dictionary

    Login or Signup to reply.
  2. When using Telegram BOT API, you can’t delete messages which are older than 2 days.

    A message can only be deleted if it was sent less than 48 hours ago.

    The only way to delete message older than 2 days is using TDLib, the Telegram API instead of Telegram BOT API. See https://core.telegram.org/api

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search