Example of sendPhoto method with pure NodeJS without third-party modules.
const fs = require('fs');
const https = require('https');
const chat_id = '...123...'; // telegram chat id
const botToken = '...aaa...'; // telegram bot token
const imageBuffer = fs.readFileSync('./image.jpg');
const boundary = '----WebKitFormBoundarydxxXNCrBx4FFWOWa';
const contentType = `multipart/form-data; boundary=${boundary}`;
const dataBefore =
`--${boundary}\r\n` +
`Content-Disposition: form-data; name="chat_id"\r\n` +
`\r\n${chat_id}\r\n` +
`--${boundary}\r\n` +
`Content-Disposition: form-data; name="photo"; filename="photo.jpg"\r\n` +
`Content-Type: image/jpeg\r\n` +
`\r\n`;
const dataAfter = `\r\n` + `--${boundary}--\r\n`;
const resultBuffer = Buffer.concat(
[Buffer.from(dataBefore), imageBuffer, Buffer.from(dataAfter)],
Buffer.byteLength(dataBefore) + imageBuffer.length + Buffer.byteLength(dataAfter),
);
https
.request({
hostname: 'api.telegram.org',
port: 443,
path: `/bot${tbotToken}/sendPhoto`,
method: 'POST',
headers: {
'Content-Type': contentType,
// no need to specify content-length, if Buffer body data is created correctly
}
}, (res) => {
const chunks = [];
res.on('data', (chunk) => chunks.push(chunk));
res.on('end', () => {
if (res.statusCode === 200) {
const resText = Buffer.concat(chunks).toString('utf8');
if (resText && res.headers['content-type']) {
if (res.headers['content-type'].includes('application/json')) {
try {
const objResponse = JSON.parse(resText);
console.log(objResponse)
} catch (err) {
console.error(err);
}
} else {
console.error('Unsupported response content type');
}
} else {
console.error(`Empty response ${res.statusCode} ${res.statusMessage} ${res.headers['content-type']}`);
}
} else {
console.error(`${res.statusCode} ${res.statusMessage} ${res.headers['content-type']} ${resText}`);
}
});
})
.on('error', console.error)
.end(resultBuffer);
Concat buffers