电报照片机器人

问题描述 投票:-1回答:1

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

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

请事先提供帮助

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()
python python-2.7 telegram python-telegram-bot
1个回答
-1
投票

时间变量应该像“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()

感谢redFIVE解决了Grats

© www.soinside.com 2019 - 2024. All rights reserved.