Skip to content

feat: Add /userinfo command to fetch Reddit user data#392

Closed
lucaspolo wants to merge 1 commit intomainfrom
feat/reddit-user-command
Closed

feat: Add /userinfo command to fetch Reddit user data#392
lucaspolo wants to merge 1 commit intomainfrom
feat/reddit-user-command

Conversation

@lucaspolo
Copy link
Owner

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.

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.
@lucaspolo lucaspolo requested a review from Copilot May 21, 2025 12:55
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

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) in reddit_crawler.py to retrieve and parse user data.
  • Implemented user_info handler in bot.py, registering /userinfo and /ui, formatting timestamps, and handling errors.
  • Covered new behavior with unit tests in both tests/ui/test_bot.py and tests/crawlers/test_crawler.py; updated Python version specifier in pyproject.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 CallbackContext but there’s no import for it in this file. Please add from telegram.ext import CallbackContext to 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.')
Copy link

Copilot AI May 21, 2025

Choose a reason for hiding this comment

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

[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

Copilot uses AI. Check for mistakes.
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.
Copy link

Copilot AI May 21, 2025

Choose a reason for hiding this comment

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

[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.

Copilot uses AI. Check for mistakes.
@lucaspolo lucaspolo closed this Feb 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments