Skip to content

Commit 38341eb

Browse files
author
Pietro Albini
committed
Fix GH-41 -- Commands with newlines in args not recognized
Before this commit, if you try to send a command with a newline in the args, botogram fails to recognize the message as a command. This commit fixes this, and also considers `\n` and `\t` as spaces while splitting the message in search for arguments.
1 parent 75a84db commit 38341eb

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

botogram/hooks.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,16 @@ def _after_init(self, args):
169169

170170
def _call(self, bot, update):
171171
message = update.message
172+
text = message.text.replace("\n", " ").replace("\t", " ")
172173

173174
# Must be the correct command for the correct bot
174-
match = self._regex.match(message.text)
175+
match = self._regex.match(text)
175176
if not match:
176177
return
177178
if match.group(1) and match.group(1) != "@"+bot.itself.username:
178179
return
179180

180-
args = message.text.split(" ")[1:]
181+
args = text.split(" ")[1:]
181182
bot._call(self.func, self.component_id, chat=message.chat,
182183
message=message, args=args)
183184
return True

docs/changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Bug fixes
5353
40`_)
5454
* Fix inability to send messages to channels from a running bot (`issue 35`_)
5555
* Fix inability to download stickers (`issue 36`_)
56+
* Fix commands with newlines in the arguments not recognized as such (`issue
57+
41`_)
5658

5759
Deprecated features
5860
-------------------
@@ -66,6 +68,7 @@ Deprecated features will be removed in botogram 1.0!
6668
.. _issue 35: https://github.com/pietroalbini/botogram/issues/35
6769
.. _issue 36: https://github.com/pietroalbini/botogram/issues/36
6870
.. _issue 40: https://github.com/pietroalbini/botogram/issues/40
71+
.. _issue 41: https://github.com/pietroalbini/botogram/issues/41
6972

7073
.. _changelog-0.1.2:
7174

tests/test_components.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,15 @@ def sample2(chat, message, args):
232232
sample2_processed = True
233233

234234
assert chat.id == -1
235-
assert message.text == "/sample2 a b c"
235+
assert message.text == "/sample2 a b c\nd\te"
236236
assert message.chat == chat
237-
assert args == ["a", "b", "c"]
237+
assert args == ["a", "b", "c", "d", "e"]
238238

239239
def sample3(chat, message, args):
240240
nonlocal sample3_processed
241241
sample3_processed = True
242242

243-
assert message.text == "/sample3@test_bot a b c"
243+
assert message.text == "/sample3@test_bot a b c\nd\te"
244244

245245

246246
comp = botogram.Component("test")
@@ -251,7 +251,7 @@ def sample3(chat, message, args):
251251
bot.use(comp)
252252

253253
for cmd in "sample1@another_bot", "sample2", "sample3@test_bot":
254-
sample_update.message.text = "/%s a b c" % cmd
254+
sample_update.message.text = "/%s a b c\nd\te" % cmd
255255
bot.process(sample_update)
256256

257257
assert sample1_processed == False

0 commit comments

Comments
 (0)