Skip to content

Commit

Permalink
Merge pull request #24 from ineshbose/updates
Browse files Browse the repository at this point in the history
Update dependencies, login and validate refactor
  • Loading branch information
ineshbose committed Nov 27, 2020
2 parents 303cef9 + ee56dbb commit 8fedf10
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 20 deletions.
2 changes: 1 addition & 1 deletion boyd_bot/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def webhook():
user_data = platform.get_user_data(sender_id)
if (
not sender_id
or ("error" in user_data and platform_user)
or (not platform.validate_user(user_data) and platform_user)
or not (platform_user or config["FEATURES"]["DEMO"])
):
log(config["LOG"]["INVALID_USER"](sender_id))
Expand Down
13 changes: 11 additions & 2 deletions boyd_bot/services/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def send_message(self, uid, message):
"""
return [
requests.post(
"https://graph.facebook.com/v7.0/me/messages",
"https://graph.facebook.com/me/messages",
params={"access_token": self.platform_token},
json={
"recipient": {"id": uid},
Expand All @@ -62,7 +62,7 @@ def get_user_data(self, uid):
Get basic information about the user from the platform.
"""
return requests.get(
f"https://graph.facebook.com/v7.0/{uid}",
f"https://graph.facebook.com/{uid}",
params={"access_token": self.platform_token}
).json()

Expand All @@ -84,6 +84,15 @@ def get_id(self, data):
False,
)

def validate_user(self, data):
"""
Check that a user is a valid platform-user by verifying
details from the platform's API.
"""
return (
"error" not in data or data["error"]["error_subcode"] == 2018218
)

def reply(self, message=None):
"""
Send a response to a message sent by the user.
Expand Down
15 changes: 10 additions & 5 deletions boyd_bot/timetable.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ def __init__(self):

def login(self, uid, uni_id, uni_pw):
try:
self.calendars[uid] = Calendar.from_ical(
requests.get(self.cal_url, auth=(uni_id, uni_pw)).content
)
return True, "Success"
req = requests.get(self.cal_url, auth=(uni_id, uni_pw))
if req.status_code == 200:
self.calendars[uid] = Calendar.from_ical(req.content)
return True, "Success"
else:
raise (
ValueError() if req.status_code in [401, 403]
else Exception(f"Code {req.status_code} : {req.content}")
)
except ValueError:
return False, "Invalid credentials."
except Exception as e:
return False, f"Something went wrong. Try again. {str(e)}"
return False, f"Something went wrong. Try again.\n{str(e)}"

def format_event(self, event):
return (
Expand Down
9 changes: 9 additions & 0 deletions docs/files/services/platform.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ Maps to unique sender ID through POST request data.
| **`data`:** the POST request dictionary | **`str`:** the unique sender ID of the user |


### `Platform`.**`validate_user(data)`**

Confirms user is valid platform-user with their API.

| Parameters | Returns |
|--------------------------------------------|---------------------------------------------|
| **`data`:** the JSON with user information | **`bool`:** if user is valid platform user |


### `Platform`.**`reply(message=None)`**

Prepares a formatted JSON containing the message as a response to the POST request.
Expand Down
4 changes: 2 additions & 2 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mkdocs==1.1.2
mkdocs-material==5.5.5
mkdocs-material-extensions==1.0
mkdocs-material==6.1.6
mkdocs-material-extensions==1.0.1
20 changes: 10 additions & 10 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
APScheduler==3.6.3
cryptography==2.9.2
cryptography==3.2.1
Flask==1.1.2
icalendar==4.0.6
pymongo==3.11.0
rapidfuzz==0.9.1
requests==2.24.0
icalendar==4.0.7
pymongo==3.11.1
rapidfuzz==0.13.3
requests==2.25.0


# requests
certifi==2020.6.20
certifi==2020.11.8
chardet==3.0.4
idna==2.9
urllib3==1.25.9
idna==2.10
urllib3==1.26.2


# cryptography
six==1.15.0
cffi==1.14.0
cffi==1.14.4
pycparser==2.20


Expand All @@ -27,7 +27,7 @@ Jinja2==2.11.2
Werkzeug==1.0.1

Flask-WTF==0.14.3
WTForms==2.3.1
WTForms==2.3.3
MarkupSafe==1.1.1


Expand Down

0 comments on commit 8fedf10

Please sign in to comment.