Skip to content

Commit 2a62a34

Browse files
committed
Bot API 5.1
1 parent 276ca13 commit 2a62a34

File tree

4 files changed

+98
-11
lines changed

4 files changed

+98
-11
lines changed

examples/file_receive_bot/file_receive_bot.nim

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ from strutils import strip
66
const API_KEY = slurp("../secret.key").strip()
77

88

9+
proc buildFile(file_id: string) =
10+
let
11+
metadata_url = fmt"https://api.telegram.org/bot{API_KEY}/getFile?file_id={file_id}"
12+
body = await newAsyncHttpClient().getContent(metadata_url) # file_id > file_path
13+
file_path = parseJson(body)["result"]["file_path"].getStr()
14+
download_url = fmt"https://api.telegram.org/file/bot{API_KEY}/{file_path}"
15+
16+
917
proc updateHandler(bot: TeleBot, e: Update): Future[bool] {.async.} =
1018
let
1119
url_getfile = fmt"https://api.telegram.org/bot{API_KEY}/getFile?file_id="
@@ -21,15 +29,14 @@ proc updateHandler(bot: TeleBot, e: Update): Future[bool] {.async.} =
2129
mime_type = document.mime_type.get
2230
file_id = document.file_id
2331
file_size = document.file_size.get
24-
responz = await newAsyncHttpClient().get(url_getfile & file_id) # file_id > file_path
25-
responz_body = await responz.body
32+
responz_body = await newAsyncHttpClient().getContent(url_getfile & file_id) # file_id > file_path
2633
file_path = parseJson(responz_body)["result"]["file_path"].getStr()
2734
responx = await newAsyncHttpClient().get(api_file & file_path) # file_path > file
2835
file_content = await responx.body
2936
msg0_text = fmt"file_name: {file_name}, mime_type: {mime_type}, file_id: {file_id}, file_size: {file_size}, file_path: {file_path}"
3037

3138
discard await bot.sendMessage(response.chat.id, msg0_text)
32-
#discard await bot.sendMessage(response.chat.id, file_content)
39+
discard await bot.sendMessage(response.chat.id, file_content)
3340

3441
let bot = newTeleBot(API_KEY)
3542

src/telebot/private/api.nim

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -539,12 +539,14 @@ proc getFile*(b: TeleBot, fileId: string): Future[types.File] {.async.} =
539539
let res = await makeRequest(b, procName, data)
540540
result = unmarshal(res, types.File)
541541

542-
proc kickChatMember*(b: TeleBot, chatId: string, userId: int, untilDate = 0): Future[bool] {.async.} =
542+
proc kickChatMember*(b: TeleBot, chatId: string, userId: int, untilDate = 0, revokeMessages = false): Future[bool] {.async.} =
543543
var data = newMultipartData()
544544
data["chat_id"] = chatId
545545
data["user_id"] = $userId
546546
if untilDate > 0:
547547
data["until_date"] = $untilDate
548+
if revokeMessages:
549+
data["revoke_messages"] = "true"
548550
let res = await makeRequest(b, procName, data)
549551
result = res.toBool
550552

@@ -569,14 +571,16 @@ proc restrictChatMember*(b: TeleBot, chatId: string, userId: int, permissions: C
569571
let res = await makeRequest(b, procName, data)
570572
result = res.toBool
571573

572-
proc promoteChatMember*(b: TeleBot, chatId: string, userId: int, isAnonymous = false, canChangeInfo = false,
573-
canPostMessages = false, canEditMessages = false, canDeleteMessages = false,
574+
proc promoteChatMember*(b: TeleBot, chatId: string, userId: int, isAnonymous = false, canManageChat = false, canChangeInfo = false,
575+
canPostMessages = false, canEditMessages = false, canDeleteMessages = false, canManageVoiceChats = false,
574576
canInviteUsers = false, canRestrictMembers = false, canPinMessages = false, canPromoteMembers = false): Future[bool] {.async.} =
575577
var data = newMultipartData()
576578
data["chat_id"] = chatId
577579
data["user_id"] = $userId
578580
if isAnonymous:
579581
data["is_anonymous"] = "true"
582+
if canManageChat:
583+
data["can_manage_chat"] = "true"
580584
if canChangeInfo:
581585
data["can_change_info"] = "true"
582586
if canPostMessages:
@@ -585,6 +589,8 @@ proc promoteChatMember*(b: TeleBot, chatId: string, userId: int, isAnonymous = f
585589
data["can_edit_messages"] = "true"
586590
if canDeleteMessages:
587591
data["can_delete_messages"] = "true"
592+
if canManageVoiceChats:
593+
data["can_manage_voice_chats"] = "true"
588594
if canInviteUsers:
589595
data["can_invite_users"] = "true"
590596
if canRestrictMembers:
@@ -1094,7 +1100,7 @@ proc pollAsync*(b: TeleBot, timeout = 50, offset, limit = 0, clean = false) {.as
10941100
await loop(b, timeout, offset, limit)
10951101

10961102

1097-
proc sendGame*(b: TeleBot, chatId: int, gameShortName: string, disableNotification = false, replyToMessageId = 0,
1103+
proc sendGame*(b: TeleBot, chatId: int64, gameShortName: string, disableNotification = false, replyToMessageId = 0,
10981104
allowSendingWithoutReply = false, replyMarkup: InlineKeyboardMarkup): Future[Message] {.async.} =
10991105
var data = newMultipartData()
11001106

@@ -1117,7 +1123,7 @@ proc setGameScore*(b: TeleBot, userId: int, score: int, force = false, disableEd
11171123

11181124
var data = newMultipartData()
11191125

1120-
data["user_id"] = $chatId
1126+
data["user_id"] = $userId
11211127
data["score"] = $score
11221128
if force:
11231129
data["force"] = "true"
@@ -1133,12 +1139,47 @@ proc setGameScore*(b: TeleBot, userId: int, score: int, force = false, disableEd
11331139
proc getGameHighScores*(b: TeleBot, userId: int, chatId = 0, messageId = 0, inlineMessageId = 0): Future[seq[GameHighScore]] {.async.} =
11341140
var data = newMultipartData()
11351141

1136-
data["user_id"] = $chatId
1142+
data["user_id"] = $userId
11371143
if chatId != 0:
11381144
data["chat_id"] = $chatId
11391145
if messageId != 0:
11401146
data["message_id"] = $messageId
11411147
if inlineMessageId != 0:
11421148
data["inline_message_id"] = $inlineMessageId
11431149
let res = await makeRequest(b, procName, data)
1144-
result = unmarshal(res, seq[GameHighScore])
1150+
result = unmarshal(res, seq[GameHighScore])
1151+
1152+
1153+
proc createChatInviteLink*(b: Telebot, chatId: ChatId, expireDate = 0, memberLimit = 0): Future[ChatInviteLink] {.async.} =
1154+
var data = newMultipartData()
1155+
1156+
data["chat_id"] = $chatId
1157+
if expireDate > 0:
1158+
data["expire_date"] = $expireDate
1159+
if memberLimit > 0:
1160+
data["member_limit"] = $memberLimit
1161+
1162+
let res = await makeRequest(b, procName, data)
1163+
result = unmarshal(res, seq[ChatInviteLink])
1164+
1165+
proc editChatInviteLink*(b: Telebot, chatId: ChatId, inviteLink: string, expireDate = 0, memberLimit = 0): Future[ChatInviteLink] {.async.} =
1166+
var data = newMultipartData()
1167+
1168+
data["chat_id"] = $chatId
1169+
data["invite_link"] = invite_link
1170+
if expireDate > 0:
1171+
data["expire_date"] = $expireDate
1172+
if memberLimit > 0:
1173+
data["member_limit"] = $memberLimit
1174+
1175+
let res = await makeRequest(b, procName, data)
1176+
result = unmarshal(res, seq[ChatInviteLink])
1177+
1178+
proc revokeChatInviteLink*(b: Telebot, chatId: ChatId, inviteLink: string): Future[ChatInviteLink] {.async.} =
1179+
var data = newMultipartData()
1180+
1181+
data["chat_id"] = $chatId
1182+
data["invite_link"] = invite_link
1183+
1184+
let res = await makeRequest(b, procName, data)
1185+
result = unmarshal(res, seq[ChatInviteLink])

src/telebot/private/types.nim

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ converter optionToBool*[T](o: Option[T]): bool = o.isSome()
66
type
77
TelegramObject* = object of RootObj
88

9+
ChatId* = int64|string
10+
911
UpdateCallback* = proc(bot: Telebot, update: Update): Future[bool] {.gcsafe.}
1012
CommandCallback* = proc(bot: Telebot, command: Command): Future[bool] {.gcsafe.}
1113
CatchallCommandCallback* = proc(bot: Telebot, command: Command): Future[bool] {.gcsafe.}
@@ -185,6 +187,17 @@ type
185187
watcher*: User
186188
distance*: int
187189

190+
MessageAutoDeleteTimerChanged* = object of TelegramObject
191+
messageAutoDeleteTime*: int
192+
193+
VoiceChatStarted* = object of TelegramObject
194+
195+
VoiceChatEnded* = object of TelegramObject
196+
duration*: int
197+
198+
VoiceChatParticipantsInvited* = object of TelegramObject
199+
users*: seq[User]
200+
188201
UserProfilePhotos* = object of TelegramObject
189202
totalCount*: int
190203
photos*: seq[seq[PhotoSize]]
@@ -312,6 +325,7 @@ type
312325
groupChatCreated*: Option[bool]
313326
superGroupChatCreated*: Option[bool]
314327
chanelChatCreated*: Option[bool]
328+
messageAutoDeleteTimerChanged*: Option[MessageAutoDeleteTimerChanged]
315329
migrateToChatId*: Option[int64]
316330
migrateFromChatId*: Option[int64]
317331
pinnedMessage*: Option[ref Message]
@@ -320,6 +334,9 @@ type
320334
connectedWebsite*: Option[string]
321335
passportData*: Option[PassportData]
322336
proximityAlertTriggered*: Option[ProximityAlertTriggered]
337+
voiceChatStarted*: Option[VoiceChatStarted]
338+
voiceChatEnded*: Option[VoiceChatEnded]
339+
voiceChatParticipantsInvited*: Option[VoiceChatParticipantsInvited]
323340
replyMarkup*: Option[InlineKeyboardMarkup]
324341

325342
MessageId* = object of TelegramObject
@@ -331,16 +348,26 @@ type
331348
bigFileId*: string
332349
bigFileUniqueId*: string
333350

351+
ChatInviteLink* = object of TelegramObject
352+
inviteLink*: string
353+
creator*: User
354+
isPrimary*: bool
355+
isRevoked*: bool
356+
expireDate*: Option[int]
357+
memberLimit*: Option[int]
358+
334359
ChatMember* = object of TelegramObject
335360
user*: User
336361
status*: string
337362
customTitle*: Option[string]
338363
isAnonymous*: Option[bool]
339364
untilDate*: Option[int]
340365
canBeEdited*: Option[bool]
366+
canManageChat*: Option[bool]
341367
canPostMessages*: Option[bool]
342368
canEditMessages*: Option[bool]
343369
canDeleteMessages*: Option[bool]
370+
canManageVoiceChats*: Option[bool]
344371
canRestrictMembers*: Option[bool]
345372
canPromoteMembers*: Option[bool]
346373
canChangeInfo*: Option[bool]
@@ -353,6 +380,14 @@ type
353380
canSendOtherMessages*: Option[bool]
354381
canAddWebPagePreviews*: Option[bool]
355382

383+
ChatMemberUpdated* = object of TelegramObject
384+
chat*: Chat
385+
fromUser*: User
386+
date*: int
387+
oldChatMember*: ChatMember
388+
newChatMember*: ChatMember
389+
inviteLink*: Option[ChatInviteLink]
390+
356391
ChatPermissions* = object of TelegramObject
357392
canSendMessages*: Option[bool]
358393
canSendMediaMessages*: Option[bool]
@@ -384,6 +419,8 @@ type
384419
preCheckoutQuery*: Option[PreCheckoutQuery]
385420
poll*: Option[Poll]
386421
pollAnswer*: Option[PollAnswer]
422+
myChatMember*: Option[ChatMemberUpdated]
423+
chatMember*: Option[ChatMemberUpdated]
387424

388425
#------------------
389426
# Game
@@ -752,3 +789,5 @@ type
752789

753790
PassportElementErrorUnspecified* = ref object of PassportElementError
754791
elementHash*: string
792+
793+

telebot.nimble

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

0 commit comments

Comments
 (0)