Skip to content

Commit 1485891

Browse files
committed
add a dirty hack to determine file upload
1 parent 50f94a4 commit 1485891

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

examples/echo_bot.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ proc handleUpdate(bot: TeleBot): UpdateCallback =
1515
message.disableNotification = true
1616
message.replyToMessageId = response.messageId
1717
message.parseMode = "markdown"
18-
discard bot.send(message)
18+
discard await bot.send(message)
1919
result = cb
2020

2121
proc greatingHandler(bot: Telebot): CommandCallback =

telebot.nimble

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

telebot/api.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ proc exportChatInviteLink*(b: TeleBot, chatId: string): Future[string] {.async.}
213213
let res = await makeRequest(endpoint % b.token, data)
214214
result = res.getStr
215215

216-
proc setChatPhoto*(b: TeleBot, chatId: string, photo: InputFile): Future[bool] {.async.} =
216+
proc setChatPhoto*(b: TeleBot, chatId: string, photo: string): Future[bool] {.async.} =
217217
END_POINT("setChatPhoto")
218218
var data = newMultipartData()
219219
data["chat_id"] = chatId
@@ -307,7 +307,7 @@ proc getStickerSet*(b: TeleBot, name: string): Future[StickerSet] {.async.} =
307307
let res = await makeRequest(endpoint % b.token, data)
308308
result = unmarshal(res, StickerSet)
309309

310-
proc uploadStickerFile*(b: TeleBot, userId: int, pngSticker: InputFile): Future[types.File] {.async.} =
310+
proc uploadStickerFile*(b: TeleBot, userId: int, pngSticker: string): Future[types.File] {.async.} =
311311
END_POINT("uploadStickerFile")
312312
var data = newMultipartData()
313313
data["user_id"] = $userId

telebot/types.nim

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import options, tables, asyncevents
22

33
type
4-
InputFile* = string
5-
64
TelegramObject* = object of RootObj
75

86
TeleBot* = ref object of TelegramObject

telebot/utils.nim

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,12 @@ proc newProcDef(name: string): NimNode {.compileTime.} =
183183
newStmtList()
184184
)
185185

186-
proc addData*(p: var MultipartData, name: string, content: auto) {.inline.} =
187-
when content is InputFile:
188-
p.addFiles({name: content})
186+
proc addData*(p: var MultipartData, name: string, content: auto, fileCheck = false) {.inline.} =
187+
when content is string:
188+
if fileCheck and content.startsWith("file://"):
189+
p.addFiles({name: content[7..content.len-1]})
190+
else:
191+
p.add(name, $content)
189192
else:
190193
p.add(name, $content)
191194

@@ -250,12 +253,21 @@ macro magic*(head, body: untyped): untyped =
250253
node[0]
251254
))
252255

256+
# dirty hack to determine if the field might be `InputFile`
257+
# if field is InputFile or string, `addData` will checks if it starts w/ file://
258+
# and do file upload
259+
var fileCheck = ident("false")
260+
if toLowerAscii(fieldName) == toLowerAscii($objNameNode):
261+
fileCheck = ident("true")
262+
263+
253264
objSendProcBody.add(
254265
newCall(
255266
ident("addData"),
256267
ident("data"),
257268
newStrLitNode(formatName(fieldName)),
258-
newDotExpr(ident("m"), node[0])
269+
newDotExpr(ident("m"), node[0]),
270+
fileCheck
259271
))
260272

261273
of nnkPragmaExpr:

0 commit comments

Comments
 (0)