From 621b191e8d218afe8947d564e2266a778438e8e7 Mon Sep 17 00:00:00 2001 From: eternnoir Date: Fri, 17 Jul 2015 09:35:37 +0800 Subject: [PATCH] Fix python3 can not send file by string bug. bug #40 --- telebot/apihelper.py | 11 ++++++++--- tests/test_telebot.py | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 534835b..faf1bd1 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import requests - +from six import string_types import telebot from telebot import types @@ -88,7 +88,7 @@ def send_photo(token, chat_id, photo, caption=None, reply_to_message_id=None, re method_url = r'sendPhoto' payload = {'chat_id': chat_id} files = None - if isinstance(photo, file): + if not is_string(photo): files = {'photo': photo} else: payload['photo'] = photo @@ -121,7 +121,7 @@ def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_m method_url = get_method_by_type(data_type) payload = {'chat_id': chat_id} files = None - if isinstance(data, file): + if not is_string(data): files = {data_type: data} else: payload[data_type] = data @@ -149,10 +149,15 @@ def _convert_markup(markup): return markup +def is_string(var): + return isinstance(var, string_types) + + class ApiException(Exception): """ This class represents an Exception thrown when a call to the Telegram API fails. """ + def __init__(self, function_name, result): super(ApiException, self).__init__('{0} failed. Returned result: {1}'.format(function_name, result)) self.function_name = function_name diff --git a/tests/test_telebot.py b/tests/test_telebot.py index 22629d2..3af6a03 100644 --- a/tests/test_telebot.py +++ b/tests/test_telebot.py @@ -4,6 +4,7 @@ import time sys.path.append('../') from telebot import types +from telebot import apihelper import telebot @@ -37,3 +38,17 @@ def create_text_message(text): params['text'] = text return types.Message(1, None, None, 1, 'text', params) + +def test_is_string_unicode(): + s1 = u'string' + assert apihelper.is_string(s1) + + +def test_is_string_string(): + s1 = 'string' + assert apihelper.is_string(s1) + + +def test_not_string(): + i1 = 10 + assert not apihelper.is_string(i1)