Skip to content

Commit 62c1c37

Browse files
$MYNAME$MYNAME
$MYNAME
authored and
$MYNAME
committed
View author profile functionality
1 parent 80bf311 commit 62c1c37

11 files changed

+95
-22
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ or if you have make
1818
make install
1919
```
2020

21-
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)
21+
3. Copy `token_data.default.yaml` to `token_data.yaml`.
22+
23+
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)
2224

2325
Follow the below steps which are already mentioned in the main.py file in the package.
2426
- Import logging library to connect and authenticate bot with Telegram API

chatbot/app/fp_api_manager.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def get_posts(payload):
6565

6666

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

7070
with requests.Session() as s:
7171
return _try_get(s, url)
@@ -79,6 +79,13 @@ def get_current_user_profile(token):
7979
return _try_get(s, url)
8080

8181

82+
def get_user_profile(user_id):
83+
url = FP_BASE_URL + f"users/{user_id}"
84+
85+
with requests.Session() as s:
86+
return _try_get(s, url)
87+
88+
8289
def get_user_location(latitude, longitude):
8390
payload = {
8491
'lat': latitude,

chatbot/app/handlers/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from .mainmenu import (
22
MainMenuCmdHandler,
3+
MainMenuQueryHandler,
34
)
45
from .info import (
56
HelpCmdHandler,
@@ -14,6 +15,8 @@
1415
ViewMyPostsQueryHandler,
1516
DisplaySelectedPostsQueryHandler,
1617
ViewPostsConvHandler,
18+
ViewAuthorProfileQueryHandler,
19+
GoBackViewAuthorQueryHandler,
1720
)
1821
from .create_post import (
1922
CreatePostConvHandler,

chatbot/app/handlers/mainmenu.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from telegram.ext import CommandHandler
1+
from telegram.ext import CommandHandler, CallbackQueryHandler
22

3-
from chatbot.app import keyboards, user_data
3+
from chatbot.app import keyboards, user_data, patterns
4+
from chatbot.app.handlers import util
45

56

67
def main_menu(update, context):
@@ -10,10 +11,13 @@ def main_menu(update, context):
1011
user_signed_in = user_data.is_user_signed_in(context=context)
1112
if not user_signed_in:
1213
text += " To see more options, create posts etc you need to first login."
13-
update.message.reply_text(
14+
util.reply(
15+
update=update,
16+
context=context,
1417
text=text,
15-
reply_markup=keyboards.main_menu(user_signed_in),
18+
keyboard=keyboards.main_menu(user_signed_in),
1619
)
1720

1821

1922
MainMenuCmdHandler = CommandHandler("mainmenu", main_menu)
23+
MainMenuQueryHandler = CallbackQueryHandler(main_menu, pattern=patterns.MAINMENU)

chatbot/app/handlers/view.py

+50-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import logging
23
from enum import Enum, auto
34

45
from telegram.ext import (
@@ -33,8 +34,7 @@ def view_my_profile(update, context):
3334
return
3435

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

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

194193

@@ -320,6 +319,7 @@ def _handle_post_id_choice(update, context, user_choice):
320319
_update_user_post_id(context, post_id=post_id)
321320
_show_user_single_post(
322321
update=update,
322+
context=context,
323323
post_id=post_id,
324324
)
325325

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

356356

357-
def _show_user_single_post(update, post_id):
357+
def _show_user_single_post(update, context, post_id):
358358
post = fpapi.get_post(post_id)
359-
if isinstance(post, fpapi.Error): # TODO handle error better
360-
raise ConnectionError("Could not get post")
359+
_assert_not_error(post)
361360
reply_text = views.Post(post_json=post).display()
362-
update.effective_message.reply_text(
361+
util.reply(
362+
update=update,
363+
context=context,
363364
text=reply_text,
364-
reply_markup=keyboards.display_selected_post(),
365+
keyboard=keyboards.display_selected_post(),
365366
)
366367

367368

369+
def _assert_not_error(post):
370+
if isinstance(post, fpapi.Error): # TODO handle error better
371+
raise ConnectionError("Could not get post")
372+
373+
368374
def _get_header_message_with_categories(context):
369375
formatted_categories = ", ".join(context.user_data[user_data.POST_CATEGORIES])
370376
page = _get_current_user_page(context)
@@ -377,7 +383,42 @@ def _get_header_message_user_posts(context):
377383
return f"Page {page} of your posts"
378384

379385

386+
387+
def view_author_profile(update, context):
388+
post_id = context.user_data.get(user_data.VIEW_POST_ID)
389+
if post_id is None:
390+
logging.warning("No post ID to view author for")
391+
return
392+
author = _get_author_profile_from_post_id(post_id=post_id)
393+
util.reply(
394+
update=update,
395+
context=context,
396+
text=author.display(),
397+
keyboard=keyboards.view_author(),
398+
)
399+
400+
401+
def handle_go_back_view_author(update, context):
402+
post_id = context.user_data[user_data.VIEW_POST_ID]
403+
_show_user_single_post(
404+
update=update,
405+
context=context,
406+
post_id=post_id,
407+
)
408+
409+
410+
def _get_author_profile_from_post_id(post_id):
411+
raw_post = fpapi.get_post(post_id)
412+
_assert_not_error(raw_post)
413+
post = views.Post(post_json=raw_post)
414+
response = fpapi.get_user_profile(user_id=post.author_id)
415+
_assert_not_error(response)
416+
return views.UserProfile(response)
417+
418+
380419
ViewMyProfileQueryHandler = CallbackQueryHandler(view_my_profile, pattern=patterns.VIEW_MY_PROFILE)
381420
ViewMyPostsQueryHandler = CallbackQueryHandler(view_my_posts, pattern=patterns.VIEW_MY_POSTS)
382421
DisplaySelectedPostsQueryHandler = CallbackQueryHandler(display_selected_post, pattern=patterns.DISPLAY_SELECTED_POST)
383422
ViewPostsConvHandler = view_posts_conversation()
423+
ViewAuthorProfileQueryHandler = CallbackQueryHandler(view_author_profile, pattern=patterns.VIEW_AUTHOR_PROFILE)
424+
GoBackViewAuthorQueryHandler = CallbackQueryHandler(handle_go_back_view_author, pattern=patterns.GO_BACK_VIEW_AUTHOR)

chatbot/app/keyboards.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,19 @@ def view_posts():
113113

114114

115115
def display_selected_post():
116-
_construct_inline_keyboard([[
116+
return _construct_inline_keyboard([[
117117
patterns.SEE_COMMENTS, patterns.VIEW_AUTHOR_PROFILE, patterns.POST_COMMENT
118118
]])
119119

120120

121+
def view_author():
122+
return InlineKeyboardMarkup([[
123+
_construct_inline_button(patterns.MAINMENU),
124+
InlineKeyboardButton('Go Back', callback_data=patterns.GO_BACK_VIEW_AUTHOR)
125+
]])
126+
127+
128+
121129
def no_location():
122130
return InlineKeyboardMarkup([[
123131
InlineKeyboardButton('I don\'t want to share my location right now', callback_data='view_posts'),

chatbot/app/patterns.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
MAINMENU = "Main Menu"
2+
13
OFFER_HELP = "Offer Help"
24
REQUEST_HELP = "Request Help"
35
VIEW_MY_POSTS = "View My Posts"
@@ -27,3 +29,4 @@
2729
OFFER_HELP_POSTS = "Posts that offer help"
2830

2931
DISPLAY_SELECTED_POST = "display_selected_post"
32+
GO_BACK_VIEW_AUTHOR = "go_back_view_author"

chatbot/app/user_data.py

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
LOCATION = 'location'
88
AUTHOR = 'author'
99
AUTHOR_NAME = 'name'
10+
AUTHOR_ID = 'id'
1011

1112
POST_TITLE = "title"
1213
POST_DESCRIPTION = "content"

chatbot/app/views.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ def __init__(self, post_json):
1212
self._post_json = post_json
1313

1414
self.title = self._extract_field(user_data.POST_TITLE)
15-
author_data = self._extract_field(user_data.AUTHOR)
16-
self.author = author_data[user_data.AUTHOR_NAME]
15+
self.author_data = self._extract_field(user_data.AUTHOR)
16+
self.author_id = self.author_data[user_data.AUTHOR_ID]
17+
self.author = self.author_data[user_data.AUTHOR_NAME]
1718
self.categories = self._extract_field(user_data.POST_CATEGORIES)
1819
self.content = self._extract_field(user_data.POST_DESCRIPTION)
19-
self.location = self._extract_location(author_data[user_data.LOCATION])
20+
self.location = self._extract_location(self.author_data[user_data.LOCATION])
2021
self.num_comments = self._extract_field(user_data.NUM_COMMENTS)
2122

2223
def _extract_field(self, field):
@@ -76,10 +77,10 @@ def _get_data_from_post_json(self):
7677
class UserProfile(object):
7778

7879
def __init__(self, json_data):
79-
self.email = json_data['email']
80+
self.email = json_data.get('email')
8081
self.firstName = json_data['firstName']
8182
self.lastName = json_data['lastName']
82-
self.address = json_data['location']['address']
83+
self.address = json_data['location'].get('address')
8384
self.is_volunteer = "No"
8485
if json_data['objectives']['volunteer']:
8586
self.is_volunteer = "Yes"

chatbot/main.py

+3
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@ def main(log_level="INFO"):
4141
dp.add_handler(handlers.ViewPostsConvHandler)
4242

4343
# Callback query handlers
44+
dp.add_handler(handlers.MainMenuQueryHandler)
4445
dp.add_handler(handlers.AboutQueryHandler)
4546
dp.add_handler(handlers.SignoutQueryHandler)
4647
dp.add_handler(handlers.ViewMyProfileQueryHandler)
4748
dp.add_handler(handlers.ViewMyPostsQueryHandler)
4849
dp.add_handler(handlers.DisplaySelectedPostsQueryHandler)
50+
dp.add_handler(handlers.ViewAuthorProfileQueryHandler)
51+
dp.add_handler(handlers.GoBackViewAuthorQueryHandler)
4952

5053
# To start polling Telegram for any chat updates on Telegram
5154
updater.start_polling()

token_data.default.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Base URL
2-
FP_BASE_URL: "http://127.0.0.1:8000/api/"
2+
FP_BASE_URL: "https://fightpandemics.com/api/"
33

44
#################
55
# Access tokens #

0 commit comments

Comments
 (0)