Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1782d0d

Browse files
committed
refactor Broadcaster to Dispatcher
1 parent 88fbf3b commit 1782d0d

File tree

6 files changed

+105
-105
lines changed

6 files changed

+105
-105
lines changed

examples/eventhandler_bot.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
This Bot uses the BotEventHandler class to handle the bot.
55
66
First, a few handler functions are defined. Then, those functions are passed to
7-
the Broadcaster and registered at their respective places.
7+
the Dispatcher and registered at their respective places.
88
Then, the bot is started and the CLI-Loop is entered, where all text inputs are
99
inserted into the update queue for the bot to handle.
1010
@@ -100,34 +100,34 @@ def main():
100100
# Create the EventHandler and pass it your bot's token.
101101
eh = BotEventHandler("TOKEN", workers=2)
102102

103-
# Get the broadcaster to register handlers
104-
bc = eh.broadcaster
103+
# Get the dispatcher to register handlers
104+
dp = eh.dispatcher
105105

106106
# on different commands - answer in Telegram
107-
bc.addTelegramCommandHandler("start", startCommandHandler)
108-
bc.addTelegramCommandHandler("help", helpCommandHandler)
107+
dp.addTelegramCommandHandler("start", startCommandHandler)
108+
dp.addTelegramCommandHandler("help", helpCommandHandler)
109109

110110
# on regex match - print all messages to stdout
111-
bc.addTelegramRegexHandler('.*', anyMessageHandler)
112-
111+
dp.addTelegramRegexHandler('.*', anyMessageHandler)
112+
113113
# on CLI commands - type "/reply text" in terminal to reply to the last
114114
# active chat
115-
bc.addStringCommandHandler('reply', CLIReplyCommandHandler)
115+
dp.addStringCommandHandler('reply', CLIReplyCommandHandler)
116116

117117
# on unknown commands - answer on Telegram
118-
bc.addUnknownTelegramCommandHandler(unknownCommandHandler)
118+
dp.addUnknownTelegramCommandHandler(unknownCommandHandler)
119119

120120
# on unknown CLI commands - notify the user
121-
bc.addUnknownStringCommandHandler(unknownCLICommandHandler)
121+
dp.addUnknownStringCommandHandler(unknownCLICommandHandler)
122122

123123
# on any CLI message that is not a command - resend it as a command
124-
bc.addStringRegexHandler('[^/].*', anyCLIHandler)
124+
dp.addStringRegexHandler('[^/].*', anyCLIHandler)
125125

126126
# on noncommand i.e message - echo the message on Telegram
127-
bc.addTelegramMessageHandler(messageHandler)
127+
dp.addTelegramMessageHandler(messageHandler)
128128

129129
# on error - print error to stdout
130-
bc.addErrorHandler(errorHandler)
130+
dp.addErrorHandler(errorHandler)
131131

132132
# Start the Bot and store the update Queue,
133133
# so we can insert updates ourselves
@@ -137,7 +137,7 @@ def main():
137137
update_queue = eh.start_webhook('example.com', 443, 'cert.pem', 'key.key',
138138
listen='0.0.0.0')
139139
'''
140-
140+
141141
# Start CLI-Loop
142142
while True:
143143
try:

examples/eventhandler_simplebot.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
This Bot uses the BotEventHandler class to handle the bot.
55
66
First, a few handler functions are defined. Then, those functions are passed to
7-
the Broadcaster and registered at their respective places.
7+
the Dispatcher and registered at their respective places.
88
Then, the bot is started and the CLI-Loop is entered.
99
1010
Usage:
@@ -48,18 +48,18 @@ def main():
4848
# Create the EventHandler and pass it your bot's token.
4949
eh = BotEventHandler("TOKEN")
5050

51-
# Get the broadcaster to register handlers
52-
bc = eh.broadcaster
51+
# Get the dispatcher to register handlers
52+
dp = eh.dispatcher
5353

5454
# on different commands - answer in Telegram
55-
bc.addTelegramCommandHandler("start", start)
56-
bc.addTelegramCommandHandler("help", help)
55+
dp.addTelegramCommandHandler("start", start)
56+
dp.addTelegramCommandHandler("help", help)
5757

5858
# on noncommand i.e message - echo the message on Telegram
59-
bc.addTelegramMessageHandler(echo)
59+
dp.addTelegramMessageHandler(echo)
6060

6161
# on error - print error to stdout
62-
bc.addErrorHandler(error)
62+
dp.addErrorHandler(error)
6363

6464
# Start the Bot
6565
eh.start_polling()

telegram/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@
4747
from .message import Message
4848
from .update import Update
4949
from .bot import Bot
50-
from .broadcaster import Broadcaster
50+
from .broadcaster import Dispatcher
5151
from .boteventhandler import BotEventHandler
5252

53-
__all__ = ['Bot', 'BotEventHandler', 'Broadcaster', 'Emoji', 'TelegramError',
53+
__all__ = ['Bot', 'BotEventHandler', 'Dispatcher', 'Emoji', 'TelegramError',
5454
'InputFile', 'ReplyMarkup', 'ForceReply', 'ReplyKeyboardHide',
5555
'ReplyKeyboardMarkup', 'UserProfilePhotos', 'ChatAction',
5656
'Location', 'Contact', 'Video', 'Sticker', 'Document', 'File',

telegram/boteventhandler.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import subprocess
1414

15-
from telegram import (Bot, TelegramError, broadcaster, Broadcaster,
15+
from telegram import (Bot, TelegramError, broadcaster, Dispatcher,
1616
NullHandler)
1717
from telegram.utils.webhookhandler import (WebhookServer, WebhookHandler)
1818

@@ -49,8 +49,8 @@ def __init__(self, token, base_url=None, workers=4):
4949

5050
self.bot = Bot(token, base_url)
5151
self.update_queue = Queue()
52-
self.broadcaster = Broadcaster(self.bot, self.update_queue,
53-
workers=workers)
52+
self.dispatcher = Dispatcher(self.bot, self.update_queue,
53+
workers=workers)
5454
self.last_update_id = 0
5555
self.logger = logging.getLogger(__name__)
5656
self.running = False
@@ -71,8 +71,8 @@ def start_polling(self, poll_interval=1.0, timeout=10, network_delay=2):
7171
"""
7272

7373
# Create Thread objects
74-
broadcaster_thread = Thread(target=self.broadcaster.start,
75-
name="broadcaster")
74+
dispatcher_thread = Thread(target=self.dispatcher.start,
75+
name="dispatcher")
7676
event_handler_thread = Thread(target=self._start_polling,
7777
name="eventhandler",
7878
args=(poll_interval, timeout,
@@ -81,7 +81,7 @@ def start_polling(self, poll_interval=1.0, timeout=10, network_delay=2):
8181
self.running = True
8282

8383
# Start threads
84-
broadcaster_thread.start()
84+
dispatcher_thread.start()
8585
event_handler_thread.start()
8686

8787
# Return the update queue so the main thread can insert updates
@@ -103,16 +103,16 @@ def start_webhook(self, host, port, cert, key, listen='0.0.0.0'):
103103
"""
104104

105105
# Create Thread objects
106-
broadcaster_thread = Thread(target=self.broadcaster.start,
107-
name="broadcaster")
106+
dispatcher_thread = Thread(target=self.dispatcher.start,
107+
name="dispatcher")
108108
event_handler_thread = Thread(target=self._start_webhook,
109109
name="eventhandler",
110110
args=(host, port, cert, key, listen))
111111

112112
self.running = True
113113

114114
# Start threads
115-
broadcaster_thread.start()
115+
dispatcher_thread.start()
116116
event_handler_thread.start()
117117

118118
# Return the update queue so the main thread can insert updates
@@ -122,7 +122,7 @@ def _start_polling(self, poll_interval, timeout, network_delay):
122122
"""
123123
Thread target of thread 'eventhandler'. Runs in background, pulls
124124
updates from Telegram and inserts them in the update queue of the
125-
Broadcaster.
125+
Dispatcher.
126126
"""
127127

128128
current_interval = poll_interval
@@ -149,7 +149,7 @@ def _start_polling(self, poll_interval, timeout, network_delay):
149149

150150
sleep(current_interval)
151151
except TelegramError as te:
152-
# Put the error into the update queue and let the Broadcaster
152+
# Put the error into the update queue and let the Dispatcher
153153
# broadcast it
154154
self.update_queue.put(te)
155155
sleep(current_interval)
@@ -200,9 +200,9 @@ def _start_webhook(self, host, port, cert, key, listen):
200200

201201
def stop(self):
202202
"""
203-
Stops the polling thread and the broadcaster
203+
Stops the polling thread and the dispatcher
204204
"""
205-
self.logger.info('Stopping Event Handler and Broadcaster...')
205+
self.logger.info('Stopping Event Handler and Dispatcher...')
206206
self.running = False
207207

208208
if self.httpd:
@@ -212,9 +212,9 @@ def stop(self):
212212
self.httpd.shutdown()
213213
self.httpd = None
214214

215-
self.logger.debug("Requesting Broadcaster to stop...")
216-
self.broadcaster.stop()
215+
self.logger.debug("Requesting Dispatcher to stop...")
216+
self.dispatcher.stop()
217217
while broadcaster.running_async > 0:
218218
sleep(1)
219219

220-
self.logger.debug("Broadcaster stopped.")
220+
self.logger.debug("Dispatcher stopped.")

0 commit comments

Comments
 (0)