Skip to content

Commit 4233bd9

Browse files
committed
send file by wrap file path w/ InputFile
1 parent 0b2e218 commit 4233bd9

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
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.3.3"
1+
version = "0.3.4"
22
author = "Huy Doan"
33
description = "Async Telegram Bot API Client"
44
license = "MIT"

telebot/types.nim

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

33
type
4+
InputFile* = string
5+
46
TelegramObject* = object of RootObj
57

68
TeleBot* = ref object of TelegramObject

telebot/utils.nim

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ proc newProcDef(name: string): NimNode {.compileTime.} =
181181
newStmtList()
182182
)
183183

184+
proc addData*(p: var MultipartData, name: string, content: auto) {.inline.} =
185+
when content is InputFile:
186+
p.addFiles({name: content})
187+
else:
188+
p.add(name, $content)
189+
190+
184191
macro magic*(head, body: untyped): untyped =
185192
result = newStmtList()
186193

@@ -198,7 +205,7 @@ macro magic*(head, body: untyped): untyped =
198205
objectTy.add(newEmptyNode(), newEmptyNode())
199206

200207
var
201-
objRealName = $objNameNode & "Config"
208+
objName = $objNameNode & "Object"
202209
objParamList = newNimNode(nnkRecList)
203210
objInitProc = newProcDef("new" & $objNameNode)
204211
objSendProc = newProcDef("send")
@@ -210,12 +217,12 @@ macro magic*(head, body: untyped): untyped =
210217
objSendProc[4] = newNimNode(nnkPragma).add(ident("async"), ident("discardable"))
211218

212219
objectTy.add(objParamList)
213-
objInitProcParams.add(ident(objRealName))
220+
objInitProcParams.add(ident(objName))
214221

215222
objSendProcParams.add(newNimNode(nnkBracketExpr).add(
216223
ident("Future"), ident("Message")) # return value
217224
).add(newIdentDefs(ident("b"), ident("TeleBot"))
218-
).add(newIdentDefs(ident("m"), ident(objRealName)))
225+
).add(newIdentDefs(ident("m"), ident(objName)))
219226

220227
objSendProcBody.add(newConstStmt(
221228
ident("endpoint"),
@@ -227,6 +234,7 @@ macro magic*(head, body: untyped): untyped =
227234

228235
for node in body.items:
229236
let fieldName = $node[0]
237+
230238
case node[1][0].kind
231239
of nnkIdent:
232240
var identDefs = newIdentDefs(
@@ -240,12 +248,12 @@ macro magic*(head, body: untyped): untyped =
240248
node[0]
241249
))
242250

243-
objSendProcBody.add(newAssignment(
244-
newNimNode(nnkBracketExpr).add(
251+
objSendProcBody.add(
252+
newCall(
253+
ident("addData"),
245254
ident("data"),
246-
newStrLitNode(formatName(fieldName))
247-
),
248-
prefix(newDotExpr(ident("m"), node[0]), "$")
255+
newStrLitNode(formatName(fieldName)),
256+
newDotExpr(ident("m"), node[0])
249257
))
250258

251259
of nnkPragmaExpr:
@@ -264,10 +272,10 @@ macro magic*(head, body: untyped): untyped =
264272
),
265273
newStmtList(
266274
newCall(
267-
ident("add"),
275+
ident("addData"),
268276
ident("data"),
269277
newStrLitNode(formatName(fieldName)),
270-
prefix(newDotExpr(ident("m"), node[0]), "$")
278+
newDotExpr(ident("m"), node[0])
271279
)
272280
)
273281
)
@@ -287,6 +295,6 @@ except:
287295
objSendProcBody.add(epilogue[0])
288296

289297
result.add(newNimNode(nnkTypeSection).add(
290-
newNimNode(nnkTypeDef).add(postfix(ident(objRealName), "*"), newEmptyNode(), objectTy)
298+
newNimNode(nnkTypeDef).add(postfix(ident(objName), "*"), newEmptyNode(), objectTy)
291299
))
292300
result.add(objInitProc, objSendProc)

0 commit comments

Comments
 (0)