-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 9: refactor: commands in /commands * 9: refactor: services * 9: refactor: auth service * 9: refactor: moved order notification into order service * 9: docs: comments and readme updated
- Loading branch information
Showing
15 changed files
with
234 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// --- imports | ||
const { Composer } = require("telegraf"); | ||
// --- services | ||
const { checkAdmin } = require("../services/auth"); | ||
// --- helpers | ||
const handleError = require("../helpers/errors"); | ||
|
||
const admin = Composer.command("admin", async (ctx) => { | ||
try { | ||
// look up id in admin collection | ||
const admin = await checkAdmin(ctx); | ||
if (!admin) return; // is not admin | ||
|
||
// is admin | ||
ctx.telegram.sendMessage(ctx.chat.id, `Du bist ein Admin!`, {}); | ||
} catch (err) { | ||
handleError(err, bot, ctx, "admin"); | ||
} | ||
}); | ||
|
||
module.exports = Composer.compose([admin]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// --- imports | ||
const { Composer } = require("telegraf"); | ||
// --- services | ||
const { checkAndCreateUser } = require("../services/auth"); | ||
const { getMenu, generateMenuString } = require("../services/menus"); | ||
// --- helpers | ||
const handleError = require("../helpers/errors"); | ||
|
||
const start = Composer.command("start", async (ctx) => { | ||
try { | ||
// get user object or create a new one | ||
const user = await checkAndCreateUser(ctx); | ||
|
||
// respond with welcome message | ||
ctx.telegram.sendMessage( | ||
ctx.chat.id, | ||
`Yo moin, ${user.first_name}! Willkommen beim Pizza Tuesday Bot!`, | ||
{} | ||
); | ||
} catch (err) { | ||
handleError(err, ctx, "start"); | ||
} | ||
}); | ||
|
||
const hunger = Composer.command("hunger", async (ctx) => { | ||
try { | ||
// get menu from database | ||
const menu = await getMenu(); | ||
|
||
// no menu is found -> return message that no menu is available | ||
if (!menu) | ||
return ctx.telegram.sendMessage( | ||
ctx.chat.id, | ||
"Aktuell ist noch kein Menü für den nächsten Pizza Tuesday verfügbar. Bestellungen können leider noch nicht aufgegeben werden." | ||
); | ||
|
||
// menu is found -> start order wizard | ||
await ctx.scene.enter("ORDER_WIZARD_SCENE_ID", { | ||
options: menu.options, | ||
}); | ||
} catch (err) { | ||
handleError(err, ctx, "hunger"); | ||
} | ||
}); | ||
|
||
const menu = Composer.command("menu", async (ctx) => { | ||
try { | ||
// get menu from database | ||
const menu = await getMenu(); | ||
|
||
// no menu is found -> return message that no menu is available | ||
if (!menu) | ||
return ctx.telegram.sendMessage( | ||
ctx.chat.id, | ||
"Aktuell ist noch kein Menü für den nächsten Pizza Tuesday verfügbar. Auch bestellungen können leider noch nicht aufgegeben werden." | ||
); | ||
|
||
// menu is found -> generate menu string and send it to user | ||
const menuString = generateMenuString(menu.options); | ||
ctx.telegram.sendMessage(ctx.chat.id, menuString, { | ||
parse_mode: "MarkdownV2", | ||
}); | ||
} catch (err) { | ||
handleError(err, ctx, "menu"); | ||
} | ||
}); | ||
|
||
module.exports = Composer.compose([start, hunger, menu]); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
const { adminErrorNotification } = require("./notifications"); | ||
// --- services | ||
const { adminErrorNotification } = require("../services/notifications"); | ||
|
||
const handleError = async (err, bot, ctx, command) => { | ||
const handleError = async (err, ctx, command) => { | ||
// log to console | ||
console.log(err); | ||
bot.telegram.sendMessage( | ||
|
||
// inform user | ||
ctx.telegram.sendMessage( | ||
ctx.chat.id, | ||
`Da scheint wohl etwas schiefgelaufen zu sein, der Bot konnte die Anfrage leider nicht verarbeiten. Bitte versuche es später noch einmal.`, | ||
{} | ||
); | ||
await adminErrorNotification(bot, err, command); | ||
|
||
// inform admins | ||
await adminErrorNotification(ctx, err, command); | ||
}; | ||
|
||
module.exports = handleError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.