09.03.2021, 07:08
Here's a working example. Change token and chat_id. filedata is a string containing your CSV data.
Code:
require('ssl.https')
token = '9876543210:abcdefgh'
chat_id = '1234567890'
filedata = [[
col_a,col_b,col_c
1,2,3
4,5,6
]]
params = {
{
name = 'chat_id',
value = chat_id,
},
{
name = 'document',
filename = 'report.csv',
ctype = 'text/csv',
value = filedata,
}
}
boundary = os.date('%d%m%Y%H%M%S')
body = { '--' .. boundary }
for _, param in ipairs(params) do
line = string.format('Content-Disposition: form-data; name=%q', param.name)
if param.filename then
line = string.format('%s; filename=%q', line, param.filename)
end
body[ #body + 1 ] = line
if param.ctype then
body[ #body + 1 ] = string.format('Content-Type: %s', param.ctype)
end
body[ #body + 1 ] = ''
body[ #body + 1 ] = param.value
body[ #body + 1 ] = '--' .. boundary
end
-- last boundary
body[ #body ] = body[ #body ] .. '--'
-- empty line at the end
body[ #body + 1 ] = ''
bodydata = table.concat(body, '\r\n')
resp = {}
log(
ssl.https.request({
url = 'https://api.telegram.org/bot' .. token .. '/sendDocument',
sink = ltn12.sink.table(resp),
method = 'POST',
source = ltn12.source.string(bodydata),
headers = {
['content-length'] = #bodydata,
['content-type'] = 'multipart/form-data; boundary=' .. boundary
}
})
)
log(table.concat(resp))