-
Notifications
You must be signed in to change notification settings - Fork 82
[WIP] Fix API connection issue in bot development #58
Changes from 3 commits
067d8c0
9ecea97
8471001
a1bd668
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| """ | ||
| Example demonstrating the correct way to use SSID with PocketOption API | ||
|
|
||
| This example shows how to: | ||
| 1. Get the correct SSID format from browser | ||
| 2. Initialize the client properly | ||
| 3. Handle authentication errors | ||
| """ | ||
|
|
||
| import asyncio | ||
| from pocketoptionapi_async import AsyncPocketOptionClient | ||
| from pocketoptionapi_async.exceptions import InvalidParameterError, AuthenticationError | ||
|
|
||
|
|
||
| async def main(): | ||
| print("=" * 70) | ||
| print("PocketOption API - Correct SSID Usage Example") | ||
| print("=" * 70) | ||
|
|
||
| print("\n📋 INSTRUCTIONS:") | ||
| print("1. Open PocketOption in your browser (https://pocketoption.com)") | ||
| print("2. Press F12 to open Developer Tools") | ||
| print("3. Go to the Network tab") | ||
| print("4. Filter by 'WS' (WebSocket)") | ||
| print("5. Look for a message starting with: 42[\"auth\",") | ||
| print("6. Copy the ENTIRE message (including 42[\"auth\",{...}])") | ||
| print("\n") | ||
|
|
||
| # Example of CORRECT SSID format | ||
| print("✅ CORRECT SSID format:") | ||
| print(' 42["auth",{"session":"your_session_here","isDemo":1,"uid":12345,"platform":1}]') | ||
| print("\n") | ||
|
|
||
| # Example of WRONG format | ||
| print("❌ WRONG SSID format (just the session ID):") | ||
| print(' dxxxxxxxxxxxxxxxxxxxxxxxxxxxx') | ||
| print("\n") | ||
|
|
||
| # Get SSID from user | ||
| ssid_input = input("Enter your SSID (or press Enter to see demo): ").strip() | ||
|
|
||
| if not ssid_input: | ||
| print("\n📝 Using demo SSID to show validation...") | ||
| ssid_input = '42["auth",{"session":"demo_session_id","isDemo":1,"uid":12345,"platform":1}]' | ||
|
|
||
| print("\n" + "=" * 70) | ||
|
|
||
| try: | ||
| print("🔧 Initializing PocketOption client...") | ||
|
|
||
| # Create client with SSID | ||
| client = AsyncPocketOptionClient( | ||
| ssid=ssid_input, | ||
| is_demo=True, # Set to False for live trading | ||
| enable_logging=True # Set to False to reduce console output | ||
| ) | ||
|
|
||
| print("✅ Client initialized successfully!") | ||
| print(f" Session ID: {client.session_id[:20]}...") | ||
| print(f" User ID: {client.uid}") | ||
| print(f" Demo mode: {client.is_demo}") | ||
| print("\n") | ||
|
|
||
| # Try to connect | ||
| print("🔌 Connecting to PocketOption...") | ||
| connected = await client.connect() | ||
|
|
||
| if connected: | ||
| print("✅ Connected successfully!") | ||
|
|
||
| # Get balance | ||
| try: | ||
| balance = await client.get_balance() | ||
| print(f"💰 Balance: {balance.balance} {balance.currency}") | ||
| except Exception as e: | ||
| print(f"⚠️ Could not get balance: {e}") | ||
|
|
||
| # Disconnect | ||
| await client.disconnect() | ||
| print("✅ Disconnected successfully") | ||
|
|
||
| else: | ||
| print("❌ Connection failed") | ||
| print("\n💡 Troubleshooting:") | ||
| print(" • Make sure your SSID is in the correct format") | ||
| print(" • Your SSID might be expired - get a fresh one from browser") | ||
| print(" • Make sure you copied the ENTIRE message including 42[\"auth\",{...}]") | ||
|
|
||
| except InvalidParameterError as e: | ||
| print(f"\n❌ SSID Format Error:") | ||
| print(f" {e}") | ||
| print("\n💡 Make sure you're using the complete SSID format from browser DevTools!") | ||
|
|
||
| except AuthenticationError as e: | ||
| print(f"\n❌ Authentication Error:") | ||
| print(f" {e}") | ||
| print("\n💡 Your SSID might be expired. Get a fresh one from your browser!") | ||
|
|
||
| except Exception as e: | ||
| print(f"\n❌ Unexpected Error:") | ||
| print(f" {type(e).__name__}: {e}") | ||
|
|
||
| print("\n" + "=" * 70) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -78,14 +78,10 @@ def __init__( | |||||
| if not enable_logging: | ||||||
| logger.remove() | ||||||
| logger.add(lambda msg: None, level="CRITICAL") # Disable most logging | ||||||
| # Parse SSID if it's a complete auth message | ||||||
|
|
||||||
| # Validate and parse SSID | ||||||
| self._original_demo = None # Store original demo value from SSID | ||||||
| if ssid.startswith('42["auth",'): | ||||||
| self._parse_complete_ssid(ssid) | ||||||
| else: | ||||||
| # Treat as raw session ID | ||||||
| self.session_id = ssid | ||||||
| self._complete_ssid = None | ||||||
| self._validate_and_parse_ssid(ssid) | ||||||
|
|
||||||
| # Core components | ||||||
| self._websocket = AsyncWebSocketClient() | ||||||
|
|
@@ -656,6 +652,39 @@ def get_connection_stats(self) -> Dict[str, Any]: | |||||
|
|
||||||
| return stats # Private methods | ||||||
|
|
||||||
| def _validate_and_parse_ssid(self, ssid: str) -> None: | ||||||
| """Validate and parse SSID format""" | ||||||
| if not ssid or not isinstance(ssid, str): | ||||||
|
||||||
| if not ssid or not isinstance(ssid, str): | |
| if ssid is None or not isinstance(ssid, str) or not ssid: |
Copilot
AI
Dec 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error handling logic is potentially confusing. After catching InvalidParameterError and re-raising it at line 738-739, the generic Exception handler at line 740-744 will never be reached if an InvalidParameterError is raised from within the try block (since it's caught earlier). Consider either removing the re-raise clause or restructuring the exception handling to avoid this dead code pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incorrect section header structure. The header "## Traceback:" should be "### Traceback:" to maintain proper heading hierarchy, as it appears under the "### Websockets version error" section.