Skip to content

Commit

Permalink
adds sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Mattiazzi committed Jan 23, 2023
1 parent c5f4170 commit ff572f0
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 790 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
TELEGRAM_TOKEN=
TELEGRAM_TOKEN=
SENTRY_URL=
37 changes: 16 additions & 21 deletions index.ts
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()
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
"typescript": "^4.9.4"
},
"dependencies": {
"@sentry/node": "^7.32.1",
"@sentry/tracing": "^7.32.1",
"@types/axios": "^0.14.0",
"axios": "^1.2.3",
"dotenv": "^16.0.3",
Expand All @@ -44,4 +46,4 @@
"ts-node": "^10.9.1",
"tsc": "^2.0.4"
}
}
}
20 changes: 18 additions & 2 deletions src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { config } from "dotenv"
import { Bot } from "grammy"
import { retrieveAvailable } from "./availability"
import { GOODBYE } from "./constants"
import { addSubscription, removeSubscriptions } from "./db"
import { Province } from "./types"

config()

const bot = new Bot(process.env.TELEGRAM_TOKEN as string)

Expand All @@ -24,4 +26,18 @@ export function onLeaveMember(cb: (chatId: number) => Promise<void>) {

export function startBot() {
bot.start()
}

export async function handleGoodbye(chatId: number) {
await removeSubscriptions(chatId)
return sendMessage(chatId, "Ciao! Spero tu sia riucitə a prenotare! Per iscriverti di nuovo, scrivi una provincia.")
}

export async function handleSubscription(chatId: number, province: Province) {
await addSubscription(chatId, province.code)
await sendMessage(chatId, `Fatto! Inviami il nome di altre province, oppure scrivimi '${GOODBYE}' per cancellarti.\nEcco lo stato attuale per questa provincia:`)
const availables = await retrieveAvailable(province.code)
for (const available of availables) {
await sendMessage(chatId, `C'è posto a ${available.city} - ${available.address}\nApri: ${available.url}`)
}
}
1 change: 1 addition & 0 deletions src/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export async function check(): Promise<void> {
const available = await retrieveAvailable(province)
const currentlyAvailableDescriptions = currentlyAvailable.map(c => c.description)
const difference = available.filter(a => !currentlyAvailableDescriptions.includes(a.description))
console.log("difference", difference)
const shouldSend = Boolean(currentlyAvailableByProvince[province])
if (shouldSend) {
for (const telegramId of telegramIds) {
Expand Down
Loading

0 comments on commit ff572f0

Please sign in to comment.