Skip to content

Commit 38a45f0

Browse files
committed
support command in edited messages
1 parent fc074c5 commit 38a45f0

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

telebot/api.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,15 +681,15 @@ proc handleUpdate*(b: TeleBot, update: Update) {.async.} =
681681
elif update.hasCommand(b.username):
682682
if b.commandCallbacks.hasKey(command):
683683
var cmd: Command
684-
cmd.message = update.message.get()
684+
cmd.message = message
685685
cmd.params = params
686686

687687
for cb in b.commandCallbacks[command]:
688688
await cb(b, cmd)
689689
elif b.catchallCommandCallback != nil:
690690
var cmd: CatchallCommand
691691
cmd.command = command
692-
cmd.message = update.message.get()
692+
cmd.message = message
693693
cmd.params = params
694694
await b.catchallCommandCallback(b, cmd)
695695
else:

telebot/utils.nim

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,39 @@ macro END_POINT*(s: string) =
1212
result = parseStmt("const endpoint = \"" & API_URL & s.strVal & "\"")
1313

1414
template hasCommand*(update: Update, username: string): bool =
15-
var result = false
15+
var
16+
result = false
17+
hasMessage = false
1618
when not declaredInScope(command):
1719
var
1820
command {.inject.} = ""
1921
params {.inject.} = ""
22+
message {.inject.}: Message
2023
if update.message.isSome:
21-
let message = update.message.get()
22-
if message.entities.isSome:
24+
hasMessage = true
25+
message = update.message.get()
26+
elif update.editedMessage.isSome:
27+
hasMessage = true
28+
message = update.editedMessage.get()
29+
else:
30+
result = false
31+
32+
if hasMessage and message.entities.isSome:
33+
let
34+
entities = message.entities.get()
35+
messageText = message.text.get()
36+
if entities[0].kind == "bot_command":
2337
let
24-
entities = message.entities.get()
25-
messageText = message.text.get()
26-
if entities[0].kind == "bot_command":
27-
let
28-
offset = entities[0].offset
29-
length = entities[0].length
30-
command = messageText[(offset + 1)..<(offset + length)].strip()
31-
params = messageText[(offset + length)..^1].strip()
32-
result = true
33-
if '@' in command:
34-
var parts = command.split('@')
35-
command = parts[0]
36-
if (parts.len == 2 and parts[1].toLowerAscii != username):
37-
result = false
38+
offset = entities[0].offset
39+
length = entities[0].length
40+
command = messageText[(offset + 1)..<(offset + length)].strip()
41+
params = messageText[(offset + length)..^1].strip()
42+
result = true
43+
if '@' in command:
44+
var parts = command.split('@')
45+
command = parts[0]
46+
if (parts.len == 2 and parts[1].toLowerAscii != username):
47+
result = false
3848
result
3949

4050
proc isSet*(value: any): bool {.inline.} =

0 commit comments

Comments
 (0)