Fix security, bugs, and cleanup#11
Conversation
- Remove hardcoded API credentials; use TELEGRAM_API_ID and TELEGRAM_API_HASH env vars - Fix crash when reporting user accounts (not channels) - Fix duplicate reports by using single session - Add error handling so one account failure doesn't kill the run - Add proper client disconnect on account add to prevent leaked tasks - Remove deprecated WindowsSelectorEventLoopPolicy (removed in Python 3.16) - Remove unused packages from requirements.txt - Add .gitignore for sessions, __pycache__, and local files - Fix typos and improve report output readability Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Reviewer's GuideRefactors the Telegram reporting script to use environment-based API credentials, safer session handling, and a more robust, readable reporting flow with improved error handling and cleanup of unused/deprecated components. Sequence diagram for updated Telegram reporting flowsequenceDiagram
actor User
participant Script
participant Environment
participant SessionsDir
participant TelegramClient
participant TelegramAPI
User->>Script: Run reper.py -r N -t target -m mode
Script->>Environment: Read TELEGRAM_API_ID, TELEGRAM_API_HASH
Environment-->>Script: API_ID, API_HASH
Script->>Script: Validate API credentials
alt Missing credentials
Script-->>User: Print error and exit
else Credentials present
Script->>SessionsDir: ac_session_numbers()
SessionsDir-->>Script: [account_numbers]
alt No accounts
Script-->>User: Print "add an account" message and exit
else At least one account
Script->>TelegramClient: Create client for sessions/Ac1
Script->>Script: asyncio.run(run_all_accounts())
Script->>TelegramClient: report_channel(client)
TelegramClient->>TelegramAPI: get_entity(target)
alt Invalid target
TelegramAPI-->>TelegramClient: ValueError
TelegramClient-->>Script: Print invalid link and return
else Valid target
TelegramAPI-->>TelegramClient: entity
TelegramClient->>TelegramAPI: iter_dialogs()
TelegramAPI-->>TelegramClient: dialogs
alt Entity is Channel and not in dialogs
TelegramClient->>TelegramAPI: JoinChannelRequest(entity)
TelegramAPI-->>TelegramClient: joined
end
Script->>Script: reason = mode_to_report_reason(mode)
loop report_count times
TelegramClient->>TelegramAPI: ReportPeerRequest(peer=entity, reason, message)
alt Success
TelegramAPI-->>TelegramClient: ok
TelegramClient-->>User: Print OK with account name and counter
else Failure
TelegramAPI-->>TelegramClient: failure
TelegramClient-->>User: Print FAIL with account name and counter
end
end
end
end
end
Class diagram for new helper functions and reporting structureclassDiagram
class ReperScriptModule {
+ac_session_numbers() int[]
+mode_to_report_reason(mode str) InputReportReason
+report_channel(telegram_client TelegramClient) None
+run_all_accounts() None
}
class TelegramClient {
+start(phone_number str) None
+disconnect() None
+get_entity(peer) Entity
+iter_dialogs() AsyncIterator
}
class InputReportReason {
}
class InputReportReasonSpam {
}
class InputReportReasonFake {
}
class InputReportReasonViolence {
}
class InputReportReasonChildAbuse {
}
class InputReportReasonPornography {
}
class InputReportReasonGeoIrrelevant {
}
class InputReportReasonOther {
}
class Channel {
}
ReperScriptModule --> TelegramClient : uses
ReperScriptModule --> InputReportReason : returns
InputReportReason <|-- InputReportReasonSpam
InputReportReason <|-- InputReportReasonFake
InputReportReason <|-- InputReportReasonViolence
InputReportReason <|-- InputReportReasonChildAbuse
InputReportReason <|-- InputReportReasonPornography
InputReportReason <|-- InputReportReasonGeoIrrelevant
InputReportReason <|-- InputReportReasonOther
ReperScriptModule --> Channel : checks_type
class EnvironmentConfig {
+API_ID str
+API_HASH str
+load_from_env() None
}
ReperScriptModule --> EnvironmentConfig : reads_credentials
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
This PR makes the output of the code execution cleaner and more understandable. It also lets the user use these TELEGRAM_API_ID and TELEGRAM_API_HASH env vars. And lots of bug fixes. Easy run and done. |
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- Converting TELEGRAM_API_ID to int with
api_id = int(API_ID)will raise a traceback on misconfigured env vars; consider validating the env values and exiting with a clear error message instead of relying on an uncaught ValueError. ac_session_numbers()assumes thesessionsdirectory exists; if this module is imported and the top-levelmakedirs('sessions', exist_ok=True)isn't run first, it will raise, so it may be safer forac_session_numbers()itself to ensure the directory exists or handleFileNotFoundError.- The early exit when TELEGRAM_API_ID/HASH are missing prevents
--helpor--reasonsfrom working in an unconfigured environment; consider deferring the env var check until a command that actually needs a Telegram client is requested.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Converting TELEGRAM_API_ID to int with `api_id = int(API_ID)` will raise a traceback on misconfigured env vars; consider validating the env values and exiting with a clear error message instead of relying on an uncaught ValueError.
- `ac_session_numbers()` assumes the `sessions` directory exists; if this module is imported and the top-level `makedirs('sessions', exist_ok=True)` isn't run first, it will raise, so it may be safer for `ac_session_numbers()` itself to ensure the directory exists or handle `FileNotFoundError`.
- The early exit when TELEGRAM_API_ID/HASH are missing prevents `--help` or `--reasons` from working in an unconfigured environment; consider deferring the env var check until a command that actually needs a Telegram client is requested.
## Individual Comments
### Comment 1
<location path="reper.py" line_range="71-72" />
<code_context>
+ print(f' [{Fore.RED}!{Fore.RESET}] Set TELEGRAM_API_ID and TELEGRAM_API_HASH environment variables.')
+ sys.exit(1)
+
+api_id = int(API_ID)
+api_hash = API_HASH
+
if command_line_args.help:
</code_context>
<issue_to_address>
**issue:** Guard against non-numeric TELEGRAM_API_ID values to avoid a crash at startup.
`API_ID` is cast with `int(API_ID)` without checking the value. If `TELEGRAM_API_ID` is set but not strictly numeric (e.g., whitespace, token, or empty string), this will raise `ValueError` at startup. Please validate the env var first (e.g., `isdigit()` or `try/except ValueError` with a clear error message and exit) so misconfiguration fails cleanly instead of with a stack trace.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| api_id = int(API_ID) | ||
| api_hash = API_HASH |
There was a problem hiding this comment.
issue: Guard against non-numeric TELEGRAM_API_ID values to avoid a crash at startup.
API_ID is cast with int(API_ID) without checking the value. If TELEGRAM_API_ID is set but not strictly numeric (e.g., whitespace, token, or empty string), this will raise ValueError at startup. Please validate the env var first (e.g., isdigit() or try/except ValueError with a clear error message and exit) so misconfiguration fails cleanly instead of with a stack trace.
Summary by Sourcery
Improve Telegram reporting script security, robustness, and usability while simplifying dependencies and repo hygiene.
New Features:
traffickingreport mode mapped to the TelegramOtherreport reason.Bug Fixes:
Enhancements:
Build:
Documentation:
Chores: