ba0f3 / telebot.nim

Async Telegram Bot API Client implement in @Nim-Lang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[REQUEST] Ability to send files from buffer

rupansh opened this issue · comments

Much faster than saving the file first and then sending it!

Example proc(s) -

proc saveBuf*(b: TeleBot, fileUrl: string): (Stream, string) =
    var client = newHttpClient()
    let file = client.get(fileUrl)
    return (file.bodyStream, file.contentType)

proc sendDocument*(b: TeleBot, chatId: int, document: telebot.File, replyToMessageId = 0, forceDoc = false, filename = ""): Future[Message] {.async.} =
    ## send Document from file
    END_POINT("sendDocument")
    var data = newMultipartData()
    data["chat_id"] = $chatId

    if replyToMessageId != 0:
        data["reply_to_message_id"] = $replyToMessageId

    if forceDoc:
        let fileUrl = FILE_URL % @[b.token, document.filePath.get]
        let buf = saveBuf(b, fileUrl)
        data["document"] = (filename, buf[1], buf[0].readAll)
    else:
        data["document"] = document.fileId

    let res = await makeRequest(b, endpoint % b.token, data)
    result = unmarshal(res, Message)

due to limitation of httpclient in stdlib, I will try to patch it first