Skip to content

Commit 4ea8784

Browse files
committed
better command handling [closes #44, fixes #39]
1 parent a93aefd commit 4ea8784

File tree

4 files changed

+45
-40
lines changed

4 files changed

+45
-40
lines changed

telebot.nimble

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version = "0.6.8"
1+
version = "0.6.9"
22
author = "Huy Doan"
33
description = "Async Telegram Bot API Client"
44
license = "MIT"

telebot/api.nim

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -662,24 +662,20 @@ proc handleUpdate*(b: TeleBot, update: Update) {.async.} =
662662
if update.inlineQuery.isSome:
663663
for cb in b.inlineQueryCallbacks:
664664
await cb(b, update.inlineQuery.get)
665-
elif update.hasCommand():
666-
var
667-
message = update.message.get
668-
var userCommands = getCommands(message.entities.get, message.text.get)
669-
for command in userCommands.keys():
670-
if b.commandCallbacks.hasKey(command):
671-
var cmd: Command
672-
cmd.message = update.message.get()
673-
cmd.params = userCommands[command]
674-
675-
for cb in b.commandCallbacks[command]:
676-
await cb(b, cmd)
677-
elif b.catchallCommandCallback != nil:
678-
var cmd: CatchallCommand
679-
cmd.command = command
680-
cmd.message = update.message.get()
681-
cmd.params = userCommands[command]
682-
await b.catchallCommandCallback(b, cmd)
665+
elif update.hasCommand(b.username):
666+
if b.commandCallbacks.hasKey(command):
667+
var cmd: Command
668+
cmd.message = update.message.get()
669+
cmd.params = params
670+
671+
for cb in b.commandCallbacks[command]:
672+
await cb(b, cmd)
673+
elif b.catchallCommandCallback != nil:
674+
var cmd: CatchallCommand
675+
cmd.command = command
676+
cmd.message = update.message.get()
677+
cmd.params = params
678+
await b.catchallCommandCallback(b, cmd)
683679
else:
684680
for cb in b.updateCallbacks:
685681
await cb(b, update)
@@ -690,6 +686,13 @@ proc cleanUpdates*(b: TeleBot) {.async.} =
690686
updates = await b.getUpdates()
691687

692688
proc loop(b: TeleBot, timeout = 50, offset, limit = 0) {.async.} =
689+
try:
690+
let me = waitFor b.getMe()
691+
if me.username.isSome:
692+
b.username = me.username.get().toLowerAscii()
693+
except IOError, OSError:
694+
d("Unable to fetch my info ", getCurrentExceptionMsg())
695+
693696
while true:
694697
let updates = await b.getUpdates(timeout=timeout, offset=offset, limit=limit)
695698
for item in updates:

telebot/types.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type
1616
catchallCommandCallback*: CatchallCommandCallback
1717
inlineQueryCallbacks*: seq[InlineQueryCallback]
1818
proxy*: Proxy
19+
username*: string
1920

2021
CatchallCommand* = object of TelegramObject
2122
command*: string

telebot/utils.nim

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,31 @@ const
1111
macro END_POINT*(s: string) =
1212
result = parseStmt("const endpoint = \"" & API_URL & s.strVal & "\"")
1313

14-
proc hasCommand*(update: Update): bool =
14+
template hasCommand*(update: Update, username: string): bool =
15+
var result = false
16+
when not declaredInScope(command):
17+
var
18+
command {.inject.} = ""
19+
params {.inject.} = ""
1520
if update.message.isSome:
16-
var message = update.message.get()
21+
let message = update.message.get()
1722
if message.entities.isSome:
18-
var entities = message.entities.get()
19-
for entity in entities:
20-
if entity.kind == "bot_command":
21-
return true
22-
23-
proc getCommands*(entities: seq[MessageEntity], messageText: string): StringTableRef =
24-
result = newStringTable(modeCaseInsensitive)
25-
for entity in entities:
26-
if entity.kind == "bot_command":
27-
var
28-
offset = entity.offset
29-
length = entity.length
23+
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
3030
command = messageText[(offset + 1)..<(offset + length)].strip()
31-
32-
if '@' in command:
33-
command = command.split('@')[0]
34-
35-
var param = messageText[(offset + length)..^1]
36-
param = param.split(Whitespace, 0).join().strip()
37-
result[command] = param
31+
params = messageText[(offset + length)..^1].splitWhitespace().join().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+
result
3839

3940
proc isSet*(value: any): bool {.inline.} =
4041
when value is string:

0 commit comments

Comments
 (0)