Skip to content
Snippets Groups Projects

RITS

Merged Lukas Liesenberg requested to merge RITS into master
Files
13
src/bot.ts 0 → 100644
+ 154
0
import dotenv from 'dotenv'
import TelegramBot from 'node-telegram-bot-api'
import moment from 'moment-timezone'
import { refreshAccessToken, requestCanteens, requestMenu, capitalize } from './utils.js'
import { MealType, type MenuOfTheDayResponse } from './responses.js'
moment.tz.setDefault('Europe/Berlin')
moment.locale('de-DE')
dotenv.config()
const bot: TelegramBot = new TelegramBot(process.env.TELEGRAM_TOKEN ?? '', { polling: true })
let accessToken: string = ''
let awaitingCanteenInput: boolean = false
let savedFilter: string = ''
let savedDate: Date = new Date(moment.now())
const mealTypeEmojis: Record<string, string> = { // eslint-disable-line
pig: '🐷',
poultry: '🐔',
beef: '🐮',
fish: '🐟',
vegetarian: '🌱',
vegan: '🥑',
lamb: '🐑',
deer: '🦌'
}
const possibleCommands: string[] = [
'heute',
'morgen',
'tag',
'alles',
'vegan',
'vegetarisch',
'license'
]
const possibleCommandsRegex: RegExp = new RegExp(`\\/(${possibleCommands.join('|')})((?: +\\w+)*)`, 'g')
accessToken = await refreshAccessToken()
const canteens: string[] = (await requestCanteens(accessToken))
.map(c => c
.replace('ue', 'ü')
.replace('strasse', 'straße'))
bot.onText(possibleCommandsRegex, (msg, match): void => {
void (async (): Promise<void> => {
if (match !== null) {
switch (match[1]) {
case 'heute':
await bot.sendMessage(msg.chat.id, 'heute')
savedDate = new Date(moment.now())
if (match[2] === '') {
awaitingCanteenInput = true
await requestCanteenName(msg)
} else if (Object.keys(MealType)
.map(m => m.toLowerCase())
.includes(match[2]
.trim()
.split(' ')[0]
.toLowerCase())) {
awaitingCanteenInput = true
savedFilter = match[2].trim().toLowerCase()
await requestCanteenName(msg)
} else if (canteens
.map(c => c.toLowerCase())
.filter(c => c.includes(match[2].trim().split(' ')[0].toLowerCase()))
.length !== 0) {
const canteen: string = canteens
.map(c => c.toLowerCase())
.filter(c => c.includes(match[2].trim().split(' ')[0].toLowerCase()))[0]
.replace('ü', 'ue')
.replace('ß', 'ss')
if (match[2].trim().split(' ')[1] !== undefined) {
savedFilter = match[2].trim().split(' ')[1].toLowerCase()
}
if (savedFilter !== '') {
await sendMenu(capitalize(canteen), savedDate, savedFilter)
savedFilter = ''
} else {
await sendMenu(capitalize(canteen), savedDate)
}
}
break
case 'morgen':
await bot.sendMessage(msg.chat.id, 'morgen')
if (match[2] === '') {
awaitingCanteenInput = true
await requestCanteenName(msg)
}
break
case 'vegan':
await bot.sendMessage(msg.chat.id, 'vegan')
break
case 'vegetarisch':
await bot.sendMessage(msg.chat.id, 'vegetarisch')
break
case 'alles':
await bot.sendMessage(msg.chat.id, 'alles')
break
case 'license':
await bot.sendMessage(msg.chat.id, 'This bot is FOSS! https://git.rwth-aachen.de/yum/mensa_bot')
break
}
}
})()
})
const requestCanteenName = async (msg: TelegramBot.Message): Promise<void> => {
await bot.sendMessage(msg.chat.id, 'Bitte Mensanamen auswählen (oder einfach beim nächsten mal dazu schreiben :))',
{
reply_to_message_id: msg.message_id,
reply_markup: {
keyboard: canteens.map(c => ([{ text: 'Mensa ' + c }])),
one_time_keyboard: true
}
})
}
bot.onText(new RegExp(`${canteens.map(c => `Mensa ${c}`).join('|')}`), (msg, match): void => {
void (async (): Promise<void> => {
if (awaitingCanteenInput && match !== null) {
awaitingCanteenInput = false
const canteenReconverted: string = match[0].replace('Mensa ', '').replace('ü', 'ue').replace('ß', 'ss')
if (savedFilter !== '') {
await sendMenu(canteenReconverted, savedDate, savedFilter)
savedFilter = ''
savedDate = new Date(moment.now())
} else {
await sendMenu(canteenReconverted, savedDate)
savedDate = new Date(moment.now())
}
}
})()
})
const sendMenu = async (canteen: string, date: Date, filter?: string): Promise<void> => {
const menu: MenuOfTheDayResponse = (await requestMenu(accessToken, canteen, date))[0]
if (filter !== undefined) {
console.log(menu.menus.filter(m => m.mealTypes.map(t => t.toString().toLowerCase()).includes(filter)))
} else {
console.log(menu.menus)
}
}
Loading