1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Merge branch 'develop'

This commit is contained in:
eternnoir
2015-10-12 22:37:17 +08:00
12 changed files with 467 additions and 45 deletions

View File

@ -115,7 +115,7 @@ class TeleBot:
for listener in self.update_listener:
self.__exec_task(listener, new_messages)
def polling(self, none_stop=False, interval=0, timeout=3):
def polling(self, none_stop=False, interval=0, timeout=20):
"""
This function creates a new Thread that calls an internal __retrieve_updates function.
This allows the bot to retrieve Updates automagically and notify listeners and message handlers accordingly.
@ -149,8 +149,7 @@ class TeleBot:
try:
polling_thread.put(self.__retrieve_updates, timeout)
while not or_event.is_set():
time.sleep(.05) # wait for polling thread finish, polling thread error or thread pool error
or_event.wait() # wait for polling thread finish, polling thread error or thread pool error
polling_thread.raise_exceptions()
self.worker_pool.raise_exceptions()

View File

@ -43,14 +43,14 @@ def _check_result(method_name, result):
"""
if result.status_code != 200:
msg = 'The server returned HTTP {0} {1}. Response body:\n[{2}]'\
.format(result.status_code, result.reason, result.text)
.format(result.status_code, result.reason, result.text.encode('utf8'))
raise ApiException(msg, method_name, result)
try:
result_json = result.json()
except:
msg = 'The server returned an invalid JSON response. Response body:\n[{0}]'\
.format(result.text)
.format(result.text.encode('utf8'))
raise ApiException(msg, method_name, result)
if not result_json['ok']:

View File

@ -29,6 +29,7 @@ class JsonSerializable:
Subclasses of this class are guaranteed to be able to be converted to JSON format.
All subclasses of this class must override to_json.
"""
def to_json(self):
"""
Returns a JSON string representation of this class.
@ -44,6 +45,7 @@ class JsonDeserializable:
Subclasses of this class are guaranteed to be able to be created from a json-style dict or json formatted string.
All subclasses of this class must override de_json.
"""
@classmethod
def de_json(cls, json_type):
"""
@ -71,7 +73,7 @@ class JsonDeserializable:
def __str__(self):
d = {}
for x, y in self.__dict__.iteritems():
for x, y in six.iteritems(self.__dict__):
if hasattr(y, '__dict__'):
d[x] = y.__dict__
else:
@ -79,6 +81,7 @@ class JsonDeserializable:
return six.text_type(d)
class Update(JsonDeserializable):
@classmethod
def de_json(cls, json_type):
@ -91,6 +94,7 @@ class Update(JsonDeserializable):
self.update_id = update_id
self.message = message
class User(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
@ -125,13 +129,44 @@ class GroupChat(JsonDeserializable):
self.title = title
class Chat(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
id = obj['id']
type = obj['type']
title = None
username = None
first_name = None
last_name = None
if 'title' in obj:
title = obj['title']
if 'username' in obj:
username = obj['username']
if 'first_name'in obj:
first_name = obj['first_name']
if 'last_name' in obj:
last_name = obj['last_name']
return Chat(id, type, title, username, first_name, last_name)
def __init__(self, id, type, title=None, username=None, first_name=None, last_name=None):
self.type = type
self.last_name = last_name
self.first_name = first_name
self.username = username
self.id = id
self.title = title
class Message(JsonDeserializable):
@classmethod
def de_json(cls, json_string):
obj = cls.check_json(json_string)
message_id = obj['message_id']
from_user = User.de_json(obj['from'])
chat = Message.parse_chat(obj['chat'])
from_user = None
if 'from' in obj:
from_user = User.de_json(obj['from'])
chat = Chat.de_json(obj['chat'])
date = obj['date']
content_type = None
opts = {}
@ -409,8 +444,8 @@ class UserProfilePhotos(JsonDeserializable):
self.total_count = total_count
self.photos = photos
class File(JsonDeserializable):
class File(JsonDeserializable):
@classmethod
def de_json(cls, json_type):
obj = cls.check_json(json_type)

View File

@ -203,7 +203,6 @@ def orify(e, changed_callback):
e.set = lambda: or_set(e)
e.clear = lambda: or_clear(e)
def OrEvent(*events):
or_event = threading.Event()
def changed():
@ -212,7 +211,14 @@ def OrEvent(*events):
or_event.set()
else:
or_event.clear()
def busy_wait():
while not or_event.is_set():
or_event._wait(3)
for e in events:
orify(e, changed)
or_event._wait = or_event.wait
or_event.wait = busy_wait
changed()
return or_event