-1

I'm writing a telegram bot as a part of a larger project but i can't seem to get the bot to write a new file every time it downloads an image, instead it just overwrites the first image it recieved.

another problem i came across is that it downloads the images without the user typing /photo, i didn't mind it so lets call it a "feature" but if thats the reason please explain it to me

Thx in advance for the help

import logging
from telegram.ext import MessageHandler, Filters
from telegram.ext import Updater
import os
import os.path
import datetime
import sys
updater = Updater(token= 'omitted')
from telegram.ext import Updater, CommandHandler
time = datetime.datetime.now().strftime("%d-%m-%y_%H:%M,%S")

def start(bot, update):
    update.message.reply_text('Hello World!')

def hello(bot, update):
    update.message.reply_text(
        'Hello {}'.format(update.message.from_user.first_name))

def photo(bot, update):
    save_path = '/Users/barthofman/Desktop/grandtest/'
    file_id = update.message.photo[-1].file_id
    newFile = bot.getFile(file_id)
    newFile.download(os.path.join(save_path, time+'.jpg'))
    bot.sendMessage(chat_id=update.message.chat_id, text="download succesful")
    filename = (time+'jpg')
    with open(filename,"rb") as f:
        Jpegcontents = (f.read())
        if Jpegcontents.startswith(b"\xff\xd8") and Jpegcontents.endswith(b"\xff\xd9"):
            bot.sendMessage(chat_id=update.message.chat_id, text="Valid Image")
        if not Jpegcontents.startswith(b"\xff\xd8") and Jpegcontents.endswith(b"\xff\xd9"):
           os.system("rm ",filename)



photo_handler = MessageHandler(Filters.photo, photo)
updater.dispatcher.add_handler(photo_handler)
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CommandHandler('hello', hello))

updater.start_polling()
updater.idle()
updater.stop()
Caramiriel
  • 7,029
  • 3
  • 30
  • 50
BluePython
  • 17
  • 4

1 Answers1

-1

The Time variable should have been inside the "def photo" like so

  import logging
from telegram.ext import MessageHandler, Filters
from telegram.ext import Updater
import os
import os.path
import datetime
import sys
updater = Updater(token= 'omitted')
from telegram.ext import Updater, CommandHandler
time = datetime.datetime.now().strftime("%d-%m-%y_%H:%M,%S")

def start(bot, update):
    update.message.reply_text('Hello World!')

def hello(bot, update):
    update.message.reply_text(
        'Hello {}'.format(update.message.from_user.first_name))

def photo(bot, update):
    save_path = '/Users/barthofman/Desktop/grandtest/'
    file_id = update.message.photo[-1].file_id
    newFile = bot.getFile(file_id)
    newFile.download(os.path.join(save_path, time+'.jpg'))
    bot.sendMessage(chat_id=update.message.chat_id, text="download succesful")
    filename = (time+'jpg')
    with open(filename,"rb") as f:
        Jpegcontents = (f.read())
        if Jpegcontents.startswith(b"\xff\xd8") and Jpegcontents.endswith(b"\xff\xd9"):
            bot.sendMessage(chat_id=update.message.chat_id, text="Valid Image")
        if not Jpegcontents.startswith(b"\xff\xd8") and Jpegcontents.endswith(b"\xff\xd9"):
           os.system("rm ",filename)



photo_handler = MessageHandler(Filters.photo, photo)
updater.dispatcher.add_handler(photo_handler)
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CommandHandler('hello', hello))

updater.start_polling()
updater.idle()
updater.stop()

Thanks to redFIVE its solved Grats

BluePython
  • 17
  • 4