电报照片机器人

时间:2017-12-31 00:15:59

标签: python python-2.7 telegram python-telegram-bot

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

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

提前帮助

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

1 个答案:

答案 0 :(得分:-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