This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

TELEGRAM APP
#22
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))
Reply


Messages In This Thread
TELEGRAM APP - by Hosutech - 01.06.2020, 11:15
RE: TELEGRAM APP - by Daniel - 01.06.2020, 14:29
RE: TELEGRAM APP - by JRP - 03.06.2020, 17:18
RE: TELEGRAM APP - by JRP - 11.07.2020, 18:47
RE: TELEGRAM APP - by khalil - 04.03.2021, 10:36
RE: TELEGRAM APP - by olegrz - 04.09.2022, 12:32
RE: TELEGRAM APP - by Daniel - 03.06.2020, 18:02
RE: TELEGRAM APP - by JRP - 03.06.2020, 18:27
RE: TELEGRAM APP - by Daniel - 03.06.2020, 18:40
RE: TELEGRAM APP - by JRP - 03.06.2020, 18:57
RE: TELEGRAM APP - by Erwin van der Zwart - 03.06.2020, 21:06
RE: TELEGRAM APP - by olegrz - 06.01.2024, 17:51
RE: TELEGRAM APP - by olegrz - 07.01.2024, 09:09
RE: TELEGRAM APP - by admin - 04.06.2020, 04:36
RE: TELEGRAM APP - by baggins - 04.06.2020, 10:54
RE: TELEGRAM APP - by JRP - 04.06.2020, 07:35
RE: TELEGRAM APP - by admin - 04.06.2020, 11:17
RE: TELEGRAM APP - by phongvucba - 04.08.2020, 09:03
RE: TELEGRAM APP - by Daniel - 04.08.2020, 09:06
RE: TELEGRAM APP - by admin - 04.03.2021, 10:38
RE: TELEGRAM APP - by khalil - 04.03.2021, 10:44
RE: TELEGRAM APP - by khalil - 04.03.2021, 14:18
RE: TELEGRAM APP - by admin - 04.03.2021, 17:17
RE: TELEGRAM APP - by khalil - 08.03.2021, 14:28
RE: TELEGRAM APP - by admin - 09.03.2021, 07:08
RE: TELEGRAM APP - by khalil - 09.03.2021, 08:42
RE: TELEGRAM APP - by admin - 09.03.2021, 08:45
RE: TELEGRAM APP - by khalil - 09.03.2021, 09:24
RE: TELEGRAM APP - by edgars - 09.02.2022, 11:49
RE: TELEGRAM APP - by admin - 05.09.2022, 06:17
RE: TELEGRAM APP - by olegrz - 05.09.2022, 09:04
RE: TELEGRAM APP - by Daniel - 02.02.2023, 18:08
RE: TELEGRAM APP - by David - 26.04.2023, 07:28
RE: TELEGRAM APP - by admin - 26.04.2023, 07:28
RE: TELEGRAM APP - by David - 26.04.2023, 07:33

Forum Jump: