First Live Version
This commit is contained in:
commit
644c0ef018
2 changed files with 204 additions and 0 deletions
3
readme.md
Normal file
3
readme.md
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Dieser Telegram Bot kann das aktuelle Video der ["📰 Tagesschau in 100 Sekunden"](http://www.tagesschau.de/100sekunden/) zu gewünschten Uhrzeiten 🕒 oder auf Abruf versenden.
|
||||
|
||||
Host: [webtask](https://webtask.io)
|
||||
201
webtask.js
Normal file
201
webtask.js
Normal file
|
|
@ -0,0 +1,201 @@
|
|||
/**
|
||||
* @param context {WebtaskContext}
|
||||
*/
|
||||
module.exports = function(context, cb) {
|
||||
|
||||
const axios = require('axios');
|
||||
const TelegramBot = require('node-telegram-bot-api');
|
||||
const bot = new TelegramBot(context.secrets.bot_token);
|
||||
//bot.setWebHook('https://wt-fb95a52ccc1baaa2f9876a683e6b1325-0.sandbox.auth0-extend.com/tages100');
|
||||
|
||||
/*
|
||||
start - Willkommens Nachticht
|
||||
news - Aktuelles Tagesschau in 100 Sekunden Video
|
||||
list - Auflistung Ihrer Abos
|
||||
add - Hinzufügen eines täglichen News Abos
|
||||
remove - Löscht ein Abo
|
||||
*/
|
||||
|
||||
bot.on('message', function onMessage(msg) {
|
||||
// console.log('message', msg);
|
||||
|
||||
if (msg.text.match(/\/news/g) !== null) {
|
||||
return sendLatestVideo(msg.chat.id);
|
||||
} else if (msg.text.match(/\/list/g) !== null) {
|
||||
return listAbo(msg.chat.id);
|
||||
} else if (msg.text.match(/\/add/g) !== null) {
|
||||
return addAbo(msg.chat.id, msg.text);
|
||||
} else if (msg.text.match(/\/remove/g) !== null) {
|
||||
return removeAbo(msg.chat.id, msg.text);
|
||||
} else if (msg.text.match(/\/start/g) !== null) {
|
||||
return help(msg.chat.id);
|
||||
} else if (msg.text.match(/\/help/g) !== null) {
|
||||
return help(msg.chat.id);
|
||||
}
|
||||
return bot.sendMessage(msg.chat.id, 'Ich konnte Ihre Eingabe nicht verarbeiten. Mit dem Befehl /help können Sie sich alle Befehle anzeigen lassen.');
|
||||
|
||||
});
|
||||
|
||||
function sendLatestVideo(chatId) {
|
||||
axios.get('http://www.tagesschau.de/100sekunden/index.html').then(function (response) {
|
||||
// parse src url from html
|
||||
response.data.replace(/\"/g, `\\`);
|
||||
// const regex = /download.media.tagesschau.de\/video\/\d{4}\/\d{4}\/TV-\d{8}-\d{4}-\d{4}.\w{4,}.h264.mp4/g;
|
||||
// sm m l xl
|
||||
const regex = /download.media.tagesschau.de\/video\/\d{4}\/\d{4}\/TV-\d{8}-\d{4}-\d{4}.websm.h264.mp4/g;
|
||||
const found = response.data.match(regex);
|
||||
// console.log(found[0]);
|
||||
|
||||
// build datestrings
|
||||
const a = found[0].split('TV-')[1].split('-');
|
||||
const date = a[0].slice(6) + "." + a[0].slice(4, 6) + "." + a[0].slice(0, 4);
|
||||
const time = a[1].slice(0, 2) + ":" + a[1].slice(-2);
|
||||
|
||||
// send video
|
||||
bot.sendVideo(chatId, 'https://' + found[0], {
|
||||
supports_streaming: true,
|
||||
caption: `Tagesschau in 100 Sekunden vom ${date} um ${time}.`
|
||||
});
|
||||
// bot.sendMessage(chatId, `Tagesschau in 100 Sekunden vom ${date} um ${time}. https://${found[0]}`);
|
||||
});
|
||||
}
|
||||
|
||||
// function startAbo(chatId, text) {
|
||||
// let time = text.slice(9).replace(':', '').trim();
|
||||
// time = time.slice(0, 2)+':'+time.slice(-2);
|
||||
// addAbo(chatId, time);
|
||||
// }
|
||||
|
||||
function help(chatId) {
|
||||
const msg = `
|
||||
Willkommen beim Tagesschau in 100 Sekunden Telegram Bot.
|
||||
|
||||
Dieser Bot kann Ihnen zu gewünschten Uhrzeiten 🕒 oder auf Abruf mit aktuellen und kompakten Nachrichten 📰 versorgen.
|
||||
|
||||
Befehle:
|
||||
|
||||
/news - Aktuelles Tagesschau in 100 Sekunden Video
|
||||
/list - Auflistung Ihrer Abonnements
|
||||
/add - Hinzufügen eines täglichen News Abos, z.B. "/add 1800"
|
||||
/remove - Löscht ein Abo, z.B. "/remove 1800"
|
||||
/help - Diese Nachricht
|
||||
|
||||
Die Uhrzeiten für die Abonnements sind in dem Format HHMM anzugeben.
|
||||
|
||||
Dieses Angebot ist inoffiziell und steht nicht mit der Tagesschau in Verbindung.
|
||||
`;
|
||||
bot.sendMessage(chatId, msg)
|
||||
}
|
||||
|
||||
function listAbo(chatId) {
|
||||
context.storage.get(function (error, data) {
|
||||
if (error || data === undefined) {
|
||||
bot.sendMessage(chatId, 'Ihre Abonements konnten leider nicht abgerufen werden.');
|
||||
return cb(error);
|
||||
};
|
||||
console.log('storage list', data)
|
||||
const abos = data.filter(d => d.chatId === chatId)[0];
|
||||
bot.sendMessage(chatId, 'Ihre Abonements: '+abos.times.join(', '));
|
||||
});
|
||||
}
|
||||
|
||||
function addAbo(chatId, time) {
|
||||
time = time.slice(4).trim();
|
||||
context.storage.get(function (error, data) {
|
||||
if (error) {
|
||||
bot.sendMessage(chatId, 'Ihre Abonements konnten leider nicht abgerufen werden.');
|
||||
return cb(error);
|
||||
};
|
||||
if (data === undefined) {
|
||||
data = [];
|
||||
}
|
||||
console.log('storage add', data)
|
||||
// set new abo
|
||||
const chatAbos = data.filter(e => e.chatId === chatId)[0];
|
||||
if (chatAbos) {
|
||||
if (chatAbos.times.indexOf(time) === -1) {
|
||||
chatAbos.times.push(time);
|
||||
bot.sendMessage(chatId, `Neues Abo um ${time}.`);
|
||||
} else {
|
||||
bot.sendMessage(chatId, 'Sie haben schon ein Abo für diese Uhrzeit.');
|
||||
}
|
||||
} else {
|
||||
data.push({
|
||||
chatId: chatId,
|
||||
times: ['1800']
|
||||
});
|
||||
}
|
||||
// save
|
||||
saveStorage(data);
|
||||
});
|
||||
}
|
||||
|
||||
function removeAbo(chatId, time) {
|
||||
time = time.slice(7).trim();
|
||||
context.storage.get(function (error, data) {
|
||||
if (error) {
|
||||
bot.sendMessage(chatId, 'Ihr Abonement konnte nicht gelöscht werden.');
|
||||
return cb(error);
|
||||
};
|
||||
if (data === undefined) {
|
||||
return;
|
||||
}
|
||||
console.log('storage remove', data)
|
||||
// remove abo
|
||||
const chatAbos = data.filter(e => e.chatId === chatId)[0];
|
||||
if (chatAbos) {
|
||||
if (chatAbos.times.indexOf(time) !== -1) {
|
||||
chatAbos.times.splice(chatAbos.times.indexOf(time), 1);
|
||||
bot.sendMessage(chatId, `Ihr Abo um ${time} wurde gelöscht.`);
|
||||
} else {
|
||||
bot.sendMessage(chatId, `Es wurde kein Abo um ${time} gefunden.`);
|
||||
}
|
||||
}
|
||||
// save
|
||||
saveStorage(data);
|
||||
});
|
||||
}
|
||||
|
||||
function saveStorage(data) {
|
||||
// var attempts = 3;
|
||||
context.storage.set(data, function set_cb(error) {
|
||||
if (error) {
|
||||
if (error.code === 409 && attempts--) {
|
||||
// resolve conflict and re-attempt set
|
||||
bot.sendMessage(context.secrets.metg, 'Es gab einen Storage Konflikt.')
|
||||
// data.counter = Math.max(data.counter, error.conflict.counter) + 1;
|
||||
// return context.storage.set(data, set_cb);
|
||||
}
|
||||
return cb(error);
|
||||
}
|
||||
// ...
|
||||
});
|
||||
}
|
||||
|
||||
function checkAbos() {
|
||||
// time
|
||||
const moment = require('moment-timezone');
|
||||
moment.locale('de');
|
||||
const serverTime = moment().tz("Europe/Berlin").format('LT').replace(':','');
|
||||
// storage
|
||||
context.storage.get(function (error, data) {
|
||||
if (error || data === undefined) {
|
||||
return cb(error);
|
||||
};
|
||||
console.log('storage check',serverTime, data);
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
if (data[i].times.indexOf(serverTime) !== -1) {
|
||||
sendLatestVideo(data[i].chatId);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// handle webtask calls
|
||||
if(context.body.update_id) {
|
||||
bot.processUpdate(context.body);
|
||||
} else {
|
||||
checkAbos();
|
||||
}
|
||||
cb(null, null);
|
||||
};
|
||||
Loading…
Reference in a new issue