Код:
import telegram
from telegram.ext import Updater, CommandHandler
import chess
import chess.svg
import cairosvg
from cairosvg import svg2png
from io import BytesIO
import xml.etree.ElementTree as ET
TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'
updater = Updater(token=TOKEN, use_context=True)
def start(update, context):
context.chat_data['board'] = chess.Board()
update.message.reply_photo(photo=get_board(context.chat_data['board']))
def move(update, context):
if not context.args:
update.message.reply_text('Please specify a move!')
return
move = context.args[0]
board = context.chat_data['board']
try:
board.push_san(move)
send_image(context.bot, update, get_board(board))
except ValueError:
update.message.reply_text('Invalid move!')
def get_board(board):
board_string = chess.svg.board(board=board)
board_string = board_string.replace('\n', '')
output = BytesIO()
try:
cairosvg.svg2png(bytestring=board_string, write_to=output)
except ET.ParseError:
print("Error: Failed to parse SVG image")
output.seek(0)
return output.getvalue()
def send_image(bot, update, image):
bio = BytesIO()
bio.name = 'image.png'
try:
svg2png(image, write_to=bio)
except ET.ParseError:
print("Error: Failed to parse SVG image")
bio.seek(0)
bot.send_photo(chat_id=update.message.chat_id, photo=bio)
start_handler = CommandHandler('start', start)
move_handler = CommandHandler('move', move)
updater.dispatcher.add_handler(start_handler)
updater.dispatcher.add_handler(move_handler)
updater.start_polling()
updater.idle()
Ошибка:
Error: Failed to parse SVG image
No error handlers are registered, logging exception.
Traceback (most recent call last):
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\ext\dispatcher.py", line 555, in process_update
handler.handle_update(update, self, check, context)
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\ext\handler.py", line 198, in handle_update
return self.callback(update, context)
File "C:\Users\LENOVO\Desktop\test\chess_bot.py", line 29, in move
send_image(context.bot, update, get_board(board))
File "C:\Users\LENOVO\Desktop\test\chess_bot.py", line 52, in send_image
bot.send_photo(chat_id=update.message.chat_id, photo=bio)
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\bot.py", line 130, in decorator
result = func(*args, **kwargs)
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\bot.py", line 699, in send_photo
return self._message( # type: ignore[return-value]
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\ext\extbot.py", line 199, in _message
result = super()._message(
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\bot.py", line 332, in _message
result = self._post(endpoint, data, timeout=timeout, api_kwargs=api_kwargs)
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\bot.py", line 295, in _post
return self.request.post(
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\utils\request.py", line 354, in post
result = self._request_wrapper('POST', url, fields=data, **urlopen_kwargs)
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\utils\request.py", line 279, in _request_wrapper
raise BadRequest(message)
telegram.error.BadRequest: File must be non-empty
Понятия не имею, что тут можно сделать. Пожалуйста, помогите.