2017-12-31 60 views
-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() 
+0

새 인증 토큰을 생성하는 것이 좋습니다. 그들은 비공개로 유지해야합니다. – Caramiriel

+0

def 사진 안에 시간 변수를 설정하면 안됩니까? – redFIVE

+0

... 당신이 맞다, 완전히 놓친 – BluePython

답변

-1

시간 변수에 미리 들으을 redFIVE 너무

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

감사처럼 "데프 사진" 내부에 있었어야 해결 된 그림

+0

이 예제는 여전히 잘못되었습니다 .... – redFIVE

+0

롤, 어쨌든 그것이 작동 작품 : P – BluePython