Skip to content

Instantly share code, notes, and snippets.

@Utopiah
Last active March 25, 2022 06:53
Show Gist options
  • Save Utopiah/80a7dc5935d9fb21b1c12d26bdc8ef5d to your computer and use it in GitHub Desktop.
Save Utopiah/80a7dc5935d9fb21b1c12d26bdc8ef5d to your computer and use it in GitHub Desktop.
const TelegramBot = require('node-telegram-bot-api');
const { execSync } = require('child_process');
// replace the value below with the Telegram token you receive from @BotFather
const token = '123456789:ABCDEFGHIJKLMNOP';
const dlpath = 'https://api.telegram.org/file/bot'+token+'/'
// /home/root/bin/adddoc is an adaptation of addWithMetadataIfNew with exit 0 on new add
// from https://gist.github.com/Utopiah/e2d5c944bbd632e3ae0530e602977f45
// epubTemplate is based on https://gist.github.com/Utopiah/6728a2477de1c09be4150e43c439e0dd
// minimalist ePub with just what's necessary to be usable.
// see https://gist.github.com/Utopiah/119f5c96fae048609a2091f7f3d81f53
// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, {polling: true});
bot.onText(/^ls$/, (msg, match) => {
// status check
const chatId = msg.chat.id;
var ls = execSync("ls /home/root/.local/share/remarkable/xochitl/ -lrth | tail -1").toString();
console.log(ls)
bot.sendMessage(chatId, ls)
});
bot.onText(/http.*/, (msg, match) => {
const chatId = msg.chat.id;
const resp = match[0]; // the captured "whatever"
console.log(resp)
mpdf = resp.match(".pdf$")
mepub = resp.match(".epub$")
if ( (mpdf && mpdf.length) || (mepub && mepub.length) ){
console.log("pdf or epub")
addFromURL(resp)
var filename = resp.replace(/.*\//,'')
bot.sendMessage(chatId, "added original as "+filename)
} else {
console.log("epub to generate")
var filename = String( Date.now() )
var cpdir = execSync("cp -r epubTemplate/ " + filename, {cwd: "/home/root/"} ).toString();
var wget = execSync("wget -O OEBPS/content.xhtml " + resp, {cwd: "/home/root/"+filename} ).toString();
var makeepub = execSync("7z a -r ../" + filename + ".epub mimetype META-INF/ OEBPS/", {cwd: "/home/root/"+filename} ).toString();
var add = execSync("/home/root/bin/adddoc " + filename + ".epub", {cwd: "/home/root/"}).toString();
var restartxochtil = execSync("systemctl restart xochitl").toString();
bot.sendMessage(chatId, "added as generated ePub "+filename)
}
});
// Listen for any kind of message. There are different kinds of
// messages.
bot.on('message', (msg) => {
const chatId = msg.chat.id;
// send a message to the chat acknowledging receipt of their message
// bot.sendMessage(chatId, 'Received your message');
});
bot.on('photo', (msg) => {
const chatId = msg.chat.id;
console.log('photo', msg)
// probably same behavior as documents. Could become sleep screens.
});
bot.on('document', (msg) => {
const chatId = msg.chat.id;
console.log('doc', msg)
bot.getFile(msg.document.file_id).then( r => {
addFromURL( dlpath+r.file_path )
var filename = r.file_path.replace(/.*\//,'')
bot.sendMessage(chatId, "added as "+filename)
})
});
function addFromURL(resp){
var wget = execSync("wget -nv " + resp, {cwd: "/home/root/"} ).toString();
var filename = resp.replace(/.*\//,'')
var add = execSync("/home/root/bin/adddoc " + filename, {cwd: "/home/root/"}).toString();
var restartxochtil = execSync("systemctl restart xochitl").toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment