-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Luca Mattiazzi
committed
Jan 23, 2023
1 parent
c5f4170
commit ff572f0
Showing
6 changed files
with
109 additions
and
790 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
TELEGRAM_TOKEN= | ||
TELEGRAM_TOKEN= | ||
SENTRY_URL= |
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,38 +1,33 @@ | ||
import * as Sentry from "@sentry/node" | ||
import { config } from "dotenv" | ||
import * as cron from "node-cron" | ||
import { onLeaveMember, onMessage, onNewChatMember, sendMessage, startBot } from "./src/bot" | ||
import { handleGoodbye, handleSubscription, onLeaveMember, onMessage, onNewChatMember, sendMessage, startBot } from "./src/bot" | ||
import { check } from "./src/checker" | ||
import { GOODBYE } from "./src/constants" | ||
import { addSubscription, removeSubscriptions } from "./src/db" | ||
import { removeSubscriptions } from "./src/db" | ||
import { findProvince } from "./src/provinces" | ||
|
||
config() | ||
|
||
Sentry.init({ | ||
dsn: process.env.SENTRY_URL, | ||
tracesSampleRate: 1.0, | ||
}) | ||
|
||
onNewChatMember(async (chatId: number) => { | ||
sendMessage(chatId, "Benvenutə! Dimmi a che provincia sei interessatə!") | ||
await sendMessage(chatId, "Benvenutə! Dimmi a che provincia sei interessatə!") | ||
}) | ||
|
||
onMessage(async (chatId: number, text: string) => { | ||
if (text === GOODBYE) { | ||
await removeSubscriptions(chatId) | ||
return sendMessage(chatId, "Ciao! Spero tu sia riucitə a prenotare! Per iscriverti di nuovo, scrivi una provincia.") | ||
} | ||
if (text === GOODBYE) return handleGoodbye(chatId) | ||
const province = findProvince(text) | ||
if (!province) return sendMessage(chatId, "Mi spiace, non conosco questa provincia, riprova!") | ||
try { | ||
await addSubscription(chatId, province.code) | ||
sendMessage(chatId, `Fatto! Inviami il nome di altre province, oppure scrivimi '${GOODBYE}' per cancellarti.`) | ||
} | ||
catch (err) { | ||
console.error(err) | ||
sendMessage(chatId, "Mi spiace, s'è scassato qualcosa, riprova!") | ||
} | ||
await handleSubscription(chatId, province) | ||
}) | ||
|
||
onLeaveMember(async (chatId: number) => { | ||
removeSubscriptions(chatId).then(console.log).catch(console.error) | ||
}) | ||
|
||
cron.schedule("* * * * *", () => { | ||
check() | ||
await removeSubscriptions(chatId) | ||
}) | ||
|
||
check() | ||
cron.schedule("* * * * *", () => check()) | ||
startBot() |
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.