diff --git a/telegram/files/inputfile.py b/telegram/files/inputfile.py index a328ad8cf..0d614db34 100644 --- a/telegram/files/inputfile.py +++ b/telegram/files/inputfile.py @@ -22,7 +22,6 @@ import imghdr import mimetypes import os -import sys from uuid import uuid4 from telegram import TelegramError @@ -74,10 +73,6 @@ class InputFile(object): if not self.filename or '.' not in self.filename: self.filename = self.mimetype.replace('/', '.') - if sys.version_info < (3,): - if isinstance(self.filename, unicode): # flake8: noqa pylint: disable=E0602 - self.filename = self.filename.encode('utf-8', 'replace') - @property def field_tuple(self): return self.filename, self.input_file_content, self.mimetype diff --git a/telegram/utils/request.py b/telegram/utils/request.py index dc306f1b0..9c297a3b7 100644 --- a/telegram/utils/request.py +++ b/telegram/utils/request.py @@ -36,6 +36,7 @@ try: import telegram.vendor.ptb_urllib3.urllib3.contrib.appengine as appengine from telegram.vendor.ptb_urllib3.urllib3.connection import HTTPConnection from telegram.vendor.ptb_urllib3.urllib3.util.timeout import Timeout + from telegram.vendor.ptb_urllib3.urllib3.fields import RequestField except ImportError: # pragma: no cover warnings.warn("python-telegram-bot wasn't properly installed. Please refer to README.rst on " "how to properly install.") @@ -45,6 +46,20 @@ from telegram import (InputFile, TelegramError, InputMedia) from telegram.error import (Unauthorized, NetworkError, TimedOut, BadRequest, ChatMigrated, RetryAfter, InvalidToken) + +def _render_part(self, name, value): + """ + Monkey patch urllib3.urllib3.fields.RequestField to make it *not* support RFC2231 compliant + Content-Disposition headers since telegram servers don't understand it. Instead just escape + \ and " and replace any \n and \r with a space. + """ + value = value.replace(u'\\', u'\\\\').replace(u'"', u'\\"') + value = value.replace(u'\r', u' ').replace(u'\n', u' ') + return u'%s="%s"' % (name, value) + + +RequestField._render_part = _render_part + logging.getLogger('urllib3').setLevel(logging.WARNING) USER_AGENT = 'Python Telegram Bot (https://github.com/python-telegram-bot/python-telegram-bot)' diff --git a/tests/data/测试.png b/tests/data/测试.png new file mode 100644 index 000000000..cf0dd3089 Binary files /dev/null and b/tests/data/测试.png differ diff --git a/tests/test_photo.py b/tests/test_photo.py index b3f71d22e..521d2cd8e 100644 --- a/tests/test_photo.py +++ b/tests/test_photo.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -# +# -*- coding: utf-8 -*- # A library that provides a Python interface to the Telegram Bot API # Copyright (C) 2015-2018 # Leandro Toledo de Souza @@ -195,6 +195,21 @@ class TestPhoto(object): assert isinstance(photo.file_id, str) assert photo.file_id != '' + @flaky(3, 1) + @pytest.mark.timeout(10) + def test_send_file_unicode_filename(self, bot, chat_id): + """ + Regression test for https://github.com/python-telegram-bot/python-telegram-bot/issues/1202 + """ + with open(u'tests/data/测试.png', 'rb') as f: + message = bot.send_photo(photo=f, chat_id=chat_id) + + photo = message.photo[-1] + + assert isinstance(photo, PhotoSize) + assert isinstance(photo.file_id, str) + assert photo.file_id != '' + @flaky(3, 1) @pytest.mark.timeout(10) def test_send_bytesio_jpg_file(self, bot, chat_id):