Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
itamarshubin committed Mar 18, 2024
2 parents 020afb4 + 72be7ca commit 5262bde
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 36 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# whatsapp-shabbat

this code looks like shit, so feal free to pr me
this code looks like shit, so feel free to pr me

how to use:

1. go to google firebase and get yourself firebaseConfig.
2. put it in the app.
3. the bot is running



my setup is ec2 in aws.
I didn't use Docker, so if you want your project to run on linux (at least ubuntu) you need to install all the packages in the Docker file.
I didn't use Docker, so if you want your project to run on linux (at least ubuntu) you need to install all the packages in the Docker file.
6 changes: 4 additions & 2 deletions src/constants/help-message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BOT_MANAGER } from "./bot-manager"
import { BOT_MANAGER } from "./bot-manager";

export const HELP_MESSAGE: string = `
היי, זה בוט השבתות ויו"ט של דרך חיים, הוא לא יודע לעשות הרבה. זה מה שאפשר לעשות בנתיים:
Expand All @@ -9,5 +9,7 @@ export const HELP_MESSAGE: string = `
מי מעניין - אפשרות לסינון מחזורים שמעניינים אותך על הפקודה 'מי מגיע'
כולם מעניינים אותי - ביטול הסינונים על הפקודה 'מי מגיע'
בעתיד יתווספו פעולות נוספות 😁
עדכן אותי - אפשרות לקבלת עדכונים על משתמש
אל תעדכן - להפסיק לקבל עדכונים על משתמש
אם ישנה בעייה צור קשר עם ${BOT_MANAGER.name} (${BOT_MANAGER.phoneNumber})
` as const;
` as const;
18 changes: 7 additions & 11 deletions src/constants/special-response.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import fs from "fs";
import { Client } from "whatsapp-web.js";

let specialMessages:any;
fs.readFile(
"special-messages.json",
"utf-8",
(err, data) => {
if (err) return null;

specialMessages= JSON.parse(data);
}
);
let specialMessages: any;
fs.readFile("special-messages.json", "utf-8", (err, data) => {
if (err) return null;

specialMessages = JSON.parse(data);
});

export const sendSpecialMessages = (from: string, client: Client) => {
if (specialMessages[from]) client.sendMessage(from, specialMessages[from]);
if (specialMessages && specialMessages[from])
client.sendMessage(from, specialMessages[from]);
};
25 changes: 15 additions & 10 deletions src/firebase/shabbas-manage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import {
import { Message } from "whatsapp-web.js";
import { client } from "../app";
import { ALL_YESHIVA_YEARS } from "../constants/yeshiva-years";
import { updateRelevantUsers } from "../constants/subscription";

const fireStore = getFirestore();
export const fireStore = getFirestore();

const getShabbasDoc = async (): Promise<
QueryDocumentSnapshot<DocumentData>
Expand All @@ -35,7 +36,7 @@ const getShabbasDoc = async (): Promise<
return shabbasDocs.docs[0];
};

const getUserRef = async (msg: Message) => {
export const getUserRef = async (msg: Message) => {
const userRef = (
await getDocs(
query(collection(fireStore, "/users"), where("phone", "==", msg.from))
Expand All @@ -58,6 +59,7 @@ export const addUser = async (msg: Message) => {
await updateDoc(shabbas.ref, { participants: arrayUnion(userRef.ref) });
await client.sendMessage(msg.from, "נהדר! נשמח לראותך.");
}
await updateRelevantUsers(userRef, client, true);
};

export const addAlcoholic = async (msg: Message) => {
Expand Down Expand Up @@ -99,7 +101,8 @@ export const getAlcoholics = async (msg: Message) => {
const userRef = await getUserRef(msg);

const subscribedYears: string[] = userRef.get("subscribedYears");
if (!subscribedYears ||subscribedYears.length === 0) await resetSubscribedYears(msg, userRef);
if (!subscribedYears || subscribedYears.length === 0)
await resetSubscribedYears(msg, userRef);
const alcoholic: DocumentReference<DocumentData>[] =
shabbas.data().alcoholics;
if (!alcoholic) {
Expand Down Expand Up @@ -150,16 +153,17 @@ export const removeUser = async (msg: Message) => {
await updateDoc(shabbas.ref, { participants: arrayRemove(userRef.ref) });
}
await client.sendMessage(msg.from, "טוב נו... פעם הבאה.");
await updateRelevantUsers(userRef, client, false);
};

export const getParticipants = async (msg: Message) => {
const shabbas = await getShabbasDoc();
const userRef = await getUserRef(msg);

const subscribedYears: string[] = userRef.get("subscribedYears");
if (!subscribedYears ||subscribedYears.length === 0) await resetSubscribedYears(msg, userRef);

if (!subscribedYears || subscribedYears.length === 0)
await resetSubscribedYears(msg, userRef);

const participant: DocumentReference<DocumentData>[] =
shabbas.data().participants;
if (!participant) {
Expand Down Expand Up @@ -272,10 +276,9 @@ export const calculateFood = async (msg: Message) => {

export const setRabbi = async (msg: Message) => {
const shabbas = await getShabbasDoc();

await updateDoc(shabbas.ref, {
rabbi: msg.body.substr(msg.body.indexOf(" ") + 1),
});
const rabbi = msg.body.substr(msg.body.indexOf(" ") + 1);
await updateDoc(shabbas.ref, { rabbi });
client.sendMessage(msg.from, `${rabbi} הוגדר`);
};

export const sessionedSubscribers: Record<string, boolean> = {};
Expand Down Expand Up @@ -326,3 +329,5 @@ export const resetSubscribedYears = async (
msg.from
);
};

const alertSubscribers = async (msg: Message) => {};
50 changes: 41 additions & 9 deletions src/message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,50 @@ import {
} from "./firebase/shabbas-manage";
import { HELP_MESSAGE } from "./constants/help-message";
import { sendSpecialMessages } from "./constants/special-response";
import {
createAlertSubscription,
removeAlertSubscription,
sessionedAddSubscribersAlert,
sessionedRemoveSubscribersAlert,
} from "./constants/subscription";

export const messageHandler = async (msg: Message) => {
sendSpecialMessages(msg.from, client);

//prevent interrupt in middle action
if (sessionedSubscribers[msg.from]) {
await addSubscribedYears(msg);
return;
}

if (sessionedAddSubscribersAlert[msg.from]) {
await createAlertSubscription(msg);
return;
}

if (sessionedRemoveSubscribersAlert[msg.from]) {
await removeAlertSubscription(msg);
return;
}

if (msg.body.includes("מי מעניין")) {
await addSubscribedYears(msg);
return;
}

if (
msg.body.includes("עדכן אותי") ||
sessionedAddSubscribersAlert[msg.from]
) {
await createAlertSubscription(msg);
return;
}

if (msg.body.includes("אל תעדכן")) {
await removeAlertSubscription(msg);
return;
}

if (msg.body.startsWith("!new") && (await isAdmin(msg))) {
await addShabbas(msg);
return;
Expand All @@ -38,10 +78,6 @@ export const messageHandler = async (msg: Message) => {
}

if (msg.body.includes("מי מגיע")) {
await client.sendMessage(
msg.from,
"*פיצ'ר חדש!!!* מעכשיו תוכל לערוך אילו מחזורים מעניינים אותך על ידי הפקודה מי מעניין "
);
await getParticipants(msg);
return;
}
Expand All @@ -51,10 +87,6 @@ export const messageHandler = async (msg: Message) => {
return;
}

if (msg.body.includes("מי מעניין") || sessionedSubscribers[msg.from]) {
await addSubscribedYears(msg);
return;
}
if (msg.body.includes("כולם מעניינים אותי")) {
await resetSubscribedYears(msg);
await client.sendMessage(msg.from, " די נו איזה חמוד אתה 🤓");
Expand All @@ -63,7 +95,7 @@ export const messageHandler = async (msg: Message) => {
if (!(await auth(msg))) {
return;
}

if (msg.body.includes("מי מביא אלכוהול")) {
await getAlcoholics(msg);
return;
Expand Down

0 comments on commit 5262bde

Please sign in to comment.