Skip to content

Commit

Permalink
View author profile functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
$MYNAME authored and $MYNAME committed Feb 25, 2021
1 parent 80bf311 commit 62c1c37
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 22 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ or if you have make
make install
```

3. Generate a chatbot token and update `TELEGRAM_TOKEN` value in `token_data.yaml` file (the other constants below are only needed to run the tests)
3. Copy `token_data.default.yaml` to `token_data.yaml`.

4. Generate a chatbot token and update `TELEGRAM_TOKEN` value in `token_data.yaml` file (the other constants below are only needed to run the tests)

Follow the below steps which are already mentioned in the main.py file in the package.
- Import logging library to connect and authenticate bot with Telegram API
Expand Down
9 changes: 8 additions & 1 deletion chatbot/app/fp_api_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def get_posts(payload):


def get_post(post_id):
url = FP_BASE_URL + "posts/" + post_id
url = FP_BASE_URL + f"posts/{post_id}"

with requests.Session() as s:
return _try_get(s, url)
Expand All @@ -79,6 +79,13 @@ def get_current_user_profile(token):
return _try_get(s, url)


def get_user_profile(user_id):
url = FP_BASE_URL + f"users/{user_id}"

with requests.Session() as s:
return _try_get(s, url)


def get_user_location(latitude, longitude):
payload = {
'lat': latitude,
Expand Down
3 changes: 3 additions & 0 deletions chatbot/app/handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .mainmenu import (
MainMenuCmdHandler,
MainMenuQueryHandler,
)
from .info import (
HelpCmdHandler,
Expand All @@ -14,6 +15,8 @@
ViewMyPostsQueryHandler,
DisplaySelectedPostsQueryHandler,
ViewPostsConvHandler,
ViewAuthorProfileQueryHandler,
GoBackViewAuthorQueryHandler,
)
from .create_post import (
CreatePostConvHandler,
Expand Down
12 changes: 8 additions & 4 deletions chatbot/app/handlers/mainmenu.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from telegram.ext import CommandHandler
from telegram.ext import CommandHandler, CallbackQueryHandler

from chatbot.app import keyboards, user_data
from chatbot.app import keyboards, user_data, patterns
from chatbot.app.handlers import util


def main_menu(update, context):
Expand All @@ -10,10 +11,13 @@ def main_menu(update, context):
user_signed_in = user_data.is_user_signed_in(context=context)
if not user_signed_in:
text += " To see more options, create posts etc you need to first login."
update.message.reply_text(
util.reply(
update=update,
context=context,
text=text,
reply_markup=keyboards.main_menu(user_signed_in),
keyboard=keyboards.main_menu(user_signed_in),
)


MainMenuCmdHandler = CommandHandler("mainmenu", main_menu)
MainMenuQueryHandler = CallbackQueryHandler(main_menu, pattern=patterns.MAINMENU)
59 changes: 50 additions & 9 deletions chatbot/app/handlers/view.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import logging
from enum import Enum, auto

from telegram.ext import (
Expand Down Expand Up @@ -33,8 +34,7 @@ def view_my_profile(update, context):
return

response = fpapi.get_current_user_profile(token=token)
if isinstance(response, fpapi.Error): # TODO handle error better
raise ConnectionError("Could not get user profile")
_assert_not_error(response)
user_info_view = views.UserProfile(response).display()
update.effective_message.reply_text(text=user_info_view)

Expand Down Expand Up @@ -187,8 +187,7 @@ def _get_posts(context, payload):
# keep a copy of post_payload in user_data for future calls - TODO is it used?
context.user_data[user_data.VIEW_POST_PAYLOAD] = payload
posts = fpapi.get_posts(payload)
if isinstance(posts, fpapi.Error): # TODO handle error better
raise ConnectionError("Could not get posts")
_assert_not_error(posts)
return posts


Expand Down Expand Up @@ -320,6 +319,7 @@ def _handle_post_id_choice(update, context, user_choice):
_update_user_post_id(context, post_id=post_id)
_show_user_single_post(
update=update,
context=context,
post_id=post_id,
)

Expand Down Expand Up @@ -354,17 +354,23 @@ def _get_real_post_id(context, user_choice):
return context.user_data[user_data.VIEW_POST_IDS][int(user_choice) - 1]


def _show_user_single_post(update, post_id):
def _show_user_single_post(update, context, post_id):
post = fpapi.get_post(post_id)
if isinstance(post, fpapi.Error): # TODO handle error better
raise ConnectionError("Could not get post")
_assert_not_error(post)
reply_text = views.Post(post_json=post).display()
update.effective_message.reply_text(
util.reply(
update=update,
context=context,
text=reply_text,
reply_markup=keyboards.display_selected_post(),
keyboard=keyboards.display_selected_post(),
)


def _assert_not_error(post):
if isinstance(post, fpapi.Error): # TODO handle error better
raise ConnectionError("Could not get post")


def _get_header_message_with_categories(context):
formatted_categories = ", ".join(context.user_data[user_data.POST_CATEGORIES])
page = _get_current_user_page(context)
Expand All @@ -377,7 +383,42 @@ def _get_header_message_user_posts(context):
return f"Page {page} of your posts"



def view_author_profile(update, context):
post_id = context.user_data.get(user_data.VIEW_POST_ID)
if post_id is None:
logging.warning("No post ID to view author for")
return
author = _get_author_profile_from_post_id(post_id=post_id)
util.reply(
update=update,
context=context,
text=author.display(),
keyboard=keyboards.view_author(),
)


def handle_go_back_view_author(update, context):
post_id = context.user_data[user_data.VIEW_POST_ID]
_show_user_single_post(
update=update,
context=context,
post_id=post_id,
)


def _get_author_profile_from_post_id(post_id):
raw_post = fpapi.get_post(post_id)
_assert_not_error(raw_post)
post = views.Post(post_json=raw_post)
response = fpapi.get_user_profile(user_id=post.author_id)
_assert_not_error(response)
return views.UserProfile(response)


ViewMyProfileQueryHandler = CallbackQueryHandler(view_my_profile, pattern=patterns.VIEW_MY_PROFILE)
ViewMyPostsQueryHandler = CallbackQueryHandler(view_my_posts, pattern=patterns.VIEW_MY_POSTS)
DisplaySelectedPostsQueryHandler = CallbackQueryHandler(display_selected_post, pattern=patterns.DISPLAY_SELECTED_POST)
ViewPostsConvHandler = view_posts_conversation()
ViewAuthorProfileQueryHandler = CallbackQueryHandler(view_author_profile, pattern=patterns.VIEW_AUTHOR_PROFILE)
GoBackViewAuthorQueryHandler = CallbackQueryHandler(handle_go_back_view_author, pattern=patterns.GO_BACK_VIEW_AUTHOR)
10 changes: 9 additions & 1 deletion chatbot/app/keyboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,19 @@ def view_posts():


def display_selected_post():
_construct_inline_keyboard([[
return _construct_inline_keyboard([[
patterns.SEE_COMMENTS, patterns.VIEW_AUTHOR_PROFILE, patterns.POST_COMMENT
]])


def view_author():
return InlineKeyboardMarkup([[
_construct_inline_button(patterns.MAINMENU),
InlineKeyboardButton('Go Back', callback_data=patterns.GO_BACK_VIEW_AUTHOR)
]])



def no_location():
return InlineKeyboardMarkup([[
InlineKeyboardButton('I don\'t want to share my location right now', callback_data='view_posts'),
Expand Down
3 changes: 3 additions & 0 deletions chatbot/app/patterns.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
MAINMENU = "Main Menu"

OFFER_HELP = "Offer Help"
REQUEST_HELP = "Request Help"
VIEW_MY_POSTS = "View My Posts"
Expand Down Expand Up @@ -27,3 +29,4 @@
OFFER_HELP_POSTS = "Posts that offer help"

DISPLAY_SELECTED_POST = "display_selected_post"
GO_BACK_VIEW_AUTHOR = "go_back_view_author"
1 change: 1 addition & 0 deletions chatbot/app/user_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
LOCATION = 'location'
AUTHOR = 'author'
AUTHOR_NAME = 'name'
AUTHOR_ID = 'id'

POST_TITLE = "title"
POST_DESCRIPTION = "content"
Expand Down
11 changes: 6 additions & 5 deletions chatbot/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ def __init__(self, post_json):
self._post_json = post_json

self.title = self._extract_field(user_data.POST_TITLE)
author_data = self._extract_field(user_data.AUTHOR)
self.author = author_data[user_data.AUTHOR_NAME]
self.author_data = self._extract_field(user_data.AUTHOR)
self.author_id = self.author_data[user_data.AUTHOR_ID]
self.author = self.author_data[user_data.AUTHOR_NAME]
self.categories = self._extract_field(user_data.POST_CATEGORIES)
self.content = self._extract_field(user_data.POST_DESCRIPTION)
self.location = self._extract_location(author_data[user_data.LOCATION])
self.location = self._extract_location(self.author_data[user_data.LOCATION])
self.num_comments = self._extract_field(user_data.NUM_COMMENTS)

def _extract_field(self, field):
Expand Down Expand Up @@ -76,10 +77,10 @@ def _get_data_from_post_json(self):
class UserProfile(object):

def __init__(self, json_data):
self.email = json_data['email']
self.email = json_data.get('email')
self.firstName = json_data['firstName']
self.lastName = json_data['lastName']
self.address = json_data['location']['address']
self.address = json_data['location'].get('address')
self.is_volunteer = "No"
if json_data['objectives']['volunteer']:
self.is_volunteer = "Yes"
Expand Down
3 changes: 3 additions & 0 deletions chatbot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ def main(log_level="INFO"):
dp.add_handler(handlers.ViewPostsConvHandler)

# Callback query handlers
dp.add_handler(handlers.MainMenuQueryHandler)
dp.add_handler(handlers.AboutQueryHandler)
dp.add_handler(handlers.SignoutQueryHandler)
dp.add_handler(handlers.ViewMyProfileQueryHandler)
dp.add_handler(handlers.ViewMyPostsQueryHandler)
dp.add_handler(handlers.DisplaySelectedPostsQueryHandler)
dp.add_handler(handlers.ViewAuthorProfileQueryHandler)
dp.add_handler(handlers.GoBackViewAuthorQueryHandler)

# To start polling Telegram for any chat updates on Telegram
updater.start_polling()
Expand Down
2 changes: 1 addition & 1 deletion token_data.default.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Base URL
FP_BASE_URL: "http://127.0.0.1:8000/api/"
FP_BASE_URL: "https://fightpandemics.com/api/"

#################
# Access tokens #
Expand Down

0 comments on commit 62c1c37

Please sign in to comment.