Skip to content

Commit

Permalink
Sending start up message to slack
Browse files Browse the repository at this point in the history
  • Loading branch information
renekann committed Jun 3, 2023
1 parent 651c3fc commit b004b40
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
34 changes: 34 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,35 @@ async function checkAppointmentAvailability() {
}
}

export async function sendInitialSlackNotification(): Promise<void> {
const slackWebhookUrl = process.env.SLACK_WEBHOOK_URL;
const bookingUrl = process.env.DOCTOR_BOOKING_URL;
const timespan = Number(process.env.TIMESPAN_DAYS || '0');
const schedule = process.env.SCHEDULE || '* * * * *';

if (!slackWebhookUrl) {
throw new Error('The SLACK_WEBHOOK_URL environment variable is not defined.');
}

if (!bookingUrl) {
throw new Error('The DOCTOR_BOOKING_URL environment variable is not defined.');
}

const message = {
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: `:robot_face: The Doctolib Appointment Finder has started checking every ${schedule} for available appointments within the next ${timespan} days. You will receive notifications when appointments become available. The booking link is: ${bookingUrl}`
}
},
]
};

await axios.post(slackWebhookUrl, message);
}

if (typeof jest !== 'undefined') {
console.log('Running in Jest environment');
} else {
Expand All @@ -151,6 +180,11 @@ if (typeof jest !== 'undefined') {
console.log(`Scheduling appointment availability check every ${schedule}.`);
try {
task = cron.schedule(schedule, checkAppointmentAvailability);

sendInitialSlackNotification().catch((error) => {
console.error(`Error while sending initial Slack notification: ${error}`);
});

} catch (error) {
console.error(`Error while scheduling appointment availability check: ${error}`);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "doctolib-appointment-finder",
"version": "1.0.3",
"version": "1.0.4",
"dependencies": {
"@types/node": "^20.2.5",
"@types/node-cron": "^3.0.7",
Expand Down

0 comments on commit b004b40

Please sign in to comment.