Skip to content

Commit 20af00c

Browse files
committed
update send file procs
1 parent ab4fb86 commit 20af00c

File tree

2 files changed

+66
-78
lines changed

2 files changed

+66
-78
lines changed

telebot.nim

Lines changed: 65 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type
4040
width*: int
4141
height*: int
4242
fileSize*: int
43-
43+
4444
Audio* = ref object of RootObj
4545
fileId*: string
4646
duration*: int
@@ -60,17 +60,17 @@ type
6060
height*: int
6161
thumb*: PhotoSize
6262
fileSize*: int
63-
63+
6464
Video* = ref object of RootObj
65-
fileId*: string
65+
fileId*: string
6666
width*: int
6767
height*: int
6868
duration*: int
6969
thumb*: PhotoSize
7070
mimeType*: string
7171
fileSize*: int
7272
caption*: string
73-
73+
7474
Contact* = ref object of RootObj
7575
phoneNumber*: string
7676
firstName*: string
@@ -101,7 +101,7 @@ type
101101
hideKeyboard*: bool
102102
of ForceReply:
103103
forceReply*: bool
104-
104+
105105
MessageKind* = enum
106106
kText
107107
kAudio
@@ -117,7 +117,7 @@ type
117117
kNewChatPhoto
118118
kDeleteChatPhoto
119119
kGroupChatCreated
120-
120+
121121

122122
Message* = ref object of RootObj
123123
messageId*: int
@@ -163,7 +163,7 @@ type
163163
proc `$`*(k: KeyboardMarkup): string =
164164
var j = newJObject()
165165
j["selective"] = %k.selective
166-
case k.kind
166+
case k.kind
167167
of ReplyKeyboardMarkup:
168168
var keyboard: seq[string] = @[]
169169
var kb = newJArray()
@@ -172,14 +172,14 @@ proc `$`*(k: KeyboardMarkup): string =
172172
for y in x:
173173
n.add(%y)
174174
kb.add(n)
175-
175+
176176
j["keyboard"] = kb
177177
j["resize_keyboard"] = %k.resizeKeyboard
178178
j["one_time_keyboard"] = %k.oneTimeKeyboard
179179
of ReplyKeyboardHide:
180180
j["hide_keyboard"] = %k.hideKeyboard
181181
of ForceReply:
182-
j["force_reply"] = %k.forceReply
182+
j["force_reply"] = %k.forceReply
183183
result = $j
184184

185185
proc id*(c: Chat): int =
@@ -188,7 +188,7 @@ proc id*(c: Chat): int =
188188
result = c.user.id
189189
of kGroupChat:
190190
result = c.group.id
191-
191+
192192
proc newReplyKeyboardMarkup*(kb: seq[seq[string]], rk = false, otk = false, s = false): KeyboardMarkup =
193193
new(result)
194194
result.kind = ReplyKeyboardMarkup
@@ -208,9 +208,9 @@ proc newForceReply*(f = true, s = false): KeyboardMarkup =
208208
result.kind = ForceReply
209209
result.forceReply = f
210210
result.selective = s
211-
211+
212212
proc parseUser(n: JsonNode): User =
213-
new(result)
213+
new(result)
214214
result.id = n["id"].num.int
215215
result.firstName = n["first_name"].str
216216
if not n["last_name"].isNil:
@@ -219,10 +219,10 @@ proc parseUser(n: JsonNode): User =
219219
result.username = n["username"].str
220220

221221
proc parseGroupChat(n: JsonNode): GroupChat =
222-
new(result)
222+
new(result)
223223
result.id = n["id"].num.int
224224
result.title = n["title"].str
225-
225+
226226
proc parseChat(n: JsonNode): Chat =
227227
new(result)
228228
if n["id"].num.int > 0:
@@ -243,22 +243,23 @@ proc parseAudio(n: JsonNode): Audio =
243243

244244
proc parsePhotoSize(n: JsonNode): PhotoSize =
245245
new(result)
246-
if not n["file_id"].isNil:
246+
if n.hasKey("file_id"):
247247
result.fileId = n["file_id"].str
248248
result.width = n["width"].num.int
249249
result.height = n["height"].num.int
250250
if not n["file_size"].isNil:
251251
result.fileSize = n["file_size"].num.int
252-
252+
253253
proc parsePhoto(n: JsonNode): seq[PhotoSize] =
254254
result = @[]
255255
for x in n:
256256
result.add(parsePhotoSize(x))
257-
257+
258258
proc parseDocument(n: JsonNode): Document =
259259
new(result)
260260
result.fileId = n["file_id"].str
261-
result.thumb = parsePhotoSize(n["thumb"])
261+
if n.hasKey("thumb"):
262+
result.thumb = parsePhotoSize(n["thumb"])
262263
if not n["file_name"].isNil:
263264
result.fileName = n["file_name"].str
264265
if not n["mime_type"].isNil:
@@ -270,8 +271,9 @@ proc parseSticker(n: JsonNode): Sticker =
270271
new(result)
271272
result.fileId = n["file_id"].str
272273
result.width = n["width"].num.int
273-
result.height = n["height"].num.int
274-
result.thumb = parsePhotoSize(n["thumb"])
274+
result.height = n["height"].num.int
275+
if n.hasKey("thumb"):
276+
result.thumb = parsePhotoSize(n["thumb"])
275277
if not n["file_size"].isNil:
276278
result.fileSize = n["file_size"].num.int
277279

@@ -280,10 +282,11 @@ proc parseVideo(n: JsonNode): Video =
280282
result.fileId = n["file_id"].str
281283
result.width = n["width"].num.int
282284
result.height = n["height"].num.int
283-
result.duration = n["duration"].num.int
284-
result.thumb = parsePhotoSize(n["thumb"])
285+
result.duration = n["duration"].num.int
286+
if n.hasKey("thumb"):
287+
result.thumb = parsePhotoSize(n["thumb"])
285288
if not n["mime_type"].isNil:
286-
result.mimeType = n["mime_type"].str
289+
result.mimeType = n["mime_type"].str
287290
if not n["file_size"].isNil:
288291
result.fileSize = n["file_size"].num.int
289292
if not n["caption"].isNil:
@@ -309,7 +312,7 @@ proc parseUserProfilePhotos(n: JsonNode): UserProfilePhotos =
309312
result.photos = @[]
310313
for x in n["photos"]:
311314
result.photos.add(parsePhoto(x))
312-
315+
313316
proc parseMessage(n: JsonNode): Message =
314317
new(result)
315318
result.messageId = n["message_id"].num.int
@@ -322,8 +325,8 @@ proc parseMessage(n: JsonNode): Message =
322325
if not n["forward_date"].isNil:
323326
result.forwardDate = n["forward_date"].num.int
324327
if not n["reply_to_message"].isNil:
325-
result.replyToMessage = parseMessage(n["reply_to_message"])
326-
328+
result.replyToMessage = parseMessage(n["reply_to_message"])
329+
327330
if not n["text"].isNil:
328331
result.kind = kText
329332
result.text = n["text"].str
@@ -371,40 +374,40 @@ proc parseUpdates(b: TeleBot, n: JsonNode): seq[Update] =
371374
result = @[]
372375
var u: Update
373376
for x in n:
374-
new(u)
377+
new(u)
375378
u.updateId = x["update_id"].num.int
376379
if u.updateId > b.lastUpdateId:
377380
b.lastUpdateId = u.updateId
378381
u.message = parseMessage(x["message"])
379382
result.add(u)
380-
383+
381384
proc newTeleBot*(token: string): TeleBot =
382-
## Init new Telegram Bot instance
385+
## Init new Telegram Bot instance
383386
new(result)
384387
result.token = token
385388
result.lastUpdateId = 0
386389

387390
proc makeRequest(endpoint: string, data: MultipartData = nil): Future[JsonNode] {.async.} =
388391
let client = newAsyncHttpClient()
389392
let r = await client.post(endpoint, multipart=data)
390-
if r.status.startsWith("200"):
393+
if r.status.startsWith("200"):
391394
var obj = parseJson(r.body)
392395
if obj["ok"].bval == true:
393396
result = obj["result"]
394397
else:
395-
raise newException(IOError, r.status)
398+
raise newException(IOError, r.status)
396399
client.close()
397-
398-
400+
401+
399402
proc getMe*(b: TeleBot): Future[User] {.async.} =
400-
## Returns basic information about the bot in form of a ``User`` object.
403+
## Returns basic information about the bot in form of a ``User`` object.
401404
let endpoint = API_URL % [b.token, "getMe"]
402405
let res = await makeRequest(endpoint)
403406
result = parseUser(res)
404407

405408
proc sendMessage*(b: TeleBot, chatId: int, text: string, disableWebPagePreview = false, replyToMessageId = 0, replyMarkup: KeyboardMarkup = nil): Future[Message] {.async.} =
406409
let endpoint = API_URL % [b.token, "sendMessage"]
407-
var data = newMultipartData()
410+
var data = newMultipartData()
408411
data["chat_id"] = $chatId
409412
data["text"] = text
410413
if disableWebPagePreview:
@@ -416,40 +419,38 @@ proc sendMessage*(b: TeleBot, chatId: int, text: string, disableWebPagePreview =
416419

417420
let res = await makeRequest(endpoint, data)
418421
result = parseMessage(res)
419-
422+
420423

421424
proc forwardMessage*(b: TeleBot, chatId: int, fromChatId: int, messageId: int): Future[Message] {.async.} =
422425
let endpoint = API_URL % [b.token, "forwardMessage"]
423-
var data = newMultipartData()
426+
var data = newMultipartData()
424427
data["chat_id"] = $chatId
425428
data["from_chat_id"] = $fromChatId
426429
data["message_id"] = $messageId
427430

428431
let res = await makeRequest(endpoint, data)
429432
result = parseMessage(res)
430-
431433

432-
proc sendPhoto*(b: TeleBot, chatId: int, photo: string, resend = false,cap = "", rtmId = 0, rM: KeyboardMarkup = nil): Future[Message] {.async.} =
433-
let endpoint = API_URL % [b.token, "sendPhoto"]
434+
435+
proc sendFile(b: TeleBot, m: string, chatId: int, key, val: string, resend = false, rtmId = 0, rM: KeyboardMarkup = nil): Future[Message] {.async.} =
436+
let endpoint = API_URL % [b.token, m]
437+
434438
var data = newMultipartData()
435439
data["chat_id"] = $chatId
436440

437441
if resend:
438-
# resend file_id
439-
data["photo"] = photo
442+
data[key] = val
440443
else:
441-
if photo.startsWith("http"):
442-
let u = parseUri(photo)
444+
if val.startsWith("http"):
445+
let u = parseUri(val)
443446
var (_, _, ext) = u.path.splitFile()
444447
let tmp = mktemp(suffix=ext)
445-
downloadFile(photo, tmp)
446-
data.addFiles({"photo": tmp})
448+
downloadFile(val, tmp)
449+
data.addFiles({key: tmp})
447450
tmp.removeFile
448451
else:
449-
data.addFiles({"photo": photo})
452+
data.addFiles({key: val})
450453

451-
if cap != "":
452-
data["caption"] = cap
453454
if rtmId != 0:
454455
data["reply_to_message_id"] = $rtmId
455456
if not rM.isNil:
@@ -458,32 +459,20 @@ proc sendPhoto*(b: TeleBot, chatId: int, photo: string, resend = false,cap = "",
458459
let res = await makeRequest(endpoint, data)
459460
result = parseMessage(res)
460461

461-
proc sendFile(b: TeleBot, m: string, chatId: int, f: string, resend = false, rtmId = 0, rM: KeyboardMarkup = nil): Future[Message] {.async.} =
462-
let endpoint = API_URL % [b.token, m]
463-
464-
var data = newMultipartData()
465-
data["chat_id"] = $chatId
466-
467-
if rtmId != 0:
468-
data["reply_to_message_id"] = $rtmId
469-
if not rM.isNil:
470-
data["reply_markup"] = $rM
471-
472-
let res = await makeRequest(endpoint, data)
473-
result = parseMessage(res)
462+
proc sendPhoto*(b: TeleBot, chatId: int, photo: string, resend = false, cap = "", rtmId = 0, rM: KeyboardMarkup = nil): Future[Message] {.async.} =
463+
result = await b.sendFile("sendPhoto", chatId, "photo", photo, resend, rtmId, rM)
474464

475-
476465
proc sendAudio*(b: TeleBot, chatId: int, audio: string, resend = false, rtmId = 0, rM: KeyboardMarkup = nil): Future[Message] {.async.} =
477-
result = await b.sendFile("sendAudio", chatId, audio, resend, rtmId, rM)
466+
result = await b.sendFile("sendAudio", chatId, "audio", audio, resend, rtmId, rM)
478467

479-
proc sendDocument*(b: TeleBot, chatId: int, audio: string, resend = false, rtmId = 0, rM: KeyboardMarkup = nil): Future[Message] {.async.} =
480-
result = await b.sendFile("sendDocument", chatId, audio, resend, rtmId, rM)
468+
proc sendDocument*(b: TeleBot, chatId: int, document: string, resend = false, rtmId = 0, rM: KeyboardMarkup = nil): Future[Message] {.async.} =
469+
result = await b.sendFile("sendDocument", chatId, "document", document, resend, rtmId, rM)
481470

482-
proc sendSticker*(b: TeleBot, chatId: int, audio: string, resend = false, rtmId = 0, rM: KeyboardMarkup = nil): Future[Message] {.async.} =
483-
result = await b.sendFile("sendSticker", chatId, audio, resend, rtmId, rM)
471+
proc sendSticker*(b: TeleBot, chatId: int, sticker: string, resend = false, rtmId = 0, rM: KeyboardMarkup = nil): Future[Message] {.async.} =
472+
result = await b.sendFile("sendSticker", chatId, "sticker", sticker, resend, rtmId, rM)
484473

485-
proc sendVideo*(b: TeleBot, chatId: int, audio: string, resend = false, rtmId = 0, rM: KeyboardMarkup = nil): Future[Message] {.async.} =
486-
result = await b.sendFile("sendVideo", chatId, audio, resend, rtmId, rM)
474+
proc sendVideo*(b: TeleBot, chatId: int, video: string, resend = false, rtmId = 0, rM: KeyboardMarkup = nil): Future[Message] {.async.} =
475+
result = await b.sendFile("sendVideo", chatId, "video", video, resend, rtmId, rM)
487476

488477
proc sendLocation*(b: TeleBot, chatId: int, lat, long: float, rtmId = 0, rM: KeyboardMarkup = nil): Future[Message] {.async.} =
489478
let endpoint = API_URL % [b.token, "sendLocation"]
@@ -492,7 +481,7 @@ proc sendLocation*(b: TeleBot, chatId: int, lat, long: float, rtmId = 0, rM: Key
492481
data["chat_id"] = $chatId
493482
data["longitude"] = $long
494483
data["latitude"] = $lat
495-
484+
496485
if rtmId != 0:
497486
data["reply_to_message_id"] = $rtmId
498487
if not rM.isNil:
@@ -508,13 +497,13 @@ proc sendChatAction*(b: TeleBot, chatId: int, action: string): Future[void] {.as
508497
data["action"] = action
509498

510499
discard makeRequest(endpoint, data)
511-
500+
512501
proc getUserProfilePhotos*(b: TeleBot, userId: int, offset = 0, limit = 100): Future[UserProfilePhotos] {.async.} =
513502
let endpoint = API_URL % [b.token, "getUserProfilePhotos"]
514503
var data = newMultipartData()
515504
data["user_id"] = $userId
516505
data["limit"] = $limit
517-
506+
518507
if offset != 0:
519508
data["offset"] = $offset
520509
let res = await makeRequest(endpoint, data)
@@ -523,7 +512,7 @@ proc getUserProfilePhotos*(b: TeleBot, userId: int, offset = 0, limit = 100): Fu
523512
proc getUpdates*(b: TeleBot, offset, limit, timeout = 0): Future[seq[Update]] {.async.} =
524513
let endpoint = API_URL % [b.token, "getUpdates"]
525514
var data = newMultipartData()
526-
515+
527516
if offset != 0:
528517
data["offset"] = $offset
529518
else:
@@ -538,8 +527,7 @@ proc getUpdates*(b: TeleBot, offset, limit, timeout = 0): Future[seq[Update]] {.
538527

539528
proc setWebhook*(b: TeleBot, url: string) {.async.} =
540529
let endpoint = API_URL % [b.token, "setWebhook"]
541-
var data = newMultipartData()
530+
var data = newMultipartData()
542531
data["url"] = url
543-
532+
544533
discard await makeRequest(endpoint, data)
545-

telebot.nimble

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[Package]
22
name = "telebot"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
author = "Huy Doan"
55
description = "Async Telegram Bot API Client"
66
license = "MIT"

0 commit comments

Comments
 (0)