nickoala/telepot

Problems with sending two times the same file/photo

pfasciani opened this issue · 1 comments

I'm creating a bot for me and my classmates and i'm struggling with this error

Traceback (most recent call last): File "build/bdist.macosx-10.13-intel/egg/telepot/__init__.py", line 1158, in collector callback(item) File "/Users/piergiuliofasciani/Documents/bot.py", line 30, in on_chat_message [KeyboardButton(text="Torna al menu principale")] File "build/bdist.macosx-10.13-intel/egg/telepot/__init__.py", line 539, in sendPhoto return self._api_request_with_file('sendPhoto', _rectify(p), 'photo', photo) File "build/bdist.macosx-10.13-intel/egg/telepot/__init__.py", line 499, in _api_request_with_file return self._api_request(method, _rectify(params), files, **kwargs) File "build/bdist.macosx-10.13-intel/egg/telepot/__init__.py", line 491, in _api_request return api.request((self._token, method, params, files), **kwargs) File "build/bdist.macosx-10.13-intel/egg/telepot/api.py", line 155, in request return _parse(r) File "build/bdist.macosx-10.13-intel/egg/telepot/api.py", line 150, in _parse raise exception.TelegramError(description, error_code, data) TelegramError: (u'Bad Request: file must be non-empty', 400, {u'error_code': 400, u'ok': False, u'description': u'Bad Request: file must be non-empty'})

This occurs only when I ask two times for the same Photo or File without restarting the bot.

Can I fix this?

Here's my code

`import telepot
import io
import time
from telepot.namedtuple import ReplyKeyboardMarkup, KeyboardButton
import emoji
import os

orario = (os.path.basename("/Users/piergiuliofasciani/Desktop/orario.jpg"), open("/Users/piergiuliofasciani/Desktop/orario.jpg"))

def on_chat_message(msg):
content_type, chat_type, chat_id = telepot.glance(msg)

command = msg['text']

if command == "/start":
    bot.sendMessage(chat_id, "In cosa posso esserti utile?",
                        reply_markup=ReplyKeyboardMarkup(
                        keyboard=[
                        [KeyboardButton(text=emoji.emojize(":watch: Orario :watch:", use_aliases=True)), KeyboardButton(text=emoji.emojize(':page_with_curl: Compiti :page_with_curl:', use_aliases=True)), KeyboardButton(text=emoji.emojize(":bookmark_tabs: Circolari :bookmark_tabs:", use_aliases=True))],
                        [KeyboardButton(text=emoji.emojize(":exclamation: Avvisi :exclamation:", use_aliases=True)), KeyboardButton(text=emoji.emojize(":boom: Verifiche in Arrivo :boom:", use_aliases=True)), KeyboardButton(text=emoji.emojize(":loudspeaker: Breaking News :loudspeaker:", use_aliases=True))],
                        [KeyboardButton(text=emoji.emojize(":satisfied: Meme del giorno :satisfied:", use_aliases=True)), KeyboardButton(text=emoji.emojize(":rocket: Credits :rocket:", use_aliases=True)), KeyboardButton(text=emoji.emojize(":question: Aiuto :question:", use_aliases=True))],

                        ]
                        )),

elif command == emoji.emojize(":watch: Orario :watch:", use_aliases=True):
    bot.sendPhoto(chat_id, orario, "Ecco qui",
                    reply_markup=ReplyKeyboardMarkup(
                    keyboard=[
                    [KeyboardButton(text="Torna al menu principale")]
                    ]
                    )),

elif command == emoji.emojize(':page_with_curl: Compiti :page_with_curl:', use_aliases=True):
    bot.sendMessage(chat_id, 'Per quale giorno?',
                        reply_markup=ReplyKeyboardMarkup(
                        keyboard=[
                        [KeyboardButton(text="Lunedi"), KeyboardButton(text="Martedi"), KeyboardButton(text="Mercoledi")],
                        [KeyboardButton(text="Giovedi"), KeyboardButton(text="Venerdi"), KeyboardButton(text="Sabato")],
                        [KeyboardButton(text="Torna al menu principale")]
                        ]
                        )),

elif command == "Torna al menu principale":
    bot.sendMessage(chat_id, "D'accordo!",
                        reply_markup=ReplyKeyboardMarkup(
                        keyboard=[
                        [KeyboardButton(text=emoji.emojize(":watch: Orario :watch:", use_aliases=True)), KeyboardButton(text=emoji.emojize(':page_with_curl: Compiti :page_with_curl:', use_aliases=True)), KeyboardButton(text=emoji.emojize(":bookmark_tabs: Circolari :bookmark_tabs:", use_aliases=True))],
                        [KeyboardButton(text=emoji.emojize(":exclamation: Avvisi :exclamation:", use_aliases=True)), KeyboardButton(text=emoji.emojize(":boom: Verifiche in Arrivo :boom:", use_aliases=True)), KeyboardButton(text=emoji.emojize(":loudspeaker: Breaking News :loudspeaker:", use_aliases=True))],
                        [KeyboardButton(text=emoji.emojize(":satisfied: Meme del giorno :satisfied:", use_aliases=True)), KeyboardButton(text=emoji.emojize(":rocket: Credits :rocket:", use_aliases=True)), KeyboardButton(text=emoji.emojize(":question: Aiuto :question:", use_aliases=True))],
                        ]
                        )),

elif command == emoji.emojize(":bookmark_tabs: Circolari :bookmark_tabs:", use_aliases=True):
    bot.sendMessage(chat_id, emoji.emojize(":sweat_smile: Scusa, non ancora disponibile :sweat_smile:", use_aliases=True),
                    reply_markup=ReplyKeyboardMarkup(
                    keyboard=[
                    [KeyboardButton(text="Torna al menu principale")]
                    ]
                    )),

elif command == emoji.emojize(":exclamation: Avvisi :exclamation:", use_aliases=True):
    bot.sendMessage(chat_id, emoji.emojize(":sweat_smile: Scusa, non ancora disponibile :sweat_smile:", use_aliases=True),
                    reply_markup=ReplyKeyboardMarkup(
                    keyboard=[
                    [KeyboardButton(text="Torna al menu principale")]
                    ]
                    )),

elif command == emoji.emojize(":boom: Verifiche in Arrivo :boom:", use_aliases=True):
    bot.sendMessage(chat_id, emoji.emojize(":sweat_smile: Scusa, non ancora disponibile :sweat_smile:", use_aliases=True),
                    reply_markup=ReplyKeyboardMarkup(
                    keyboard=[
                    [KeyboardButton(text="Torna al menu principale")]
                    ]
                    )),

elif command == emoji.emojize(":rocket: Credits :rocket:", use_aliases=True):
    bot.sendMessage(chat_id, emoji.emojize(":sweat_smile: Scusa, non ancora disponibile :sweat_smile:", use_aliases=True),
                    reply_markup=ReplyKeyboardMarkup(
                    keyboard=[
                    [KeyboardButton(text="Torna al menu principale")]
                    ]
                    )),

elif command == emoji.emojize(":question: Aiuto :question:", use_aliases=True):
    bot.sendMessage(chat_id, emoji.emojize(":sweat_smile: Scusa, non ancora disponibile :sweat_smile:", use_aliases=True),
                    reply_markup=ReplyKeyboardMarkup(
                    keyboard=[
                    [KeyboardButton(text="Torna al menu principale")]
                    ]
                    )),

bot = telepot.Bot(token)
bot.message_loop(on_chat_message)
print("Awaiting Input...")
while True:
time.sleep(10)`

Hello @pfasciani

The problem is not from the telepot module. Its something with the io.BufferedReader built-in module, i don't know what happens but here is the workaround:

Open the file every time you need to send the photo instead once globally.
Sample:

elif command == emoji.emojize(":watch: Orario :watch:", use_aliases=True):
with open("/Users/piergiuliofasciani/Desktop/orario.jpg", 'rb') as photo:
bot.sendPhoto(chat_id, photo, "Ecco qui",
reply_markup=ReplyKeyboardMarkup(
keyboard=[
[KeyboardButton(text="Torna al menu principale")]
]
)),

Hope this will help you.