Skip to content

Commit

Permalink
Adding daily overview for bot
Browse files Browse the repository at this point in the history
  • Loading branch information
nahakiole committed Nov 17, 2020
1 parent d2e91be commit bbbf30b
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
A flatastic telegram bot that shows the tasks and things on the shopping list in a telegram chat.

## How to get started
- Create .env file with a BOT_TOKEN and FLATASTIC_TOKEN environment variable.
- Create .env file with a BOT_TOKEN, FLATASTIC_TOKEN and TELEGRAM_GROUP environment variable.
- Run ```node bot.js```

## Planned features:
Expand Down
94 changes: 85 additions & 9 deletions bot.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
const {Telegraf} = require('telegraf')

const {Flatastic} = require('./flatastic.js')
const LocalSession = require('telegraf-session-local')

require('dotenv').config()

let flatastic = new Flatastic(process.env.FLATASTIC_TOKEN)

const bot = new Telegraf(process.env.BOT_TOKEN)

bot.use((new LocalSession({database: 'example_db.json'})).middleware())

bot.start((ctx) => ctx.reply('Welcome!'))
bot.help((ctx) => ctx.reply('Send me a sticker'))

Expand All @@ -16,36 +20,108 @@ flatastic.getInformation(function (data) {
let user = data.flatmates[key];
users[user.id] = user;
}
})

var cron = require('node-cron');

cron.schedule('00 15 * * *', () => {

flatastic.getTaskList(function (data) {
var tasks = "";
data = data.sort(function (x, y) {
let n = x.currentUser - y.currentUser;
if (n !== 0) {
return n;
}

return x.timeLeftNext - y.timeLeftNext;
});
let lastUser = 0;
for (const dataKey in data) {
let task = data[dataKey];

if (task.currentUser !== lastUser) {
tasks += "\n<b>" + users[task.currentUser].firstName + "</b>\n";
}

var daysUntilTask = (task.timeLeftNext / 60 / 60 / 24);
var passed = 'heute';
if (daysUntilTask < 0) {
passed = "vor " + Math.ceil(Math.abs(daysUntilTask)) + " Tag/en"
}
if (daysUntilTask > 1) {
continue;
}
tasks += task.title + " "
tasks += passed + " fällig\n";

lastUser = task.currentUser;
}

bot.telegram.sendMessage(process.env.TELEGRAM_GROUP, tasks, {
parse_mode: 'HTML'
})

})

})

cron.schedule('0 12 * * *', () => {
flatastic.getShoppingList(function (data) {
var output = "";
data = data.filter(function (a) {
return a.bought === 0;
})
if (data.length === 0) {

} else {
output = "Momentan ist folgendes auf der Einkaufsliste:\n";
for (const dataKey in data) {
let item = data[dataKey];
if (item.bought) {
continue;
}

output += item.itemName + " hinzugefügt von " + users[item.inserterId].firstName + "\n"
}
bot.telegram.sendMessage(process.env.TELEGRAM_GROUP, output)
}

})
});

bot.hears(/einkaufsliste|ichoufe|einkaufen|kaufen|shopping/i, (ctx) => {
console.dir(ctx.update.message.chat)
flatastic.getShoppingList(function (data) {
var output = "";
data = data.filter(function (a){
data = data.filter(function (a) {
return a.bought === 0;
})
console.log(data)
if (data.length === 0){
if (data.length === 0) {
output = "Die Einkaufsliste ist leer."
}
else {
} else {
output = "<b>Einkaufsliste</b>\n";
for (const dataKey in data) {
let item = data[dataKey];
if (item.bought){
if (item.bought) {
continue;
}

output += item.itemName + " hinzugefügt von " + users[item.inserterId].firstName +"\n"
output += item.itemName + " hinzugefügt von " + users[item.inserterId].firstName + "\n"
}
}

ctx.replyWithHTML(output);
})
});

bot.hears(/counter/i, (ctx, next) => {
ctx.session.counter = ctx.session.counter || 0
ctx.session.counter = ctx.session.counter * 2
ctx.replyWithMarkdown(`Counter updated, new value: \`${ctx.session.counter}\``)
return next()
})

bot.hears(/task|aufgabe|ämtli/i, (ctx) => {

Expand All @@ -63,8 +139,8 @@ bot.hears(/task|aufgabe|ämtli/i, (ctx) => {
for (const dataKey in data) {
let task = data[dataKey];

if (task.currentUser !== lastUser){
tasks += "\n<b>"+users[task.currentUser].firstName+ "</b>\n";
if (task.currentUser !== lastUser) {
tasks += "\n<b>" + users[task.currentUser].firstName + "</b>\n";
}

tasks += task.title + " "
Expand All @@ -78,7 +154,7 @@ bot.hears(/task|aufgabe|ämtli/i, (ctx) => {
passed = "in " + Math.ceil(Math.abs(daysUntilTask)) + " Tag/en"
}

tasks += passed +" fällig\n";
tasks += passed + " fällig\n";

lastUser = task.currentUser;
}
Expand Down
76 changes: 76 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
"main": "bot.js",
"dependencies": {
"dotenv": "^8.2.0",
"node-cron": "^2.0.3",
"nodemon": "^2.0.4",
"request": "^2.88.2",
"telegraf": "^3.38.0"
"telegraf": "^3.38.0",
"telegraf-session-local": "^2.0.0"
},
"devDependencies": {},
"scripts": {
Expand All @@ -19,5 +21,11 @@
"telegraf"
],
"author": "Robin Glauser",
"license": "MIT"
"license": "MIT",
"nodemonConfig": {
"ignore": [
"example_db.json"
],
"delay": "2500"
}
}

0 comments on commit bbbf30b

Please sign in to comment.