13.08.2020, 14:56
First you need to retrieve the image using http request.
Then you can send the image using this example. filedata variable must contain image data as a string, change PUT_CHAT_ID_HERE and PUT_BOT_TOKEN_HERE placeholders
Then you can send the image using this example. filedata variable must contain image data as a string, change PUT_CHAT_ID_HERE and PUT_BOT_TOKEN_HERE placeholders
Code:
require('ssl.https')
boundary = os.date('%d%m%Y%H%M%S')
filedata = '...' -- snapshot image data as a string
params = {
{
name = 'chat_id',
value = 'PUT_CHAT_ID_HERE',
},
{
name = 'photo',
filename = 'snapshot.jpg',
ctype = 'image/jpeg',
value = filedata,
}
}
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/PUT_BOT_TOKEN_HERE/sendPhoto',
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))