-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_message.php
More file actions
79 lines (66 loc) · 2.01 KB
/
send_message.php
File metadata and controls
79 lines (66 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/**
* Send a WhatsApp message via WAzion API (PHP)
*
* Usage:
* WAZION_TOKEN=your_token php send_message.php
*/
$token = getenv('WAZION_TOKEN');
if (!$token) {
fwrite(STDERR, "Error: Set the WAZION_TOKEN environment variable.\n");
exit(1);
}
$apiUrl = 'https://www.wazion.com/api/mcp/';
function callTool(string $token, string $apiUrl, string $toolName, array $arguments): array
{
$payload = json_encode([
'jsonrpc' => '2.0',
'method' => 'tools/call',
'params' => [
'name' => $toolName,
'arguments' => $arguments,
],
'id' => 1,
]);
$ch = curl_init($apiUrl);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
"Authorization: Bearer {$token}",
],
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
throw new RuntimeException("cURL error: {$error}");
}
if ($httpCode !== 200) {
throw new RuntimeException("HTTP {$httpCode}: {$response}");
}
$data = json_decode($response, true);
if (isset($data['error'])) {
throw new RuntimeException("RPC Error: {$data['error']['message']}");
}
if (!empty($data['result']['isError'])) {
throw new RuntimeException("Tool Error: {$data['result']['content'][0]['text']}");
}
return $data['result'];
}
// Send a message
try {
$result = callTool($token, $apiUrl, 'send_whatsapp_message', [
'phone' => '+34600000000',
'message' => 'Hello from WAzion PHP!',
'session_id' => 1,
]);
echo "Success: " . $result['content'][0]['text'] . "\n";
} catch (RuntimeException $e) {
fwrite(STDERR, "Error: " . $e->getMessage() . "\n");
exit(1);
}