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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//! WhatsApp Agent
//!
//! Single bot instance — handles pairing, reconnection, and message processing.
//! Onboarding subscribes to QR/connected events via WhatsAppState.
use super::WhatsAppState;
use super::handler;
use crate::brain::agent::AgentService;
use crate::config::Config;
use crate::db::ChannelMessageRepository;
use crate::services::{ServiceContext, SessionService};
use std::sync::Arc;
use tokio::sync::Mutex;
use uuid::Uuid;
use super::store::Store;
use wacore::types::events::Event;
use whatsapp_rust::TokioRuntime;
use whatsapp_rust::bot::Bot;
use whatsapp_rust_tokio_transport::TokioWebSocketTransportFactory;
use whatsapp_rust_ureq_http_client::UreqHttpClient;
/// WhatsApp agent that forwards messages to the AgentService
pub struct WhatsAppAgent {
agent_service: Arc<AgentService>,
session_service: SessionService,
shared_session_id: Arc<Mutex<Option<Uuid>>>,
whatsapp_state: Arc<WhatsAppState>,
config_rx: tokio::sync::watch::Receiver<Config>,
channel_msg_repo: ChannelMessageRepository,
}
impl WhatsAppAgent {
pub fn new(
agent_service: Arc<AgentService>,
service_context: ServiceContext,
shared_session_id: Arc<Mutex<Option<Uuid>>>,
whatsapp_state: Arc<WhatsAppState>,
config_rx: tokio::sync::watch::Receiver<Config>,
channel_msg_repo: ChannelMessageRepository,
) -> Self {
Self {
agent_service,
session_service: SessionService::new(service_context),
shared_session_id,
whatsapp_state,
config_rx,
channel_msg_repo,
}
}
/// Start as a background task. Returns JoinHandle.
/// Always starts — if no session exists, emits QR events for onboarding.
/// If already paired, reconnects and handles messages.
pub fn start(self) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
let db_path = crate::config::opencrabs_home()
.join("whatsapp")
.join("session.db");
// Ensure parent directory exists
if let Some(parent) = db_path.parent() {
let _ = std::fs::create_dir_all(parent);
}
let backend = match Store::new(db_path.to_string_lossy().as_ref()).await {
Ok(store) => Arc::new(store),
Err(e) => {
let msg = format!(
"Failed to open session store at {}: {}",
db_path.display(),
e
);
tracing::error!("WhatsApp: {}", msg);
self.whatsapp_state.broadcast_error(&msg);
return;
}
};
let cfg = self.config_rx.borrow().clone();
tracing::info!(
"WhatsApp agent running (STT={}, TTS={})",
cfg.voice_config().stt_enabled,
cfg.voice_config().tts_enabled,
);
// Derive owner JID from first allowed phone (for proactive messaging)
let owner_jid = cfg
.channels
.whatsapp
.allowed_phones
.first()
.map(|p| format!("{}@s.whatsapp.net", p.trim_start_matches('+')));
let agent = self.agent_service.clone();
let session_svc = self.session_service.clone();
let shared_session = self.shared_session_id.clone();
let wa_state = self.whatsapp_state.clone();
let config_rx = self.config_rx.clone();
let channel_msg_repo = self.channel_msg_repo.clone();
let owner_jid_clone = owner_jid.clone();
let bot_result = Bot::builder()
.with_backend(backend)
.with_transport_factory(TokioWebSocketTransportFactory::new())
.with_http_client(UreqHttpClient::new())
.with_runtime(TokioRuntime)
.on_event(move |event, client| {
let agent = agent.clone();
let session_svc = session_svc.clone();
let shared_session = shared_session.clone();
let wa_state = wa_state.clone();
let owner_jid = owner_jid_clone.clone();
let config_rx = config_rx.clone();
let channel_msg_repo = channel_msg_repo.clone();
async move {
match &*event {
Event::PairingQrCode { code, .. } => {
tracing::info!(
"WhatsApp: QR code available (scan with your phone)"
);
wa_state.broadcast_qr(code);
}
Event::PairSuccess(s) => {
// The paired account's JID IS the owner (the
// person who scanned). Pin them as the
// authoritative owner so a config mismatch can
// never lock them out, and ensure they are in
// the allow list. Numbers are stored WITHOUT a
// leading '+' (matching sender_phone); the
// handler's wa_should_respond normalises both
// sides, so '+'-prefixed legacy entries still
// match.
let full = s.id.to_string();
let num = full
.split('@')
.next()
.unwrap_or(&full)
.split(':')
.next()
.unwrap_or(&full)
.trim_start_matches('+')
.to_string();
if num.is_empty() {
tracing::warn!(
"WhatsApp: pairing successful but could not extract \
owner number from JID '{full}'"
);
} else {
tracing::info!("WhatsApp: pairing successful — owner is {num}");
if let Err(e) = Config::write_key(
"channels.whatsapp",
"bot_owner",
&format!("[\"{num}\"]"),
) {
tracing::warn!(
"WhatsApp: failed to persist bot_owner: {e}"
);
}
// Append to allowed_phones if not already present.
let mut allowed: Vec<String> =
config_rx.borrow().channels.whatsapp.allowed_phones.clone();
let present =
allowed.iter().any(|a| a.trim_start_matches('+') == num);
if !present {
allowed.push(num.clone());
let json = format!(
"[{}]",
allowed
.iter()
.map(|a| format!("\"{a}\""))
.collect::<Vec<_>>()
.join(",")
);
if let Err(e) = Config::write_key(
"channels.whatsapp",
"allowed_phones",
&json,
) {
tracing::warn!(
"WhatsApp: failed to persist allowed_phones: {e}"
);
}
}
// Make the freshly-paired owner available to
// the Connected handler (which sends the
// confirmation greeting once the socket is
// ready) and to the whatsapp_send tool.
wa_state
.set_owner_jid(format!("{num}@s.whatsapp.net"))
.await;
}
}
Event::Connected(_) => {
tracing::info!("WhatsApp: connected successfully");
// Prefer the freshly-paired owner (set on
// PairSuccess) over the startup-derived one,
// which is None on a first-time pairing.
let owner = match wa_state.owner_jid().await {
Some(j) => Some(j),
None => owner_jid.clone(),
};
// Greet only on the FIRST connect of this run, not
// on every reconnect. Keepalive timeouts force
// frequent reconnects, each emitting Connected; the
// `connected` flag stays set across them (only a
// user reset clears it), so a true reading here
// means this is a reconnect and the greeting must
// be suppressed. Otherwise the owner is spammed
// with the same confirmation over and over.
let was_connected = wa_state.is_connected().await;
wa_state.set_connected(client.clone(), owner.clone()).await;
if was_connected {
tracing::debug!(
"WhatsApp: reconnected — suppressing duplicate \
confirmation greeting"
);
} else if let Some(jid) = owner {
// First connect: a real agent turn into the
// owner's self-chat. Spawned so the event loop
// is never blocked by a full agent turn.
let num = jid.split('@').next().unwrap_or(&jid).to_string();
tokio::spawn(handler::send_connection_greeting(
client.clone(),
agent.clone(),
session_svc.clone(),
wa_state.clone(),
num,
));
} else {
tracing::warn!(
"WhatsApp: connected but no owner number known — \
skipping confirmation greeting"
);
}
}
Event::Message(msg, info) => {
tracing::debug!("WhatsApp: Event::Message received");
// Spawned onto its own task: the agent turn is a
// very large async state machine, and polling it
// inline inside the event-loop future overflows
// the worker stack (and would block the loop).
tokio::spawn(handler::handle_message(
(**msg).clone(),
(**info).clone(),
client,
agent,
session_svc,
shared_session,
wa_state.clone(),
config_rx,
channel_msg_repo,
));
}
Event::LoggedOut(_) => {
tracing::warn!("WhatsApp: logged out");
}
Event::Disconnected(_) => {
tracing::warn!("WhatsApp: disconnected");
}
Event::Receipt(receipt) => {
// A message we sent was accepted/delivered.
// `Delivered` is the normal recipient receipt;
// `Sender` is what a self-chat send gets back —
// the bot is paired AS the owner, so its replies
// go to the owner's own devices, which ack with
// `sender`, not `delivered`. Either one means the
// message landed, so surface the id for the
// onboarding connection test.
if matches!(
receipt.r#type,
wacore::types::presence::ReceiptType::Delivered
| wacore::types::presence::ReceiptType::Sender
) {
for id in &receipt.message_ids {
wa_state.broadcast_delivered(id);
}
}
}
other => {
tracing::debug!("WhatsApp: unhandled event: {:?}", other);
}
}
}
})
.build()
.await;
let mut bot = match bot_result {
Ok(b) => b,
Err(e) => {
let msg = format!("Failed to build WhatsApp bot: {}", e);
tracing::error!("WhatsApp: {}", msg);
self.whatsapp_state.broadcast_error(&msg);
return;
}
};
match bot.run().await {
Ok(handle) => {
if let Err(e) = handle.await {
tracing::warn!("WhatsApp bot handle cancelled: {:?}", e);
}
}
Err(e) => {
let msg = format!("WhatsApp agent failed to start: {}", e);
tracing::error!("{}", msg);
self.whatsapp_state.broadcast_error(&msg);
}
}
})
}
}