Integration Guide

Step-by-step guides for popular platforms and frameworks

Node.js Integration

Installation

npm install axios

Example Code

const axios = require('axios');

const apiKey = 'YOUR_API_KEY';
const baseURL = 'https://tawasol.site/api';

async function sendMessage(deviceId, to, message) {
  try {
    const response = await axios.post(
      `${baseURL}/send-message`,
      {
        device_id: deviceId,
        to: to,
        message: message
      },
      {
        headers: {
          'Authorization': `Bearer ${apiKey}`,
          'Content-Type': 'application/json'
        }
      }
    );
    console.log('Message sent:', response.data);
    return response.data;
  } catch (error) {
    console.error('Error:', error.response.data);
    throw error;
  }
}

// Usage
sendMessage('device_123', '1234567890', 'Hello from Node.js!');

Python Integration

Installation

pip install requests

Example Code

import requests

API_KEY = 'YOUR_API_KEY'
BASE_URL = 'https://tawasol.site/api'

def send_message(device_id, to, message):
    url = f"{BASE_URL}/send-message"
    headers = {
        'Authorization': f'Bearer {API_KEY}',
        'Content-Type': 'application/json'
    }
    data = {
        'device_id': device_id,
        'to': to,
        'message': message
    }
    
    response = requests.post(url, json=data, headers=headers)
    response.raise_for_status()
    return response.json()

# Usage
result = send_message('device_123', '1234567890', 'Hello from Python!')
print(result)

PHP Integration

Example Code

<?php

$apiKey = 'YOUR_API_KEY';
$baseURL = 'https://tawasol.site/api';

function sendMessage($deviceId, $to, $message) {
    global $apiKey, $baseURL;
    
    $url = $baseURL . '/send-message';
    $data = [
        'device_id' => $deviceId,
        'to' => $to,
        'message' => $message
    ];
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Authorization: Bearer ' . $apiKey,
        'Content-Type: application/json'
    ]);
    
    $response = curl_exec($ch);
    curl_close($ch);
    
    return json_decode($response, true);
}

// Usage
$result = sendMessage('device_123', '1234567890', 'Hello from PHP!');
print_r($result);

?>

Laravel Integration

Service Class

<?php

namespace App\Services;

use Illuminate\Support\Facades\Http;

class WhatsAppService
{
    private $apiKey;
    private $baseURL;
    
    public function __construct()
    {
        $this->apiKey = config('services.whatsapp.api_key');
        $this->baseURL = config('services.whatsapp.base_url');
    }
    
    public function sendMessage($deviceId, $to, $message)
    {
        $response = Http::withHeaders([
            'Authorization' => 'Bearer ' . $this->apiKey,
            'Content-Type' => 'application/json'
        ])->post($this->baseURL . '/send-message', [
            'device_id' => $deviceId,
            'to' => $to,
            'message' => $message
        ]);
        
        return $response->json();
    }
}

Usage in Controller

use App\Services\WhatsAppService;

class MessageController extends Controller
{
    public function send(WhatsAppService $whatsapp)
    {
        $result = $whatsapp->sendMessage(
            'device_123',
            '1234567890',
            'Hello from Laravel!'
        );
        
        return response()->json($result);
    }
}

Webhook Integration

Setting Up Webhooks

Configure webhooks to receive real-time notifications about events:

POST /api/webhook
{
  "device_id": "device_123",
  "url": "https://your-domain.com/webhook",
  "events": ["message", "status", "device"]
}

Webhook Payload Example

{
  "event": "message.received",
  "device_id": "device_123",
  "data": {
    "from": "1234567890",
    "message": "Hello!",
    "timestamp": "2024-01-01T12:00:00Z"
  }
}

Best Practices

  • Error Handling: Always implement proper error handling and retry logic for failed requests.
  • Rate Limiting: Respect rate limits and implement exponential backoff.
  • Security: Store API keys securely, never commit them to version control.
  • Webhooks: Verify webhook signatures to ensure requests are authentic.
  • Logging: Log API requests and responses for debugging and monitoring.