This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (52 loc) · 1.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const util = require('util')
const fs = require('fs')
const request = require('request-promise-native')
const exec = util.promisify(require('child_process').exec)
const writeFile = util.promisify(fs.writeFile)
const TelegramBot = require('node-telegram-bot-api');
const botToken = process.env.BOT_TOKEN;
const bot = new TelegramBot(botToken, {polling: true});
bot.on('message', async (msg) => {
console.log(msg);
const chatId = msg.chat.id;
if (msg.text === '/start') {
bot.sendMessage(chatId, 'Hi! Ich bin der ChaosPrinterBot. Ich drucke deine Bilder aus. Plz no spam');
} else {
if (msg.document) {
bot.sendMessage(chatId, 'Nice Dokument bekommen!');
const file = await getFile(msg.document)
await printLabel(file)
} else if (msg.photo) {
bot.sendMessage(chatId, 'Nice Foto bekommen!');
const file = await getFile(msg.photo[0])
await printLabel(file)
} else if(msg.text) {
bot.sendMessage(chatId, 'Nice Nachricht bekommen!');
const file = await generateLabelFromText(msg.text)
await printLabel(file)
} else {
bot.sendMessage(chatId, 'Ich weiß leider nicht wie ich damit umgehen soll :(');
}
}
});
async function getFile(file) {
// TODO: Use bot.getFileStrem insted of request end the telegram bot endpoint
const filePathRequest = JSON.parse(await request(`https://api.telegram.org/bot${botToken}/getFile?file_id=${file.file_id}`))
const filePath = filePathRequest.result.file_path
const fileReq = await request({
uri: `https://api.telegram.org/file/bot${botToken}/${filePath}`,
method: 'GET',
encoding: 'binary',
headers: {
"Content-type": file.mime_type
}
})
return Buffer.from(fileReq, 'binary')
}
async function generateLabelFromText(text) {
return null
}
async function printLabel (file) {
await writeFile('printFile', file)
await exec('~/.local/bin/brother_ql -b linux_kernel -m QL-810W -p file:///dev/usb/lp0 print printFile -l 62 -d');
}