feat: Add /userinfo command to fetch Reddit user data#392
Closed
feat: Add /userinfo command to fetch Reddit user data#392
Conversation
This commit introduces a new command `/userinfo` (alias `/ui`) that allows you to fetch information about a Reddit user.
New functionality:
- I added a `get_user_info(username)` function in `reddit_crawler.py` to retrieve user details (name, karma, account creation date) from the Reddit API (`/user/{username}/about.json`).
- I implemented the `user_info(update, context)` command handler in `bot.py`. This function processes the command, calls the crawler, formats the information (including a human-readable account creation date), and sends it to you on Telegram.
- I handled cases where the username is missing, the user is not found on Reddit, or an API error occurs.
- I registered the new command and its alias in `bot.py`.
Testing:
- I added unit tests for `get_user_info` in `tests/crawlers/test_crawler.py` to cover successful data retrieval, user-not-found scenarios, and API errors.
- I added unit tests for the `user_info` command handler in `tests/ui/test_bot.py` to verify correct message formatting and error handling for various scenarios (successful lookup, user not found, no username, API error).
All new tests are passing.
There was a problem hiding this comment.
Pull Request Overview
Adds a new /userinfo (alias /ui) command to fetch Reddit user details via the Reddit API, with supporting crawler logic and unit tests.
- Added
get_user_info(username)inreddit_crawler.pyto retrieve and parse user data. - Implemented
user_infohandler inbot.py, registering/userinfoand/ui, formatting timestamps, and handling errors. - Covered new behavior with unit tests in both
tests/ui/test_bot.pyandtests/crawlers/test_crawler.py; updated Python version specifier inpyproject.toml.
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/ui/test_bot.py | Added tests for /userinfo command covering success, not-found, missing username, and API errors |
| tests/crawlers/test_crawler.py | Added tests for get_user_info covering successful retrieval, 404, and API failures |
| redditbot/ui/bot.py | Introduced user_info handler, registered new commands, and formatted creation date |
| redditbot/crawlers/reddit_crawler.py | Added get_user_info async function to call /user/{username}/about.json and handle responses |
| pyproject.toml | Changed python = "3.12" to python = "~3.12.0" |
Comments suppressed due to low confidence (1)
redditbot/ui/bot.py:114
- The signature references
CallbackContextbut there’s no import for it in this file. Please addfrom telegram.ext import CallbackContextto avoid a NameError.
async def user_info(update: Update, context: CallbackContext):
Comment on lines
+137
to
+139
| except Exception as e: | ||
| logging.error(f"Error fetching user info for {username}: {e}") | ||
| await update.message.reply_text('Sorry, something went wrong while fetching user information.') |
There was a problem hiding this comment.
[nitpick] Catching all Exception can mask unexpected errors. Consider narrowing this to httpx.HTTPError (or the specific exception types) so other issues bubble up.
Suggested change
| except Exception as e: | |
| logging.error(f"Error fetching user info for {username}: {e}") | |
| await update.message.reply_text('Sorry, something went wrong while fetching user information.') | |
| except httpx.HTTPError as e: | |
| logging.error(f"HTTP error while fetching user info for {username}: {e}") | |
| await update.message.reply_text('Sorry, there was a network error while fetching user information.') | |
| except Exception as e: | |
| logging.error(f"Unexpected error fetching user info for {username}: {e}") | |
| raise |
| A dictionary containing user information (name, karma, created_utc), | ||
| or None if the user is not found. | ||
| Raises: | ||
| Exception: If there is an API error. |
There was a problem hiding this comment.
[nitpick] The docstring says it raises a generic Exception, but the code actually raises httpx.HTTPStatusError. Update the docstring to reflect the specific exception type.
Suggested change
| Exception: If there is an API error. | |
| httpx.HTTPStatusError: If there is an API error. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This commit introduces a new command
/userinfo(alias/ui) that allows you to fetch information about a Reddit user.New functionality:
get_user_info(username)function inreddit_crawler.pyto retrieve user details (name, karma, account creation date) from the Reddit API (/user/{username}/about.json).user_info(update, context)command handler inbot.py. This function processes the command, calls the crawler, formats the information (including a human-readable account creation date), and sends it to you on Telegram.bot.py.Testing:
get_user_infointests/crawlers/test_crawler.pyto cover successful data retrieval, user-not-found scenarios, and API errors.user_infocommand handler intests/ui/test_bot.pyto verify correct message formatting and error handling for various scenarios (successful lookup, user not found, no username, API error).All new tests are passing.