improved code quality

This commit is contained in:
Jannes Höke 2015-11-06 00:24:01 +01:00
parent 404fdc2dd7
commit c19a84ac05
3 changed files with 26 additions and 20 deletions

View file

@ -37,6 +37,7 @@ def anyMessageHandler(bot, update):
def unknownCommandHandler(bot, update): def unknownCommandHandler(bot, update):
bot.sendMessage(update.message.chat_id, text='Command not recognized!') bot.sendMessage(update.message.chat_id, text='Command not recognized!')
@run_async @run_async
def messageHandler(bot, update): def messageHandler(bot, update):
""" """
@ -63,7 +64,7 @@ def unknownCLICommandHandler(bot, update):
def main(): def main():
# Create the EventHandler and pass it your bot's token # Create the EventHandler and pass it your bot's token
eh = BotEventHandler("TOKEN") eh = BotEventHandler("148447715:AAGG70cC6s_kwrji2pLsaMeEzKGe0llA1hY")
# on different commands - answer in Telegram # on different commands - answer in Telegram
eh.broadcaster.addTelegramCommandHandler("start", startCommandHandler) eh.broadcaster.addTelegramCommandHandler("start", startCommandHandler)

View file

@ -9,6 +9,7 @@ import sys
from threading import Thread from threading import Thread
from telegram import (Bot, TelegramError, TelegramObject, Broadcaster) from telegram import (Bot, TelegramError, TelegramObject, Broadcaster)
from time import sleep from time import sleep
from functools import wraps
# Adjust for differences in Python versions # Adjust for differences in Python versions
if sys.version_info.major is 2: if sys.version_info.major is 2:
@ -16,18 +17,27 @@ if sys.version_info.major is 2:
elif sys.version_info.major is 3: elif sys.version_info.major is 3:
from queue import Queue from queue import Queue
def run_async(func): def run_async(func):
from threading import Thread """
from functools import wraps Function decorator that will run the function in a new thread.
Args:
func (function): The function to run in the thread.
Returns:
function:
"""
@wraps(func) @wraps(func)
def async_func(*args, **kwargs): def async_func(*args, **kwargs):
func_hl = Thread(target=func, args=args, kwargs=kwargs) thread = Thread(target=func, args=args, kwargs=kwargs)
func_hl.start() thread.start()
return func_hl return thread
return async_func return async_func
class BotEventHandler(TelegramObject): class BotEventHandler(TelegramObject):
""" """
This class provides a frontend to telegram.Bot to the programmer, so they This class provides a frontend to telegram.Bot to the programmer, so they
@ -40,9 +50,6 @@ class BotEventHandler(TelegramObject):
Args: Args:
token (str): The bots token given by the @BotFather token (str): The bots token given by the @BotFather
**kwargs: Arbitrary keyword arguments.
Keyword Args:
base_url (Optional[str]): base_url (Optional[str]):
""" """
@ -57,9 +64,6 @@ class BotEventHandler(TelegramObject):
Starts polling updates from Telegram. Starts polling updates from Telegram.
Args: Args:
**kwargs: Arbitrary keyword arguments.
Keyword Args:
poll_interval (Optional[float]): Time to wait between polling poll_interval (Optional[float]): Time to wait between polling
updates from Telegram in seconds. Default is 1.0 updates from Telegram in seconds. Default is 1.0

View file

@ -6,6 +6,7 @@ This module contains the Broadcaster class.
from telegram import (TelegramError, TelegramObject, Update) from telegram import (TelegramError, TelegramObject, Update)
class Broadcaster(TelegramObject): class Broadcaster(TelegramObject):
""" """
This class broadcasts all kinds of updates to its registered handlers. This class broadcasts all kinds of updates to its registered handlers.
@ -15,7 +16,7 @@ class Broadcaster(TelegramObject):
Args: Args:
bot (telegram.Bot): The bot object that should be passed to the handlers bot (telegram.Bot): The bot object that should be passed to the handlers
update_queue (queue.Queue): The synchronized queue that will contain the update_queue (queue.Queue): The synchronized queue that will contain the
updates updates.
""" """
def __init__(self, bot, update_queue): def __init__(self, bot, update_queue):
self.bot = bot self.bot = bot
@ -64,8 +65,8 @@ class Broadcaster(TelegramObject):
if matcher.math(update.message.text) is True. if matcher.math(update.message.text) is True.
Args: Args:
matcher (str): A compiled regex object that matches on messages that matcher (__Regex): A compiled regex object that matches on messages
handler should be listening to that handler should be listening to
handler (function): A function that takes (Bot, Update) as handler (function): A function that takes (Bot, Update) as
arguments. arguments.
""" """
@ -96,7 +97,7 @@ class Broadcaster(TelegramObject):
if matcher.math(string) is True. if matcher.math(string) is True.
Args: Args:
matcher (str): A compiled regex object that matches on the string matcher (__Regex): A compiled regex object that matches on the string
input that handler should be listening to input that handler should be listening to
handler (function): A function that takes (Bot, Update) as handler (function): A function that takes (Bot, Update) as
arguments. arguments.