09.03.2021, 08:42
(09.03.2021, 07:08)admin Wrote: 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))
thank you admin for your time
I want to insert the log object script such as this one
I tried but I still not tough in LUA
logtime = os.time() - 60 * 60
-- Список объектов по id
objects = {}
-- получаем объекты по которым ведется запись действий
query = 'SELECT address, datatype, name FROM objects WHERE disablelog=0'
for _, object in ipairs(db:getall(query)) do
objects[ tonumber(object.address) ] = {
datatype = tonumber(object.datatype),
name = tostring(object.name or ''),
}
end
-- csv буфер
buffer = {'"date","address","name","value"'}
-- получаем список действий по объектам
query = 'SELECT src, address, datahex, logtime, eventtype FROM objectlog WHERE logtime >= ? ORDER BY id DESC'
for _, row in ipairs(db:getall(query, logtime)) do
object = objects[ tonumber(row.address) ]
-- находим соответствие между объектом и типом события – запись по групповому адресу
if object and row.eventtype == 'write' then
datatype = object.datatype
-- проверяем что тип данных установлен
if datatype then
-- раскодируем данные
data = knxdatatype.decode(row.datahex, datatype)
-- удаляем null символы из char/string типов
if datatype == dt.char or datatype == dt.string then
data = data:gsub('%z+', '')
-- конвертируем date в формат DD.MM.YYYY
elseif datatype == dt.date then
data = string.format('%.2d.%.2d.%.2d', data.day, data.month, data.year)
-- time в HH:MM:SS
elseif datatype == dt.time then
data = string.format('%.2d:%.2d:%.2d', data.hour, data.minute, data.second)
end
else
data = ''
end
-- форматируем csv колонки
logdate = os.date('%Y.%m.%d %H:%M:%S', row.logtime)
csv = string.format('%q,%q,%q,%q', logdate, knxlib.decodega(row.address), object.name, tostring(data))
-- добавляем в buffer данные
table.insert(buffer, csv)
end
end
Best Regards,