Python 电报照片机器人

Python 电报照片机器人,python,python-2.7,telegram,python-telegram-bot,Python,Python 2.7,Telegram,Python Telegram Bot,我正在写一个电报机器人作为一个更大项目的一部分,但我似乎不能让机器人每次下载一个图像时都写一个新文件,相反,它只是覆盖它收到的第一个图像 我遇到的另一个问题是,它下载的图像没有用户键入/照片,我不介意,所以让我们称之为“功能”,但如果这是原因,请向我解释 请提前向Thx寻求帮助 import logging from telegram.ext import MessageHandler, Filters from telegram.ext import Updater import os imp

我正在写一个电报机器人作为一个更大项目的一部分,但我似乎不能让机器人每次下载一个图像时都写一个新文件,相反,它只是覆盖它收到的第一个图像

我遇到的另一个问题是,它下载的图像没有用户键入/照片,我不介意,所以让我们称之为“功能”,但如果这是原因,请向我解释

请提前向Thx寻求帮助

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()

时间变量应该在“def照片”中 像这样

多亏了redFIVE,它才得以解决
Grats

我强烈建议您生成一个新的身份验证令牌。它们应该是保密的。你不应该在def照片中设置时间变量吗?…天哪,你说得对,完全错了
  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()