This commit is contained in:
Jannes Höke 2015-11-21 16:04:06 +01:00
parent d38add1a1c
commit 618df51811
3 changed files with 24 additions and 29 deletions

View file

@ -50,9 +50,9 @@ from .bot import Bot
from .broadcaster import Broadcaster from .broadcaster import Broadcaster
from .boteventhandler import BotEventHandler from .boteventhandler import BotEventHandler
__all__ = ['Bot', 'BotEventHandler', 'Broadcaster', 'Emoji', 'TelegramError', 'InputFile', 'ReplyMarkup', __all__ = ['Bot', 'BotEventHandler', 'Broadcaster', 'Emoji', 'TelegramError',
'ForceReply', 'ReplyKeyboardHide', 'ReplyKeyboardMarkup', 'InputFile', 'ReplyMarkup', 'ForceReply', 'ReplyKeyboardHide',
'UserProfilePhotos', 'ChatAction', 'Location', 'Contact', 'ReplyKeyboardMarkup', 'UserProfilePhotos', 'ChatAction',
'Video', 'Sticker', 'Document', 'File', 'Audio', 'PhotoSize', 'Location', 'Contact', 'Video', 'Sticker', 'Document', 'File',
'GroupChat', 'Update', 'ParseMode', 'Message', 'User', 'Audio', 'PhotoSize', 'GroupChat', 'Update', 'ParseMode', 'Message',
'TelegramObject', 'NullHandler', 'Voice'] 'User', 'TelegramObject', 'NullHandler', 'Voice']

View file

@ -1,7 +1,8 @@
#!/usr/bin/env python #!/usr/bin/env python
""" """
This module contains the class BotEventHandler, which tries to make creating This module contains the class BotEventHandler, which tries to make creating
Telegram Bots intuitive! Telegram Bots intuitive!
""" """
import logging import logging
@ -19,11 +20,6 @@ try:
except ImportError: except ImportError:
from queue import Queue from queue import Queue
try:
import BaseHTTPServer
except ImportError:
import http.server as BaseHTTPServer
H = NullHandler() H = NullHandler()
logging.getLogger(__name__).addHandler(H) logging.getLogger(__name__).addHandler(H)
@ -35,9 +31,9 @@ class BotEventHandler:
can interact with the bot, for example on the command line. It supports can interact with the bot, for example on the command line. It supports
Handlers for different kinds of data: Updates from Telegram, basic text Handlers for different kinds of data: Updates from Telegram, basic text
commands and even arbitrary types. commands and even arbitrary types.
Polling as well as webhook are supported. Polling as well as webhook are supported.
Attributes: Attributes:
Args: Args:
@ -211,4 +207,3 @@ class BotEventHandler:
self.broadcaster.stop() self.broadcaster.stop()
while broadcaster.running_async > 0: while broadcaster.running_async > 0:
sleep(1) sleep(1)

View file

@ -118,7 +118,7 @@ class Broadcaster:
break break
self.processUpdate(update) self.processUpdate(update)
# Broadcast any errors # Broadcast any errors
except TelegramError as te: except TelegramError as te:
self.broadcastError(update, te) self.broadcastError(update, te)
@ -420,16 +420,16 @@ class Broadcaster:
def broadcastTelegramCommand(self, update): def broadcastTelegramCommand(self, update):
""" """
Broadcasts an update that contains a command. Broadcasts an update that contains a command.
Args: Args:
command (str): The command keyword command (str): The command keyword
update (telegram.Update): The Telegram update that contains the update (telegram.Update): The Telegram update that contains the
command command
""" """
command = update.message.text.split(' ')[0][1:].split('@')[0] command = update.message.text.split(' ')[0][1:].split('@')[0]
if command in self.telegram_command_handlers: if command in self.telegram_command_handlers:
self.broadcastTo(self.telegram_command_handlers[command], update) self.broadcastTo(self.telegram_command_handlers[command], update)
else: else:
@ -454,17 +454,17 @@ class Broadcaster:
matching_handlers.append(handler) matching_handlers.append(handler)
self.broadcastTo(matching_handlers, update) self.broadcastTo(matching_handlers, update)
def broadcastStringCommand(self, update): def broadcastStringCommand(self, update):
""" """
Broadcasts a string-update that contains a command. Broadcasts a string-update that contains a command.
Args: Args:
update (str): The string input update (str): The string input
""" """
command = update.split(' ')[0][1:] command = update.split(' ')[0][1:]
if command in self.string_command_handlers: if command in self.string_command_handlers:
self.broadcastTo(self.string_command_handlers[command], update) self.broadcastTo(self.string_command_handlers[command], update)
else: else:
@ -493,7 +493,7 @@ class Broadcaster:
def broadcastType(self, update): def broadcastType(self, update):
""" """
Broadcasts an update of any type. Broadcasts an update of any type.
Args: Args:
update (any): The update update (any): The update
""" """
@ -504,16 +504,16 @@ class Broadcaster:
else: else:
self.broadcastError(update, TelegramError( self.broadcastError(update, TelegramError(
"Received update of unknown type %s" % type(update))) "Received update of unknown type %s" % type(update)))
def broadcastTelegramMessage(self, update): def broadcastTelegramMessage(self, update):
""" """
Broadcasts an update that contains a regular message. Broadcasts an update that contains a regular message.
Args: Args:
update (telegram.Update): The Telegram update that contains the update (telegram.Update): The Telegram update that contains the
message. message.
""" """
self.broadcastTo(self.telegram_message_handlers, update) self.broadcastTo(self.telegram_message_handlers, update)
def broadcastError(self, update, error): def broadcastError(self, update, error):