Skip to content

Commit 78094b7

Browse files
Eldinniejh0ker
authored andcommitted
Fix commandhandler (python-telegram-bot#515)
* stripping token of whitespaces before starting bot * Line feed * CommandHandler checks if message is for this bot * CommandHandler checks if message is for this bot - Added tests * Fixed tests in test_conversationHandler to work with new commandhandler structure * type in conversationhandler test
1 parent 264b9bd commit 78094b7

3 files changed

Lines changed: 36 additions & 27 deletions

File tree

telegram/ext/commandhandler.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,14 @@ def check_update(self, update):
8383
and (update.message or update.edited_message and self.allow_edited)):
8484
message = update.message or update.edited_message
8585

86-
return (message.text and message.text.startswith('/')
87-
and message.text[1:].split(' ')[0].split('@')[0] == self.command)
86+
if message.text:
87+
command = message.text[1:].split(' ')[0].split('@')
88+
command.append(
89+
update.message.bot.username) # in case the command was send without a username
90+
return (message.text.startswith('/') and command[0] == self.command
91+
and command[1] == update.message.bot.username)
92+
else:
93+
return False
8894

8995
else:
9096
return False

tests/test_conversationhandler.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ def setUp(self):
8080
self.fallbacks = [CommandHandler('eat', self.start)]
8181

8282
def _setup_updater(self, *args, **kwargs):
83-
bot = MockBot(*args, **kwargs)
84-
self.updater = Updater(workers=2, bot=bot)
83+
self.bot = MockBot(*args, **kwargs)
84+
self.updater = Updater(workers=2, bot=self.bot)
8585

8686
def tearDown(self):
8787
if self.updater is not None:
@@ -137,32 +137,32 @@ def test_addConversationHandler(self):
137137
queue = self.updater.start_polling(0.01)
138138

139139
# User one, starts the state machine.
140-
message = Message(0, user, None, None, text="/start")
140+
message = Message(0, user, None, None, text="/start", bot=self.bot)
141141
queue.put(Update(update_id=0, message=message))
142142
sleep(.1)
143143
self.assertTrue(self.current_state[user.id] == self.THIRSTY)
144144

145145
# The user is thirsty and wants to brew coffee.
146-
message = Message(0, user, None, None, text="/brew")
146+
message = Message(0, user, None, None, text="/brew", bot=self.bot)
147147
queue.put(Update(update_id=0, message=message))
148148
sleep(.1)
149149
self.assertTrue(self.current_state[user.id] == self.BREWING)
150150

151151
# Lets see if an invalid command makes sure, no state is changed.
152-
message = Message(0, user, None, None, text="/nothing")
152+
message = Message(0, user, None, None, text="/nothing", bot=self.bot)
153153
queue.put(Update(update_id=0, message=message))
154154
sleep(.1)
155155
self.assertTrue(self.current_state[user.id] == self.BREWING)
156156

157157
# Lets see if the state machine still works by pouring coffee.
158-
message = Message(0, user, None, None, text="/pourCoffee")
158+
message = Message(0, user, None, None, text="/pourCoffee", bot=self.bot)
159159
queue.put(Update(update_id=0, message=message))
160160
sleep(.1)
161161
self.assertTrue(self.current_state[user.id] == self.DRINKING)
162162

163163
# Let's now verify that for another user, who did not start yet,
164164
# the state has not been changed.
165-
message = Message(0, second_user, None, None, text="/brew")
165+
message = Message(0, second_user, None, None, text="/brew", bot=self.bot)
166166
queue.put(Update(update_id=0, message=message))
167167
sleep(.1)
168168
self.assertRaises(KeyError, self._get_state, user_id=second_user.id)
@@ -197,13 +197,13 @@ def test_endOnFirstMessageAsync(self):
197197

198198
# User starts the state machine with an async function that immediately ends the
199199
# conversation. Async results are resolved when the users state is queried next time.
200-
message = Message(0, user, None, None, text="/start")
200+
message = Message(0, user, None, None, text="/start", bot=self.bot)
201201
queue.put(Update(update_id=0, message=message))
202202
sleep(.1)
203203
# Assert that the Promise has been accepted as the new state
204204
self.assertEquals(len(handler.conversations), 1)
205205

206-
message = Message(0, user, None, None, text="resolve promise pls")
206+
message = Message(0, user, None, None, text="resolve promise pls", bot=self.bot)
207207
queue.put(Update(update_id=0, message=message))
208208
sleep(.1)
209209
# Assert that the Promise has been resolved and the conversation ended.

tests/test_updater.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -231,30 +231,32 @@ def test_addRemoveTelegramRegexHandler(self):
231231
self.assertTrue(None is self.received_message)
232232

233233
def test_addRemoveTelegramCommandHandler(self):
234-
self._setup_updater('/test')
234+
self._setup_updater('', messages=0)
235235
d = self.updater.dispatcher
236236
handler = CommandHandler('test', self.telegramHandlerTest)
237237
self.updater.dispatcher.add_handler(handler)
238-
self.updater.start_polling(0.01)
238+
user = User(first_name="singelton", id=404)
239+
bot = self.updater.bot
240+
queue = self.updater.start_polling(0.01)
241+
242+
# regular use
243+
message = Message(0, user, None, None, text="/test", bot=bot)
244+
queue.put(Update(update_id=0, message=message))
239245
sleep(.1)
240246
self.assertEqual(self.received_message, '/test')
241247

242-
# Remove handler
243-
d.remove_handler(handler)
244-
self.reset()
245-
246-
self.updater.bot.send_messages = 1
248+
# assigned use
249+
message = Message(0, user, None, None, text="/test@MockBot", bot=bot)
250+
queue.put(Update(update_id=0, message=message))
247251
sleep(.1)
248-
self.assertTrue(None is self.received_message)
252+
self.assertEqual(self.received_message, '/test@MockBot')
249253

250-
def test_editedCommandHandler(self):
251-
self._setup_updater('/test', edited=True)
252-
d = self.updater.dispatcher
253-
handler = CommandHandler('test', self.telegramHandlerEditedTest, allow_edited=True)
254-
d.addHandler(handler)
255-
self.updater.start_polling(0.01)
254+
# directed at other bot
255+
self.reset()
256+
message = Message(0, user, None, None, text="/test@OtherBot", bot=bot)
257+
queue.put(Update(update_id=0, message=message))
256258
sleep(.1)
257-
self.assertEqual(self.received_message, '/test')
259+
self.assertTrue(None is self.received_message)
258260

259261
# Remove handler
260262
d.removeHandler(handler)
@@ -788,9 +790,10 @@ def __init__(self,
788790
self.bootstrap_attempts = 0
789791
self.bootstrap_err = bootstrap_err
790792
self.edited = edited
793+
self.username = "MockBot"
791794

792795
def mockUpdate(self, text):
793-
message = Message(0, User(0, 'Testuser'), None, Chat(0, Chat.GROUP))
796+
message = Message(0, User(0, 'Testuser'), None, Chat(0, Chat.GROUP), bot=self)
794797
message.text = text
795798
update = Update(0)
796799

0 commit comments

Comments
 (0)