mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-12-23 06:50:29 +01:00
improved code quality
This commit is contained in:
parent
404fdc2dd7
commit
c19a84ac05
3 changed files with 26 additions and 20 deletions
|
@ -37,6 +37,7 @@ def anyMessageHandler(bot, update):
|
|||
def unknownCommandHandler(bot, update):
|
||||
bot.sendMessage(update.message.chat_id, text='Command not recognized!')
|
||||
|
||||
|
||||
@run_async
|
||||
def messageHandler(bot, update):
|
||||
"""
|
||||
|
@ -63,7 +64,7 @@ def unknownCLICommandHandler(bot, update):
|
|||
|
||||
def main():
|
||||
# Create the EventHandler and pass it your bot's token
|
||||
eh = BotEventHandler("TOKEN")
|
||||
eh = BotEventHandler("148447715:AAGG70cC6s_kwrji2pLsaMeEzKGe0llA1hY")
|
||||
|
||||
# on different commands - answer in Telegram
|
||||
eh.broadcaster.addTelegramCommandHandler("start", startCommandHandler)
|
||||
|
|
|
@ -9,24 +9,34 @@ import sys
|
|||
from threading import Thread
|
||||
from telegram import (Bot, TelegramError, TelegramObject, Broadcaster)
|
||||
from time import sleep
|
||||
from functools import wraps
|
||||
|
||||
# Adjust for differences in Python versions
|
||||
if sys.version_info.major is 2:
|
||||
from Queue import Queue
|
||||
elif sys.version_info.major is 3:
|
||||
from queue import Queue
|
||||
|
||||
|
||||
|
||||
def run_async(func):
|
||||
from threading import Thread
|
||||
from functools import wraps
|
||||
"""
|
||||
Function decorator that will run the function in a new thread.
|
||||
|
||||
@wraps(func)
|
||||
def async_func(*args, **kwargs):
|
||||
func_hl = Thread(target=func, args=args, kwargs=kwargs)
|
||||
func_hl.start()
|
||||
return func_hl
|
||||
Args:
|
||||
func (function): The function to run in the thread.
|
||||
|
||||
Returns:
|
||||
function:
|
||||
"""
|
||||
|
||||
@wraps(func)
|
||||
def async_func(*args, **kwargs):
|
||||
thread = Thread(target=func, args=args, kwargs=kwargs)
|
||||
thread.start()
|
||||
return thread
|
||||
|
||||
return async_func
|
||||
|
||||
return async_func
|
||||
|
||||
class BotEventHandler(TelegramObject):
|
||||
"""
|
||||
|
@ -40,9 +50,6 @@ class BotEventHandler(TelegramObject):
|
|||
|
||||
Args:
|
||||
token (str): The bots token given by the @BotFather
|
||||
**kwargs: Arbitrary keyword arguments.
|
||||
|
||||
Keyword Args:
|
||||
base_url (Optional[str]):
|
||||
"""
|
||||
|
||||
|
@ -57,9 +64,6 @@ class BotEventHandler(TelegramObject):
|
|||
Starts polling updates from Telegram.
|
||||
|
||||
Args:
|
||||
**kwargs: Arbitrary keyword arguments.
|
||||
|
||||
Keyword Args:
|
||||
poll_interval (Optional[float]): Time to wait between polling
|
||||
updates from Telegram in seconds. Default is 1.0
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ This module contains the Broadcaster class.
|
|||
|
||||
from telegram import (TelegramError, TelegramObject, Update)
|
||||
|
||||
|
||||
class Broadcaster(TelegramObject):
|
||||
"""
|
||||
This class broadcasts all kinds of updates to its registered handlers.
|
||||
|
@ -15,7 +16,7 @@ class Broadcaster(TelegramObject):
|
|||
Args:
|
||||
bot (telegram.Bot): The bot object that should be passed to the handlers
|
||||
update_queue (queue.Queue): The synchronized queue that will contain the
|
||||
updates
|
||||
updates.
|
||||
"""
|
||||
def __init__(self, bot, update_queue):
|
||||
self.bot = bot
|
||||
|
@ -64,8 +65,8 @@ class Broadcaster(TelegramObject):
|
|||
if matcher.math(update.message.text) is True.
|
||||
|
||||
Args:
|
||||
matcher (str): A compiled regex object that matches on messages that
|
||||
handler should be listening to
|
||||
matcher (__Regex): A compiled regex object that matches on messages
|
||||
that handler should be listening to
|
||||
handler (function): A function that takes (Bot, Update) as
|
||||
arguments.
|
||||
"""
|
||||
|
@ -96,7 +97,7 @@ class Broadcaster(TelegramObject):
|
|||
if matcher.math(string) is True.
|
||||
|
||||
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
|
||||
handler (function): A function that takes (Bot, Update) as
|
||||
arguments.
|
||||
|
|
Loading…
Reference in a new issue