|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Example usage of the Wasender API SDK |
| 4 | +
|
| 5 | +This example demonstrates: |
| 6 | +1. Sending a text message |
| 7 | +2. Getting all contacts |
| 8 | +
|
| 9 | +Set the following environment variables before running: |
| 10 | +- WASENDER_API_KEY: Your Wasender API key |
| 11 | +- WASENDER_ACCESS_TOKEN: Your personal access token (optional) |
| 12 | +
|
| 13 | +Usage: |
| 14 | + python example.py |
| 15 | +""" |
| 16 | + |
| 17 | +import os |
| 18 | +import asyncio |
| 19 | +from wasenderapi import create_sync_wasender, create_async_wasender |
| 20 | +from wasenderapi.models import RetryConfig |
| 21 | +from wasenderapi.errors import WasenderAPIError |
| 22 | + |
| 23 | + |
| 24 | +def get_api_credentials(): |
| 25 | + """Get API credentials from environment variables.""" |
| 26 | + api_key = os.getenv('WASENDER_API_KEY') |
| 27 | + access_token = os.getenv('WASENDER_ACCESS_TOKEN') |
| 28 | + |
| 29 | + if not api_key: |
| 30 | + raise ValueError( |
| 31 | + "WASENDER_API_KEY environment variable is required. " |
| 32 | + "Please set it to your Wasender API key." |
| 33 | + ) |
| 34 | + |
| 35 | + return api_key, access_token |
| 36 | + |
| 37 | + |
| 38 | +def sync_example(): |
| 39 | + """Example using the synchronous client.""" |
| 40 | + print("=== Synchronous Client Example ===") |
| 41 | + |
| 42 | + try: |
| 43 | + # Get credentials from environment |
| 44 | + api_key, access_token = get_api_credentials() |
| 45 | + |
| 46 | + # Create sync client with retry configuration |
| 47 | + retry_config = RetryConfig(enabled=True, max_retries=3) |
| 48 | + client = create_sync_wasender( |
| 49 | + api_key=api_key, |
| 50 | + personal_access_token=access_token, |
| 51 | + retry_options=retry_config |
| 52 | + ) |
| 53 | + |
| 54 | + # Example 1: Send a text message |
| 55 | + print("\n1. Sending text message...") |
| 56 | + try: |
| 57 | + # Replace with an actual phone number for testing |
| 58 | + phone_number = "+1234567890" # Update this with a real number |
| 59 | + message_text = "Hello from Wasender API SDK! 🚀" |
| 60 | + |
| 61 | + result = client.send_text( |
| 62 | + to=phone_number, |
| 63 | + text_body=message_text |
| 64 | + ) |
| 65 | + |
| 66 | + print(f"✅ Message sent successfully!") |
| 67 | + print(f" Status: {result.response.message}") |
| 68 | + |
| 69 | + # Show rate limit info |
| 70 | + if result.rate_limit: |
| 71 | + print(f" Rate limit: {result.rate_limit.remaining}/{result.rate_limit.limit} remaining") |
| 72 | + |
| 73 | + except WasenderAPIError as e: |
| 74 | + print(f"❌ Failed to send message: {e.message}") |
| 75 | + if e.status_code == 429: |
| 76 | + print(f" Rate limited. Retry after: {e.retry_after} seconds") |
| 77 | + elif e.error_details: |
| 78 | + print(f" Error details: {e.error_details}") |
| 79 | + # Example 2: Get all contacts |
| 80 | + print("\n2. Getting contacts...") |
| 81 | + try: |
| 82 | + contacts_result = client.get_contacts() |
| 83 | + |
| 84 | + print(f"✅ Retrieved contacts successfully!") |
| 85 | + print(f" Total contacts: {len(contacts_result.response.data)}") |
| 86 | + |
| 87 | + # Show first few contacts |
| 88 | + for i, contact in enumerate(contacts_result.response.data[:3]): |
| 89 | + contact_name = contact.name or "Unknown" |
| 90 | + contact_id = contact.id or "No ID" |
| 91 | + print(f" Contact {i+1}: {contact_name} ({contact_id})") |
| 92 | + |
| 93 | + if len(contacts_result.response.data) > 3: |
| 94 | + print(f" ... and {len(contacts_result.response.data) - 3} more") |
| 95 | + |
| 96 | + except WasenderAPIError as e: |
| 97 | + print(f"❌ Failed to get contacts: {e.message}") |
| 98 | + if e.status_code == 401: |
| 99 | + print(" Check your API key and access token") |
| 100 | + elif e.error_details: |
| 101 | + print(f" Error details: {e.error_details}") |
| 102 | + |
| 103 | + except ValueError as e: |
| 104 | + print(f"❌ Configuration error: {e}") |
| 105 | + except Exception as e: |
| 106 | + print(f"❌ Unexpected error: {e}") |
| 107 | + |
| 108 | + |
| 109 | +async def async_example(): |
| 110 | + """Example using the asynchronous client.""" |
| 111 | + print("\n=== Asynchronous Client Example ===") |
| 112 | + |
| 113 | + try: |
| 114 | + # Get credentials from environment |
| 115 | + api_key, access_token = get_api_credentials() |
| 116 | + |
| 117 | + # Create async client with retry configuration |
| 118 | + retry_config = RetryConfig(enabled=True, max_retries=3) |
| 119 | + |
| 120 | + async with create_async_wasender( |
| 121 | + api_key=api_key, |
| 122 | + personal_access_token=access_token, |
| 123 | + retry_options=retry_config |
| 124 | + ) as client: |
| 125 | + |
| 126 | + # Example 1: Send a text message |
| 127 | + print("\n1. Sending text message (async)...") |
| 128 | + try: |
| 129 | + # Replace with an actual phone number for testing |
| 130 | + phone_number = "+1234567890" # Update this with a real number |
| 131 | + message_text = "Hello from Wasender API SDK (async)! 🚀" |
| 132 | + |
| 133 | + result = await client.send_text( |
| 134 | + to=phone_number, |
| 135 | + text_body=message_text |
| 136 | + ) |
| 137 | + |
| 138 | + print(f"✅ Message sent successfully!") |
| 139 | + print(f" Status: {result.response.message}") |
| 140 | + |
| 141 | + # Show rate limit info |
| 142 | + if result.rate_limit: |
| 143 | + print(f" Rate limit: {result.rate_limit.remaining}/{result.rate_limit.limit} remaining") |
| 144 | + |
| 145 | + except WasenderAPIError as e: |
| 146 | + print(f"❌ Failed to send message: {e.message}") |
| 147 | + if e.status_code == 429: |
| 148 | + print(f" Rate limited. Retry after: {e.retry_after} seconds") |
| 149 | + elif e.error_details: |
| 150 | + print(f" Error details: {e.error_details}") |
| 151 | + # Example 2: Get all contacts |
| 152 | + print("\n2. Getting contacts (async)...") |
| 153 | + try: |
| 154 | + contacts_result = await client.get_contacts() |
| 155 | + |
| 156 | + print(f"✅ Retrieved contacts successfully!") |
| 157 | + print(f" Total contacts: {len(contacts_result.response.data)}") |
| 158 | + |
| 159 | + # Show first few contacts |
| 160 | + for i, contact in enumerate(contacts_result.response.data[:3]): |
| 161 | + contact_name = contact.name or "Unknown" |
| 162 | + contact_id = contact.id or "No ID" |
| 163 | + print(f" Contact {i+1}: {contact_name} ({contact_id})") |
| 164 | + |
| 165 | + if len(contacts_result.response.data) > 3: |
| 166 | + print(f" ... and {len(contacts_result.response.data) - 3} more") |
| 167 | + |
| 168 | + except WasenderAPIError as e: |
| 169 | + print(f"❌ Failed to get contacts: {e.message}") |
| 170 | + if e.status_code == 401: |
| 171 | + print(" Check your API key and access token") |
| 172 | + elif e.error_details: |
| 173 | + print(f" Error details: {e.error_details}") |
| 174 | + |
| 175 | + except ValueError as e: |
| 176 | + print(f"❌ Configuration error: {e}") |
| 177 | + except Exception as e: |
| 178 | + print(f"❌ Unexpected error: {e}") |
| 179 | + |
| 180 | + |
| 181 | +def main(): |
| 182 | + """Main function to run both sync and async examples.""" |
| 183 | + print("Wasender API SDK Example") |
| 184 | + print("=" * 40) |
| 185 | + |
| 186 | + # Check if environment variables are set |
| 187 | + api_key = os.getenv('WASENDER_API_KEY') |
| 188 | + if not api_key: |
| 189 | + print("❌ Environment variable WASENDER_API_KEY is not set!") |
| 190 | + print("\nTo run this example, please set the required environment variables:") |
| 191 | + print(" WASENDER_API_KEY=your_api_key_here") |
| 192 | + print(" WASENDER_ACCESS_TOKEN=your_access_token_here # Optional") |
| 193 | + print("\nExample:") |
| 194 | + print(" export WASENDER_API_KEY=wsk_...") |
| 195 | + print(" export WASENDER_ACCESS_TOKEN=pat_...") |
| 196 | + print(" python example.py") |
| 197 | + return |
| 198 | + |
| 199 | + print(f"✅ API Key found: {api_key[:10]}...") |
| 200 | + |
| 201 | + access_token = os.getenv('WASENDER_ACCESS_TOKEN') |
| 202 | + if access_token: |
| 203 | + print(f"✅ Access Token found: {access_token[:10]}...") |
| 204 | + else: |
| 205 | + print("ℹ️ Access Token not provided (optional)") |
| 206 | + |
| 207 | + # Run synchronous example |
| 208 | + sync_example() |
| 209 | + |
| 210 | + # Run asynchronous example |
| 211 | + asyncio.run(async_example()) |
| 212 | + |
| 213 | + print("\n" + "=" * 40) |
| 214 | + print("Examples completed!") |
| 215 | + |
| 216 | + |
| 217 | +if __name__ == "__main__": |
| 218 | + main() |
0 commit comments