-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathecho-bot.ts
More file actions
68 lines (57 loc) · 1.7 KB
/
Copy pathecho-bot.ts
File metadata and controls
68 lines (57 loc) · 1.7 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
/**
* Echo Bot Example
*
* A simple bot that echoes back any message it receives.
* Perfect for testing WaSP integration and message flow.
*
* Usage:
* npx tsx examples/echo-bot.ts
*
* 1. Scan QR code with WhatsApp
* 2. Send a message to the bot
* 3. Bot echoes it back
*/
import { WaSP } from '../src';
async function main() {
// Create WaSP instance with debug logging
const wasp = new WaSP({
debug: true,
queue: {
minDelay: 1000, // 1s min delay
maxDelay: 2000, // 2s max delay
},
});
console.log('Starting Echo Bot...');
// Create session
const session = await wasp.createSession('echo-bot', 'BAILEYS');
console.log('Session created:', session.id);
// Listen for QR code
wasp.on('SESSION_QR', (event) => {
console.log('\n📱 Scan this QR code with WhatsApp:\n');
console.log(event.data.qr);
});
// Listen for connection
wasp.on('SESSION_CONNECTED', (event) => {
console.log('✅ Connected as:', event.data.phone);
console.log('Echo bot is ready! Send any message to test.\n');
});
// Echo incoming messages
wasp.on('MESSAGE_RECEIVED', async (event) => {
const msg = event.data;
console.log(`📨 Received: "${msg.content}" from ${msg.from}`);
// Echo back
await wasp.sendMessage(event.sessionId, msg.from, `Echo: ${msg.content}`);
console.log(`📤 Echoed back to ${msg.from}`);
});
// Handle errors
wasp.on('SESSION_ERROR', (event) => {
console.error('❌ Session error:', event.data.error);
});
// Keep process alive
process.on('SIGINT', async () => {
console.log('\n👋 Shutting down...');
await wasp.destroySession('echo-bot');
process.exit(0);
});
}
main().catch(console.error);