Skip to content

Commit 6e317e8

Browse files
authored
feat: Add a feature for sharing information with a WhatsApp account (#83)
* feat: Add a feature to share to a WhatsApp account
1 parent bcf470b commit 6e317e8

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

app/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
from app.dependencies import (
88
MEDIA_PATH, STATIC_PATH, templates)
99
from app.routers import (
10-
agenda, dayview, email, event, invitation, profile, search, telegram)
10+
agenda, dayview, email, event, invitation, profile, search, telegram,
11+
whatsapp
12+
)
1113
from app.telegram.bot import telegram_bot
1214

1315

@@ -34,6 +36,7 @@ def create_tables(engine, psql_environment):
3436
app.include_router(dayview.router)
3537
app.include_router(email.router)
3638
app.include_router(invitation.router)
39+
app.include_router(whatsapp.router)
3740
app.include_router(search.router)
3841

3942
telegram_bot.set_webhook()

app/routers/whatsapp.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from fastapi import APIRouter
2+
from typing import Optional
3+
from urllib.parse import urlencode
4+
5+
6+
router = APIRouter()
7+
8+
9+
@router.get("/whatsapp")
10+
def make_link(phone_number: Optional[str], message: Optional[str]) -> str:
11+
"""This function is being used to send whatsapp messages.
12+
It takes a string message and a cell phone number and it returns a link so
13+
we can add it to an html page and send the message to that phone number.
14+
Args:
15+
phone_number (str): Cell phone number to send the message to.
16+
message (str): Message that is going to be sent.
17+
18+
Returns:
19+
str: Returns a string which contains a link to whatsapp api so we can
20+
send the message via whatsapp.
21+
"""
22+
link = 'https://api.whatsapp.com/send?'
23+
mydict = {'phone': phone_number, 'text': message}
24+
msglink = link + urlencode(mydict)
25+
return {"link": msglink}

tests/test_whatsapp.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from app.routers import whatsapp
2+
3+
4+
def test_whatsapp_send():
5+
# Redirects you directly to the specified contact and the message will
6+
# already be there (or to whatsapp web if the call is from the web)
7+
phone_number = "972536106106"
8+
message = 'Event or a joke or the schedule of one day'
9+
assert whatsapp.make_link(phone_number, message) == {
10+
"link": "https://api.whatsapp.com/send?phone=972536106106&text=Event+"
11+
"or+a+joke+or+the+schedule+of+one+day"}
12+
13+
14+
def test_wrong_phone_number():
15+
# Redirects you to a popup: The phone number shared via a link is incorrect
16+
phone_number = "999999"
17+
message = 'Wrong phone number?'
18+
assert whatsapp.make_link(phone_number, message) == {
19+
"link": "https://api.whatsapp.com/send?phone=999999&text=Wrong+phone+"
20+
"number%3F"}
21+
22+
23+
def test_no_message():
24+
# Redirects to whatsapp of the specified number. Write your own message.
25+
phone_number = "972536106106"
26+
message = ''
27+
assert whatsapp.make_link(phone_number, message) == {
28+
"link": "https://api.whatsapp.com/send?phone=972536106106&text="}
29+
30+
31+
def test_no_number():
32+
# Redirects to whatsapp window. Choose someone from your own contact list.
33+
phone_number = ""
34+
message = 'Which phone number?'
35+
assert whatsapp.make_link(phone_number, message) == {
36+
"link": "https://api.whatsapp.com/send?phone=&text=Which+phone+"
37+
"number%3F"}
38+
39+
40+
def test_end_to_end_testing(client):
41+
resp = client.get('/whatsapp?phone_number=972536106106&message=testing')
42+
assert resp.ok
43+
assert resp.json

0 commit comments

Comments
 (0)