Skip to content

feat: Add a feature for sharing information with a WhatsApp account #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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()
Expand Down
25 changes: 25 additions & 0 deletions app/routers/whatsapp.py
Original file line number Diff line number Diff line change
@@ -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}
43 changes: 43 additions & 0 deletions tests/test_whatsapp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from app.routers import whatsapp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an end to end test:

Use the client to fetch the /whatsapp route and check you get a valid JSON.



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