diff --git a/app/main.py b/app/main.py index 9f562d34..b9ae3d03 100644 --- a/app/main.py +++ b/app/main.py @@ -7,7 +7,9 @@ from app.dependencies import ( MEDIA_PATH, STATIC_PATH, templates) from app.routers import ( - agenda, dayview, email, event, invitation, profile, search, telegram) + agenda, dayview, email, event, invitation, profile, search, telegram, + whatsapp + ) from app.telegram.bot import telegram_bot @@ -34,6 +36,7 @@ def create_tables(engine, psql_environment): app.include_router(dayview.router) app.include_router(email.router) app.include_router(invitation.router) +app.include_router(whatsapp.router) app.include_router(search.router) telegram_bot.set_webhook() diff --git a/app/routers/whatsapp.py b/app/routers/whatsapp.py new file mode 100644 index 00000000..76ad0720 --- /dev/null +++ b/app/routers/whatsapp.py @@ -0,0 +1,25 @@ +from fastapi import APIRouter +from typing import Optional +from urllib.parse import urlencode + + +router = APIRouter() + + +@router.get("/whatsapp") +def make_link(phone_number: Optional[str], message: Optional[str]) -> str: + """This function is being used to send whatsapp messages. + It takes a string message and a cell phone number and it returns a link so + we can add it to an html page and send the message to that phone number. + Args: + phone_number (str): Cell phone number to send the message to. + message (str): Message that is going to be sent. + + Returns: + str: Returns a string which contains a link to whatsapp api so we can + send the message via whatsapp. + """ + link = 'https://api.whatsapp.com/send?' + mydict = {'phone': phone_number, 'text': message} + msglink = link + urlencode(mydict) + return {"link": msglink} diff --git a/tests/test_whatsapp.py b/tests/test_whatsapp.py new file mode 100644 index 00000000..a49c53e3 --- /dev/null +++ b/tests/test_whatsapp.py @@ -0,0 +1,43 @@ +from app.routers import whatsapp + + +def test_whatsapp_send(): + # Redirects you directly to the specified contact and the message will + # already be there (or to whatsapp web if the call is from the web) + phone_number = "972536106106" + message = 'Event or a joke or the schedule of one day' + assert whatsapp.make_link(phone_number, message) == { + "link": "https://api.whatsapp.com/send?phone=972536106106&text=Event+" + "or+a+joke+or+the+schedule+of+one+day"} + + +def test_wrong_phone_number(): + # Redirects you to a popup: The phone number shared via a link is incorrect + phone_number = "999999" + message = 'Wrong phone number?' + assert whatsapp.make_link(phone_number, message) == { + "link": "https://api.whatsapp.com/send?phone=999999&text=Wrong+phone+" + "number%3F"} + + +def test_no_message(): + # Redirects to whatsapp of the specified number. Write your own message. + phone_number = "972536106106" + message = '' + assert whatsapp.make_link(phone_number, message) == { + "link": "https://api.whatsapp.com/send?phone=972536106106&text="} + + +def test_no_number(): + # Redirects to whatsapp window. Choose someone from your own contact list. + phone_number = "" + message = 'Which phone number?' + assert whatsapp.make_link(phone_number, message) == { + "link": "https://api.whatsapp.com/send?phone=&text=Which+phone+" + "number%3F"} + + +def test_end_to_end_testing(client): + resp = client.get('/whatsapp?phone_number=972536106106&message=testing') + assert resp.ok + assert resp.json