40
40
width* : int
41
41
height* : int
42
42
fileSize* : int
43
-
43
+
44
44
Audio * = ref object of RootObj
45
45
fileId* : string
46
46
duration* : int
60
60
height* : int
61
61
thumb* : PhotoSize
62
62
fileSize* : int
63
-
63
+
64
64
Video * = ref object of RootObj
65
- fileId* : string
65
+ fileId* : string
66
66
width* : int
67
67
height* : int
68
68
duration* : int
69
69
thumb* : PhotoSize
70
70
mimeType* : string
71
71
fileSize* : int
72
72
caption* : string
73
-
73
+
74
74
Contact * = ref object of RootObj
75
75
phoneNumber* : string
76
76
firstName* : string
101
101
hideKeyboard* : bool
102
102
of ForceReply :
103
103
forceReply* : bool
104
-
104
+
105
105
MessageKind * = enum
106
106
kText
107
107
kAudio
117
117
kNewChatPhoto
118
118
kDeleteChatPhoto
119
119
kGroupChatCreated
120
-
120
+
121
121
122
122
Message * = ref object of RootObj
123
123
messageId* : int
163
163
proc `$` * (k: KeyboardMarkup ): string =
164
164
var j = newJObject ()
165
165
j[" selective" ] = % k.selective
166
- case k.kind
166
+ case k.kind
167
167
of ReplyKeyboardMarkup :
168
168
var keyboard: seq [string ] = @ []
169
169
var kb = newJArray ()
@@ -172,14 +172,14 @@ proc `$`*(k: KeyboardMarkup): string =
172
172
for y in x:
173
173
n.add (% y)
174
174
kb.add (n)
175
-
175
+
176
176
j[" keyboard" ] = kb
177
177
j[" resize_keyboard" ] = % k.resizeKeyboard
178
178
j[" one_time_keyboard" ] = % k.oneTimeKeyboard
179
179
of ReplyKeyboardHide :
180
180
j[" hide_keyboard" ] = % k.hideKeyboard
181
181
of ForceReply :
182
- j[" force_reply" ] = % k.forceReply
182
+ j[" force_reply" ] = % k.forceReply
183
183
result = $ j
184
184
185
185
proc id * (c: Chat ): int =
@@ -188,7 +188,7 @@ proc id*(c: Chat): int =
188
188
result = c.user.id
189
189
of kGroupChat:
190
190
result = c.group.id
191
-
191
+
192
192
proc newReplyKeyboardMarkup * (kb: seq [seq [string ]], rk = false , otk = false , s = false ): KeyboardMarkup =
193
193
new (result )
194
194
result .kind = ReplyKeyboardMarkup
@@ -208,9 +208,9 @@ proc newForceReply*(f = true, s = false): KeyboardMarkup =
208
208
result .kind = ForceReply
209
209
result .forceReply = f
210
210
result .selective = s
211
-
211
+
212
212
proc parseUser (n: JsonNode ): User =
213
- new (result )
213
+ new (result )
214
214
result .id = n[" id" ].num.int
215
215
result .firstName = n[" first_name" ].str
216
216
if not n[" last_name" ].isNil:
@@ -219,10 +219,10 @@ proc parseUser(n: JsonNode): User =
219
219
result .username = n[" username" ].str
220
220
221
221
proc parseGroupChat (n: JsonNode ): GroupChat =
222
- new (result )
222
+ new (result )
223
223
result .id = n[" id" ].num.int
224
224
result .title = n[" title" ].str
225
-
225
+
226
226
proc parseChat (n: JsonNode ): Chat =
227
227
new (result )
228
228
if n[" id" ].num.int > 0 :
@@ -243,22 +243,23 @@ proc parseAudio(n: JsonNode): Audio =
243
243
244
244
proc parsePhotoSize (n: JsonNode ): PhotoSize =
245
245
new (result )
246
- if not n[ " file_id" ].isNil :
246
+ if n. hasKey ( " file_id" ) :
247
247
result .fileId = n[" file_id" ].str
248
248
result .width = n[" width" ].num.int
249
249
result .height = n[" height" ].num.int
250
250
if not n[" file_size" ].isNil:
251
251
result .fileSize = n[" file_size" ].num.int
252
-
252
+
253
253
proc parsePhoto (n: JsonNode ): seq [PhotoSize ] =
254
254
result = @ []
255
255
for x in n:
256
256
result .add (parsePhotoSize (x))
257
-
257
+
258
258
proc parseDocument (n: JsonNode ): Document =
259
259
new (result )
260
260
result .fileId = n[" file_id" ].str
261
- result .thumb = parsePhotoSize (n[" thumb" ])
261
+ if n.hasKey (" thumb" ):
262
+ result .thumb = parsePhotoSize (n[" thumb" ])
262
263
if not n[" file_name" ].isNil:
263
264
result .fileName = n[" file_name" ].str
264
265
if not n[" mime_type" ].isNil:
@@ -270,8 +271,9 @@ proc parseSticker(n: JsonNode): Sticker =
270
271
new (result )
271
272
result .fileId = n[" file_id" ].str
272
273
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" ])
275
277
if not n[" file_size" ].isNil:
276
278
result .fileSize = n[" file_size" ].num.int
277
279
@@ -280,10 +282,11 @@ proc parseVideo(n: JsonNode): Video =
280
282
result .fileId = n[" file_id" ].str
281
283
result .width = n[" width" ].num.int
282
284
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" ])
285
288
if not n[" mime_type" ].isNil:
286
- result .mimeType = n[" mime_type" ].str
289
+ result .mimeType = n[" mime_type" ].str
287
290
if not n[" file_size" ].isNil:
288
291
result .fileSize = n[" file_size" ].num.int
289
292
if not n[" caption" ].isNil:
@@ -309,7 +312,7 @@ proc parseUserProfilePhotos(n: JsonNode): UserProfilePhotos =
309
312
result .photos = @ []
310
313
for x in n[" photos" ]:
311
314
result .photos.add (parsePhoto (x))
312
-
315
+
313
316
proc parseMessage (n: JsonNode ): Message =
314
317
new (result )
315
318
result .messageId = n[" message_id" ].num.int
@@ -322,8 +325,8 @@ proc parseMessage(n: JsonNode): Message =
322
325
if not n[" forward_date" ].isNil:
323
326
result .forwardDate = n[" forward_date" ].num.int
324
327
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
+
327
330
if not n[" text" ].isNil:
328
331
result .kind = kText
329
332
result .text = n[" text" ].str
@@ -371,40 +374,40 @@ proc parseUpdates(b: TeleBot, n: JsonNode): seq[Update] =
371
374
result = @ []
372
375
var u: Update
373
376
for x in n:
374
- new (u)
377
+ new (u)
375
378
u.updateId = x[" update_id" ].num.int
376
379
if u.updateId > b.lastUpdateId:
377
380
b.lastUpdateId = u.updateId
378
381
u.message = parseMessage (x[" message" ])
379
382
result .add (u)
380
-
383
+
381
384
proc newTeleBot * (token: string ): TeleBot =
382
- # # Init new Telegram Bot instance
385
+ # # Init new Telegram Bot instance
383
386
new (result )
384
387
result .token = token
385
388
result .lastUpdateId = 0
386
389
387
390
proc makeRequest (endpoint: string , data: MultipartData = nil ): Future [JsonNode ] {.async .} =
388
391
let client = newAsyncHttpClient ()
389
392
let r = await client.post (endpoint, multipart= data)
390
- if r.status.startsWith (" 200" ):
393
+ if r.status.startsWith (" 200" ):
391
394
var obj = parseJson (r.body)
392
395
if obj[" ok" ].bval == true :
393
396
result = obj[" result" ]
394
397
else :
395
- raise newException (IOError , r.status)
398
+ raise newException (IOError , r.status)
396
399
client.close ()
397
-
398
-
400
+
401
+
399
402
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.
401
404
let endpoint = API_URL % [b.token, " getMe" ]
402
405
let res = await makeRequest (endpoint)
403
406
result = parseUser (res)
404
407
405
408
proc sendMessage * (b: TeleBot , chatId: int , text: string , disableWebPagePreview = false , replyToMessageId = 0 , replyMarkup: KeyboardMarkup = nil ): Future [Message ] {.async .} =
406
409
let endpoint = API_URL % [b.token, " sendMessage" ]
407
- var data = newMultipartData ()
410
+ var data = newMultipartData ()
408
411
data[" chat_id" ] = $ chatId
409
412
data[" text" ] = text
410
413
if disableWebPagePreview:
@@ -416,40 +419,38 @@ proc sendMessage*(b: TeleBot, chatId: int, text: string, disableWebPagePreview =
416
419
417
420
let res = await makeRequest (endpoint, data)
418
421
result = parseMessage (res)
419
-
422
+
420
423
421
424
proc forwardMessage * (b: TeleBot , chatId: int , fromChatId: int , messageId: int ): Future [Message ] {.async .} =
422
425
let endpoint = API_URL % [b.token, " forwardMessage" ]
423
- var data = newMultipartData ()
426
+ var data = newMultipartData ()
424
427
data[" chat_id" ] = $ chatId
425
428
data[" from_chat_id" ] = $ fromChatId
426
429
data[" message_id" ] = $ messageId
427
430
428
431
let res = await makeRequest (endpoint, data)
429
432
result = parseMessage (res)
430
-
431
433
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
+
434
438
var data = newMultipartData ()
435
439
data[" chat_id" ] = $ chatId
436
440
437
441
if resend:
438
- # resend file_id
439
- data[" photo" ] = photo
442
+ data[key] = val
440
443
else :
441
- if photo .startsWith (" http" ):
442
- let u = parseUri (photo )
444
+ if val .startsWith (" http" ):
445
+ let u = parseUri (val )
443
446
var (_, _, ext) = u.path.splitFile ()
444
447
let tmp = mktemp (suffix= ext)
445
- downloadFile (photo , tmp)
446
- data.addFiles ({" photo " : tmp})
448
+ downloadFile (val , tmp)
449
+ data.addFiles ({key : tmp})
447
450
tmp.removeFile
448
451
else :
449
- data.addFiles ({" photo " : photo })
452
+ data.addFiles ({key: val })
450
453
451
- if cap != " " :
452
- data[" caption" ] = cap
453
454
if rtmId != 0 :
454
455
data[" reply_to_message_id" ] = $ rtmId
455
456
if not rM.isNil:
@@ -458,32 +459,20 @@ proc sendPhoto*(b: TeleBot, chatId: int, photo: string, resend = false,cap = "",
458
459
let res = await makeRequest (endpoint, data)
459
460
result = parseMessage (res)
460
461
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)
474
464
475
-
476
465
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)
478
467
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)
481
470
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)
484
473
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)
487
476
488
477
proc sendLocation * (b: TeleBot , chatId: int , lat, long: float , rtmId = 0 , rM: KeyboardMarkup = nil ): Future [Message ] {.async .} =
489
478
let endpoint = API_URL % [b.token, " sendLocation" ]
@@ -492,7 +481,7 @@ proc sendLocation*(b: TeleBot, chatId: int, lat, long: float, rtmId = 0, rM: Key
492
481
data[" chat_id" ] = $ chatId
493
482
data[" longitude" ] = $ long
494
483
data[" latitude" ] = $ lat
495
-
484
+
496
485
if rtmId != 0 :
497
486
data[" reply_to_message_id" ] = $ rtmId
498
487
if not rM.isNil:
@@ -508,13 +497,13 @@ proc sendChatAction*(b: TeleBot, chatId: int, action: string): Future[void] {.as
508
497
data[" action" ] = action
509
498
510
499
discard makeRequest (endpoint, data)
511
-
500
+
512
501
proc getUserProfilePhotos * (b: TeleBot , userId: int , offset = 0 , limit = 100 ): Future [UserProfilePhotos ] {.async .} =
513
502
let endpoint = API_URL % [b.token, " getUserProfilePhotos" ]
514
503
var data = newMultipartData ()
515
504
data[" user_id" ] = $ userId
516
505
data[" limit" ] = $ limit
517
-
506
+
518
507
if offset != 0 :
519
508
data[" offset" ] = $ offset
520
509
let res = await makeRequest (endpoint, data)
@@ -523,7 +512,7 @@ proc getUserProfilePhotos*(b: TeleBot, userId: int, offset = 0, limit = 100): Fu
523
512
proc getUpdates * (b: TeleBot , offset, limit, timeout = 0 ): Future [seq [Update ]] {.async .} =
524
513
let endpoint = API_URL % [b.token, " getUpdates" ]
525
514
var data = newMultipartData ()
526
-
515
+
527
516
if offset != 0 :
528
517
data[" offset" ] = $ offset
529
518
else :
@@ -538,8 +527,7 @@ proc getUpdates*(b: TeleBot, offset, limit, timeout = 0): Future[seq[Update]] {.
538
527
539
528
proc setWebhook * (b: TeleBot , url: string ) {.async .} =
540
529
let endpoint = API_URL % [b.token, " setWebhook" ]
541
- var data = newMultipartData ()
530
+ var data = newMultipartData ()
542
531
data[" url" ] = url
543
-
532
+
544
533
discard await makeRequest (endpoint, data)
545
-
0 commit comments