-
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.
Merge pull request #30 from itamarshubin/subscribe-to-alert
Subscribe to alert
- Loading branch information
Showing
6 changed files
with
234 additions
and
28 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,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. |
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 |
---|---|---|
@@ -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]); | ||
}; |
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,173 @@ | ||
import { | ||
arrayUnion, | ||
DocumentData, | ||
getDocFromServer, | ||
QueryDocumentSnapshot, | ||
arrayRemove, | ||
DocumentSnapshot, | ||
updateDoc, | ||
collection, | ||
getDocs, | ||
query, | ||
DocumentReference, | ||
} from "@firebase/firestore"; | ||
|
||
import { Client, Message } from "whatsapp-web.js"; | ||
import { fireStore, getUserRef } from "../firebase/shabbas-manage"; | ||
import { client } from "../app"; | ||
|
||
type User = { phone: string; name: string }; | ||
export const updateRelevantUsers = async ( | ||
userRef: QueryDocumentSnapshot<DocumentData>, | ||
client: Client, | ||
coming: boolean | ||
): Promise<void> => { | ||
const subscribedUsers: DocumentReference<User>[] = | ||
userRef.get("subscribedUsers"); | ||
|
||
subscribedUsers?.forEach(async (user) => { | ||
const userData = (await getDocFromServer<User>(user)).data(); | ||
userData && | ||
client.sendMessage( | ||
userData.phone, | ||
`${userRef.get("name")} ${coming ? "מגיע" : "לא מגיע"} לשבת` | ||
); | ||
}); | ||
}; | ||
|
||
let users: QueryDocumentSnapshot<DocumentData>[] = []; | ||
export const sessionedAddSubscribersAlert: Record< | ||
string, | ||
{ matches: QueryDocumentSnapshot<DocumentData>[] } | ||
> = {}; | ||
export const sessionedRemoveSubscribersAlert: Record< | ||
string, | ||
{ followers: DocumentSnapshot<User>[] } | ||
> = {}; | ||
|
||
//refetch users twice a day and save it in the cache. | ||
//you probably should use some library to fetch user by name. | ||
//but I didn't want to get all the users every time | ||
setInterval(() => { | ||
users = []; | ||
}, 43200000); | ||
|
||
export const removeAlertSubscription = async (msg: Message) => { | ||
if (!users.length) { | ||
const docs = await getDocs(query(collection(fireStore, "/users"))); | ||
docs.forEach((doc) => { | ||
users.push(doc); | ||
}); | ||
} | ||
if (!sessionedRemoveSubscribersAlert[msg.from]) { | ||
const userRef = await getUserRef(msg); | ||
|
||
const followersRef: DocumentReference<User>[] = | ||
userRef.data().subscribedUsers; | ||
const followers = await Promise.all( | ||
followersRef.map((followerRef) => getDocFromServer(followerRef)) | ||
); | ||
|
||
let response = "אלה האנשים שאתה תקבל עליהם עדכונים: \n"; | ||
|
||
followers.forEach((follower, index) => { | ||
response += index + 1 + ". " + String(follower.data()?.name) + "\n"; | ||
}); | ||
|
||
client.sendMessage( | ||
msg.from, | ||
response + " שלח את המספר של מי שאתה רוצה להסיר" | ||
); | ||
sessionedRemoveSubscribersAlert[msg.from] = { followers }; | ||
return; | ||
} | ||
|
||
if (msg.body === "בטל") { | ||
return delete sessionedRemoveSubscribersAlert[msg.from]; | ||
} | ||
|
||
const userRef = await getUserRef(msg); | ||
|
||
const userToRemove = | ||
sessionedRemoveSubscribersAlert[msg.from].followers[Number(msg.body) - 1]; | ||
if (!userToRemove) { | ||
return client.sendMessage( | ||
msg.from, | ||
"הזן מספר תקין או כתוב 'בטל' בשביל להפסיק את הפעולה" | ||
); | ||
} | ||
|
||
await updateDoc(userRef.ref, { | ||
subscribedUsers: arrayRemove(userToRemove.ref), | ||
}); | ||
client.sendMessage( | ||
msg.from, | ||
`הסרנו את ${userToRemove.data()?.name}, לא תקבל יותר עדכונים עליו` | ||
); | ||
delete sessionedRemoveSubscribersAlert[msg.from]; | ||
}; | ||
|
||
export const createAlertSubscription = async (msg: Message) => { | ||
if (!users.length) { | ||
const docs = await getDocs(query(collection(fireStore, "/users"))); | ||
docs.forEach((doc) => { | ||
users.push(doc); | ||
}); | ||
} | ||
if (!sessionedAddSubscribersAlert[msg.from]) { | ||
client.sendMessage( | ||
msg.from, | ||
"כתוב שם (מלא או חלקי) של מישהו שתרצה לקבל עדכון כשהוא מגיע" | ||
); | ||
sessionedAddSubscribersAlert[msg.from] = { matches: [] }; | ||
return; | ||
} | ||
if (msg.body === "בטל") { | ||
return delete sessionedAddSubscribersAlert[msg.from]; | ||
} | ||
|
||
if (!sessionedAddSubscribersAlert[msg.from].matches.length) { | ||
const matches = users.filter((user) => | ||
user.data().name?.includes(msg.body) | ||
); | ||
if (!matches) { | ||
return client.sendMessage( | ||
msg.from, | ||
"לא נמצא משתמש עם שם כזה, הזן שם אחר או 'בטל' כדי להפסיק את הפעולה" | ||
); | ||
} | ||
let response = ""; | ||
matches.forEach((match, index) => { | ||
response += String( | ||
index + | ||
1 + | ||
". " + | ||
match.data().name + | ||
", מחזור: " + | ||
match.data().year + | ||
"\n" | ||
); | ||
}); | ||
|
||
client.sendMessage(msg.from, response); | ||
sessionedAddSubscribersAlert[msg.from].matches = matches; | ||
return; | ||
} | ||
|
||
const userRef = await getUserRef(msg); | ||
|
||
const userToAdd = | ||
sessionedAddSubscribersAlert[msg.from].matches[Number(msg.body) - 1]; | ||
if (!userToAdd) { | ||
return client.sendMessage( | ||
msg.from, | ||
"הזן מספר תקין או כתוב 'בטל' בשביל להפסיק את הפעולה" | ||
); | ||
} | ||
await updateDoc(userRef.ref, { subscribedUsers: arrayUnion(userToAdd.ref) }); | ||
client.sendMessage( | ||
msg.from, | ||
`תקבל הודעה כש${userToAdd.data().name} יעדכן על מצב ההגעה שלו` | ||
); | ||
delete sessionedAddSubscribersAlert[msg.from]; | ||
}; |
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