commune-cli 0.1.6__tar.gz → 0.1.9__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. {commune_cli-0.1.6 → commune_cli-0.1.9}/.gitignore +8 -1
  2. {commune_cli-0.1.6 → commune_cli-0.1.9}/PKG-INFO +1 -1
  3. {commune_cli-0.1.6 → commune_cli-0.1.9}/commune_cli/commands/phone_numbers.py +96 -0
  4. commune_cli-0.1.9/commune_cli/commands/sms.py +353 -0
  5. {commune_cli-0.1.6 → commune_cli-0.1.9}/pyproject.toml +1 -1
  6. commune_cli-0.1.6/commune_cli/commands/sms.py +0 -187
  7. {commune_cli-0.1.6 → commune_cli-0.1.9}/README.md +0 -0
  8. {commune_cli-0.1.6 → commune_cli-0.1.9}/commune_cli/__init__.py +0 -0
  9. {commune_cli-0.1.6 → commune_cli-0.1.9}/commune_cli/banner.py +0 -0
  10. {commune_cli-0.1.6 → commune_cli-0.1.9}/commune_cli/client.py +0 -0
  11. {commune_cli-0.1.6 → commune_cli-0.1.9}/commune_cli/commands/__init__.py +0 -0
  12. {commune_cli-0.1.6 → commune_cli-0.1.9}/commune_cli/commands/attachments.py +0 -0
  13. {commune_cli-0.1.6 → commune_cli-0.1.9}/commune_cli/commands/config_cmd.py +0 -0
  14. {commune_cli-0.1.6 → commune_cli-0.1.9}/commune_cli/commands/credits.py +0 -0
  15. {commune_cli-0.1.6 → commune_cli-0.1.9}/commune_cli/commands/data.py +0 -0
  16. {commune_cli-0.1.6 → commune_cli-0.1.9}/commune_cli/commands/delivery.py +0 -0
  17. {commune_cli-0.1.6 → commune_cli-0.1.9}/commune_cli/commands/dmarc.py +0 -0
  18. {commune_cli-0.1.6 → commune_cli-0.1.9}/commune_cli/commands/domains.py +0 -0
  19. {commune_cli-0.1.6 → commune_cli-0.1.9}/commune_cli/commands/inboxes.py +0 -0
  20. {commune_cli-0.1.6 → commune_cli-0.1.9}/commune_cli/commands/messages.py +0 -0
  21. {commune_cli-0.1.6 → commune_cli-0.1.9}/commune_cli/commands/search.py +0 -0
  22. {commune_cli-0.1.6 → commune_cli-0.1.9}/commune_cli/commands/threads.py +0 -0
  23. {commune_cli-0.1.6 → commune_cli-0.1.9}/commune_cli/commands/webhooks.py +0 -0
  24. {commune_cli-0.1.6 → commune_cli-0.1.9}/commune_cli/config.py +0 -0
  25. {commune_cli-0.1.6 → commune_cli-0.1.9}/commune_cli/errors.py +0 -0
  26. {commune_cli-0.1.6 → commune_cli-0.1.9}/commune_cli/main.py +0 -0
  27. {commune_cli-0.1.6 → commune_cli-0.1.9}/commune_cli/output.py +0 -0
  28. {commune_cli-0.1.6 → commune_cli-0.1.9}/commune_cli/state.py +0 -0
  29. {commune_cli-0.1.6 → commune_cli-0.1.9}/uv.lock +0 -0
@@ -79,4 +79,11 @@ build/
79
79
  .mypy_cache/
80
80
  .ruff_cache/
81
81
  *.egg-info/
82
- pip-wheel-metadata/
82
+ pip-wheel-metadata/
83
+
84
+ # Large model/binary files (GitHub 100MB limit)
85
+ launch-video/whisper.cpp/
86
+ launch-video/documents/
87
+
88
+ # OpenNext build output
89
+ frontend/.open-next/
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: commune-cli
3
- Version: 0.1.6
3
+ Version: 0.1.9
4
4
  Summary: Official CLI for the Commune email API — agent-native, pipe-friendly, covers every API surface.
5
5
  Project-URL: Homepage, https://commune.email
6
6
  Project-URL: Documentation, https://docs.commune.email
@@ -199,3 +199,99 @@ def phone_numbers_settings(
199
199
  api_error(r, json_output=json_output or state.should_json())
200
200
 
201
201
  print_record(r.json(), json_output=json_output or state.should_json(), title="SMS Settings")
202
+
203
+
204
+ @app.command("update")
205
+ def phone_numbers_update(
206
+ ctx: typer.Context,
207
+ phone_number_id: str = typer.Argument(..., help="Phone number ID to update."),
208
+ friendly_name: Optional[str] = typer.Option(None, "--friendly-name", help="Human-readable label for this number."),
209
+ auto_reply: Optional[str] = typer.Option(None, "--auto-reply", help="Auto-reply message body (empty string to disable)."),
210
+ auto_reply_enabled: Optional[bool] = typer.Option(None, "--auto-reply-enabled/--no-auto-reply-enabled", help="Enable or disable auto-reply."),
211
+ webhook_endpoint: Optional[str] = typer.Option(None, "--webhook-endpoint", help="HTTPS URL to receive SMS event webhooks."),
212
+ webhook_secret: Optional[str] = typer.Option(None, "--webhook-secret", help="Webhook signing secret."),
213
+ json_output: bool = typer.Option(False, "--json", help="Output JSON."),
214
+ ) -> None:
215
+ """Update a phone number's settings. PATCH /v1/phone-numbers/{id}."""
216
+ state: AppState = ctx.obj or AppState()
217
+ if not state.has_any_auth():
218
+ auth_required_error(json_output=json_output or state.should_json())
219
+
220
+ payload: dict = {}
221
+ if friendly_name is not None:
222
+ payload["friendly_name"] = friendly_name
223
+ if auto_reply is not None or auto_reply_enabled is not None:
224
+ auto_reply_obj: dict = {}
225
+ if auto_reply_enabled is not None:
226
+ auto_reply_obj["enabled"] = auto_reply_enabled
227
+ if auto_reply is not None:
228
+ auto_reply_obj["body"] = auto_reply
229
+ payload["auto_reply"] = auto_reply_obj
230
+ if webhook_endpoint is not None:
231
+ webhook: dict = {"endpoint": webhook_endpoint}
232
+ if webhook_secret is not None:
233
+ webhook["secret"] = webhook_secret
234
+ payload["webhook"] = webhook
235
+
236
+ if not payload:
237
+ from ..errors import validation_error
238
+ validation_error("No fields to update. Provide at least one option.", json_output=json_output or state.should_json())
239
+
240
+ client = CommuneClient.from_state(state)
241
+ try:
242
+ r = client.patch(f"/v1/phone-numbers/{phone_number_id}", json=payload)
243
+ except Exception as exc:
244
+ network_error(exc, json_output=json_output or state.should_json())
245
+
246
+ if not r.is_success:
247
+ api_error(r, json_output=json_output or state.should_json())
248
+
249
+ print_record(r.json(), json_output=json_output or state.should_json(), title="Updated Phone Number")
250
+
251
+
252
+ @app.command("set-allow-list")
253
+ def phone_numbers_set_allow_list(
254
+ ctx: typer.Context,
255
+ phone_number_id: str = typer.Argument(..., help="Phone number ID."),
256
+ numbers: list[str] = typer.Option(..., "--number", help="E.164 number to allow (repeat for multiple, e.g. --number +15551234567 --number +15559876543). Pass no --number flags to clear."),
257
+ json_output: bool = typer.Option(False, "--json", help="Output JSON."),
258
+ ) -> None:
259
+ """Set allow list for a phone number (only listed numbers can message it). PUT /v1/phone-numbers/{id}/allow-list."""
260
+ state: AppState = ctx.obj or AppState()
261
+ if not state.has_any_auth():
262
+ auth_required_error(json_output=json_output or state.should_json())
263
+
264
+ client = CommuneClient.from_state(state)
265
+ try:
266
+ r = client.put(f"/v1/phone-numbers/{phone_number_id}/allow-list", json={"numbers": numbers})
267
+ except Exception as exc:
268
+ network_error(exc, json_output=json_output or state.should_json())
269
+
270
+ if not r.is_success:
271
+ api_error(r, json_output=json_output or state.should_json())
272
+
273
+ print_record(r.json(), json_output=json_output or state.should_json(), title="Updated Phone Number")
274
+
275
+
276
+ @app.command("set-block-list")
277
+ def phone_numbers_set_block_list(
278
+ ctx: typer.Context,
279
+ phone_number_id: str = typer.Argument(..., help="Phone number ID."),
280
+ numbers: list[str] = typer.Option(..., "--number", help="E.164 number to block (repeat for multiple). Pass no --number flags to clear."),
281
+ json_output: bool = typer.Option(False, "--json", help="Output JSON."),
282
+ ) -> None:
283
+ """Set block list for a phone number (listed numbers are rejected). PUT /v1/phone-numbers/{id}/block-list."""
284
+ state: AppState = ctx.obj or AppState()
285
+ if not state.has_any_auth():
286
+ auth_required_error(json_output=json_output or state.should_json())
287
+
288
+ client = CommuneClient.from_state(state)
289
+ try:
290
+ r = client.put(f"/v1/phone-numbers/{phone_number_id}/block-list", json={"numbers": numbers})
291
+ except Exception as exc:
292
+ network_error(exc, json_output=json_output or state.should_json())
293
+
294
+ if not r.is_success:
295
+ api_error(r, json_output=json_output or state.should_json())
296
+
297
+ print_record(r.json(), json_output=json_output or state.should_json(), title="Updated Phone Number")
@@ -0,0 +1,353 @@
1
+ """commune sms — send SMS and manage conversations."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Optional
6
+ from urllib.parse import quote
7
+
8
+ import typer
9
+
10
+ from ..client import CommuneClient
11
+ from ..errors import api_error, auth_required_error, network_error, validation_error
12
+ from ..output import print_json, print_list, print_record, print_success
13
+ from ..state import AppState
14
+
15
+ app = typer.Typer(help="Send SMS and manage conversations.", no_args_is_help=True)
16
+
17
+
18
+ @app.command("send")
19
+ def sms_send(
20
+ ctx: typer.Context,
21
+ to: str = typer.Option(..., "--to", help="Recipient phone number in E.164 format (e.g. +1234567890)."),
22
+ body: str = typer.Option(..., "--body", help="SMS message body."),
23
+ phone_number_id: Optional[str] = typer.Option(
24
+ None,
25
+ "--phone-number-id",
26
+ help="Phone number ID to send from. Uses org default if omitted.",
27
+ ),
28
+ json_output: bool = typer.Option(False, "--json", help="Output JSON."),
29
+ ) -> None:
30
+ """Send an SMS message. POST /v1/sms/send."""
31
+ state: AppState = ctx.obj or AppState()
32
+ if not state.has_any_auth():
33
+ auth_required_error(json_output=json_output or state.should_json())
34
+
35
+ payload: dict = {"to": to, "body": body}
36
+ if phone_number_id:
37
+ payload["phone_number_id"] = phone_number_id
38
+
39
+ client = CommuneClient.from_state(state)
40
+ try:
41
+ r = client.post("/v1/sms/send", json=payload)
42
+ except Exception as exc:
43
+ network_error(exc, json_output=json_output or state.should_json())
44
+
45
+ if not r.is_success:
46
+ api_error(r, json_output=json_output or state.should_json())
47
+
48
+ data = r.json()
49
+ if json_output or state.should_json():
50
+ print_json(data)
51
+ return
52
+
53
+ msg_id = (data.get("data") or {}).get("message_id", "")
54
+ print_success(f"SMS sent. ID: [bold]{msg_id}[/bold]")
55
+
56
+
57
+ @app.command("conversations")
58
+ def sms_conversations(
59
+ ctx: typer.Context,
60
+ phone_number_id: Optional[str] = typer.Option(
61
+ None,
62
+ "--phone-number-id",
63
+ help="Filter by phone number ID.",
64
+ ),
65
+ limit: Optional[int] = typer.Option(20, "--limit", help="Maximum results to return."),
66
+ json_output: bool = typer.Option(False, "--json", help="Output JSON."),
67
+ ) -> None:
68
+ """List SMS conversations. GET /v1/sms/conversations."""
69
+ state: AppState = ctx.obj or AppState()
70
+ if not state.has_any_auth():
71
+ auth_required_error(json_output=json_output or state.should_json())
72
+
73
+ client = CommuneClient.from_state(state)
74
+ try:
75
+ r = client.get("/v1/sms/conversations", params={
76
+ "phone_number_id": phone_number_id,
77
+ "limit": limit,
78
+ })
79
+ except Exception as exc:
80
+ network_error(exc, json_output=json_output or state.should_json())
81
+
82
+ if not r.is_success:
83
+ api_error(r, json_output=json_output or state.should_json())
84
+
85
+ print_list(
86
+ r.json(),
87
+ json_output=json_output or state.should_json(),
88
+ title="SMS Conversations",
89
+ columns=[
90
+ ("Remote Number", "remote_number"),
91
+ ("Last Message", "last_message_preview"),
92
+ ("Messages", "message_count"),
93
+ ("Updated", "last_message_at"),
94
+ ],
95
+ )
96
+
97
+
98
+ @app.command("thread")
99
+ def sms_thread(
100
+ ctx: typer.Context,
101
+ remote_number: str = typer.Argument(..., help="Remote phone number (E.164, e.g. +1234567890)."),
102
+ phone_number_id: str = typer.Option(
103
+ ...,
104
+ "--phone-number-id",
105
+ help="Phone number ID for the conversation.",
106
+ ),
107
+ json_output: bool = typer.Option(False, "--json", help="Output JSON."),
108
+ ) -> None:
109
+ """Get the message thread with a specific number. GET /v1/sms/conversations/{remoteNumber}."""
110
+ state: AppState = ctx.obj or AppState()
111
+ if not state.has_any_auth():
112
+ auth_required_error(json_output=json_output or state.should_json())
113
+
114
+ encoded = quote(remote_number, safe="")
115
+ client = CommuneClient.from_state(state)
116
+ try:
117
+ r = client.get(
118
+ f"/v1/sms/conversations/{encoded}",
119
+ params={"phone_number_id": phone_number_id},
120
+ )
121
+ except Exception as exc:
122
+ network_error(exc, json_output=json_output or state.should_json())
123
+
124
+ if not r.is_success:
125
+ api_error(r, json_output=json_output or state.should_json())
126
+
127
+ data = r.json()
128
+ if json_output or state.should_json():
129
+ print_json(data)
130
+ return
131
+
132
+ messages = data if isinstance(data, list) else data.get("data", data)
133
+ print_list(
134
+ messages,
135
+ json_output=False,
136
+ title=f"Thread with {remote_number}",
137
+ columns=[
138
+ ("ID", "message_id"),
139
+ ("Direction", "direction"),
140
+ ("Body", "content"),
141
+ ("Sent At", "created_at"),
142
+ ],
143
+ )
144
+
145
+
146
+ @app.command("search")
147
+ def sms_search(
148
+ ctx: typer.Context,
149
+ query: str = typer.Argument(..., help="Search query."),
150
+ phone_number_id: Optional[str] = typer.Option(
151
+ None,
152
+ "--phone-number-id",
153
+ help="Scope search to a specific phone number.",
154
+ ),
155
+ limit: Optional[int] = typer.Option(20, "--limit", help="Maximum results to return."),
156
+ json_output: bool = typer.Option(False, "--json", help="Output JSON."),
157
+ ) -> None:
158
+ """Search SMS messages. GET /v1/sms/search."""
159
+ state: AppState = ctx.obj or AppState()
160
+ if not state.has_any_auth():
161
+ auth_required_error(json_output=json_output or state.should_json())
162
+
163
+ client = CommuneClient.from_state(state)
164
+ try:
165
+ r = client.get("/v1/sms/search", params={
166
+ "q": query,
167
+ "phone_number_id": phone_number_id,
168
+ "limit": limit,
169
+ })
170
+ except Exception as exc:
171
+ network_error(exc, json_output=json_output or state.should_json())
172
+
173
+ if not r.is_success:
174
+ api_error(r, json_output=json_output or state.should_json())
175
+
176
+ print_list(
177
+ r.json(),
178
+ json_output=json_output or state.should_json(),
179
+ title=f"SMS Search: {query}",
180
+ columns=[
181
+ ("ID", "message_id"),
182
+ ("Direction", "direction"),
183
+ ("Body", "content"),
184
+ ("Date", "created_at"),
185
+ ],
186
+ )
187
+
188
+
189
+ @app.command("suppressions")
190
+ def sms_suppressions(
191
+ ctx: typer.Context,
192
+ phone_number_id: Optional[str] = typer.Option(
193
+ None,
194
+ "--phone-number-id",
195
+ help="Filter by phone number ID.",
196
+ ),
197
+ json_output: bool = typer.Option(False, "--json", help="Output JSON."),
198
+ ) -> None:
199
+ """List SMS suppressed numbers (opted out via STOP). GET /v1/sms/suppressions."""
200
+ state: AppState = ctx.obj or AppState()
201
+ if not state.has_any_auth():
202
+ auth_required_error(json_output=json_output or state.should_json())
203
+
204
+ client = CommuneClient.from_state(state)
205
+ try:
206
+ r = client.get("/v1/sms/suppressions", params={"phone_number_id": phone_number_id})
207
+ except Exception as exc:
208
+ network_error(exc, json_output=json_output or state.should_json())
209
+
210
+ if not r.is_success:
211
+ api_error(r, json_output=json_output or state.should_json())
212
+
213
+ print_list(
214
+ r.json(),
215
+ json_output=json_output or state.should_json(),
216
+ title="SMS Suppressions",
217
+ columns=[
218
+ ("Phone Number", "phone_number"),
219
+ ("Reason", "reason"),
220
+ ("Created", "created_at"),
221
+ ],
222
+ )
223
+
224
+
225
+ @app.command("remove-suppression")
226
+ def sms_remove_suppression(
227
+ ctx: typer.Context,
228
+ phone_number: str = typer.Argument(..., help="E.164 phone number to remove from suppression list (e.g. +15551234567)."),
229
+ json_output: bool = typer.Option(False, "--json", help="Output JSON."),
230
+ ) -> None:
231
+ """Remove a number from the SMS suppression list. DELETE /v1/sms/suppressions/{phoneNumber}."""
232
+ state: AppState = ctx.obj or AppState()
233
+ if not state.has_any_auth():
234
+ auth_required_error(json_output=json_output or state.should_json())
235
+
236
+ from urllib.parse import quote
237
+ encoded = quote(phone_number, safe="")
238
+ client = CommuneClient.from_state(state)
239
+ try:
240
+ r = client.delete(f"/v1/sms/suppressions/{encoded}")
241
+ except Exception as exc:
242
+ network_error(exc, json_output=json_output or state.should_json())
243
+
244
+ if not r.is_success:
245
+ api_error(r, json_output=json_output or state.should_json())
246
+
247
+ if json_output or state.should_json():
248
+ from ..output import print_json
249
+ print_json(r.json())
250
+ return
251
+
252
+ print_success(f"[bold]{phone_number}[/bold] removed from suppression list.")
253
+
254
+
255
+ @app.command("listen")
256
+ def sms_listen(
257
+ ctx: typer.Context,
258
+ phone_number_id: Optional[str] = typer.Option(
259
+ None,
260
+ "--phone-number-id",
261
+ help="Listen for events on a specific phone number. Omit to listen on all.",
262
+ ),
263
+ events: Optional[str] = typer.Option(
264
+ None,
265
+ "--events",
266
+ help="Comma-separated event types to filter: sms.received,sms.sent,sms.status_updated (default: all SMS events).",
267
+ ),
268
+ ) -> None:
269
+ """Stream SMS events in real time (like az logs). Ctrl+C to stop."""
270
+ import sys
271
+ import time
272
+ from rich.console import Console
273
+ from rich.text import Text
274
+
275
+ state: AppState = ctx.obj or AppState()
276
+ if not state.has_any_auth():
277
+ auth_required_error(json_output=False)
278
+
279
+ console = Console()
280
+
281
+ base_url = state.base_url or "https://api.commune.email"
282
+ api_key = state.api_key or state.session_token or ""
283
+
284
+ params: list[tuple[str, str]] = []
285
+ if phone_number_id:
286
+ params.append(("phone_number_id", phone_number_id))
287
+ if events:
288
+ params.append(("events", events))
289
+ else:
290
+ params.append(("events", "sms.received,sms.sent,sms.status_updated"))
291
+
292
+ import httpx
293
+ from urllib.parse import urlencode
294
+
295
+ qs = urlencode(params)
296
+ url = f"{base_url.rstrip('/')}/v1/events/stream?{qs}"
297
+
298
+ label = f"phone number [bold]{phone_number_id}[/bold]" if phone_number_id else "all phone numbers"
299
+ console.print(f"\n[dim]Listening for SMS events on {label}... (Ctrl+C to stop)[/dim]\n")
300
+
301
+ try:
302
+ with httpx.Client(timeout=None) as client:
303
+ with client.stream(
304
+ "GET",
305
+ url,
306
+ headers={"Authorization": f"Bearer {api_key}", "Accept": "text/event-stream"},
307
+ ) as response:
308
+ if response.status_code != 200:
309
+ console.print(f"[red]Error {response.status_code}:[/red] {response.text}")
310
+ raise SystemExit(1)
311
+
312
+ event_type = None
313
+ for line in response.iter_lines():
314
+ if line.startswith("event:"):
315
+ event_type = line[6:].strip()
316
+ elif line.startswith("data:"):
317
+ raw = line[5:].strip()
318
+ if raw == "[DONE]" or raw == "":
319
+ continue
320
+ try:
321
+ import json as _json
322
+ data = _json.loads(raw)
323
+ except Exception:
324
+ continue
325
+
326
+ ts = data.get("created_at", time.strftime("%H:%M:%S"))
327
+ if len(ts) > 8:
328
+ ts = ts[11:19] if "T" in ts else ts[:8]
329
+
330
+ ev = event_type or data.get("type", "event")
331
+ from_num = data.get("from_number", data.get("from", ""))
332
+ to_num = data.get("to_number", data.get("to", ""))
333
+ body = data.get("content", data.get("body", ""))
334
+ status = data.get("delivery_status", "")
335
+
336
+ color = "green" if ev == "sms.received" else "blue" if ev == "sms.sent" else "yellow"
337
+ direction = f"[dim]{from_num}[/dim] → [dim]{to_num}[/dim]" if from_num and to_num else ""
338
+ status_str = f" · [dim]{status}[/dim]" if status else ""
339
+
340
+ console.print(
341
+ f"[[dim]{ts}[/dim]] [{color}]{ev:<20}[/{color}] {direction}{status_str}"
342
+ )
343
+ if body:
344
+ preview = body[:100] + ("…" if len(body) > 100 else "")
345
+ console.print(f" [dim]\"{preview}\"[/dim]")
346
+ elif line == "":
347
+ event_type = None
348
+
349
+ except KeyboardInterrupt:
350
+ console.print("\n[dim]Stopped.[/dim]")
351
+ except Exception as exc:
352
+ console.print(f"[red]Connection error:[/red] {exc}")
353
+ raise SystemExit(1)
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "commune-cli"
7
- version = "0.1.6"
7
+ version = "0.1.9"
8
8
  description = "Official CLI for the Commune email API — agent-native, pipe-friendly, covers every API surface."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.9"
@@ -1,187 +0,0 @@
1
- """commune sms — send SMS and manage conversations."""
2
-
3
- from __future__ import annotations
4
-
5
- from typing import Optional
6
- from urllib.parse import quote
7
-
8
- import typer
9
-
10
- from ..client import CommuneClient
11
- from ..errors import api_error, auth_required_error, network_error, validation_error
12
- from ..output import print_json, print_list, print_record, print_success
13
- from ..state import AppState
14
-
15
- app = typer.Typer(help="Send SMS and manage conversations.", no_args_is_help=True)
16
-
17
-
18
- @app.command("send")
19
- def sms_send(
20
- ctx: typer.Context,
21
- to: str = typer.Option(..., "--to", help="Recipient phone number in E.164 format (e.g. +1234567890)."),
22
- body: str = typer.Option(..., "--body", help="SMS message body."),
23
- phone_number_id: Optional[str] = typer.Option(
24
- None,
25
- "--phone-number-id",
26
- help="Phone number ID to send from. Uses org default if omitted.",
27
- ),
28
- json_output: bool = typer.Option(False, "--json", help="Output JSON."),
29
- ) -> None:
30
- """Send an SMS message. POST /v1/sms/send."""
31
- state: AppState = ctx.obj or AppState()
32
- if not state.has_any_auth():
33
- auth_required_error(json_output=json_output or state.should_json())
34
-
35
- payload: dict = {"to": to, "body": body}
36
- if phone_number_id:
37
- payload["phone_number_id"] = phone_number_id
38
-
39
- client = CommuneClient.from_state(state)
40
- try:
41
- r = client.post("/v1/sms/send", json=payload)
42
- except Exception as exc:
43
- network_error(exc, json_output=json_output or state.should_json())
44
-
45
- if not r.is_success:
46
- api_error(r, json_output=json_output or state.should_json())
47
-
48
- data = r.json()
49
- if json_output or state.should_json():
50
- print_json(data)
51
- return
52
-
53
- msg_id = data.get("id") or data.get("messageId", "")
54
- print_success(f"SMS sent. ID: [bold]{msg_id}[/bold]")
55
-
56
-
57
- @app.command("conversations")
58
- def sms_conversations(
59
- ctx: typer.Context,
60
- phone_number_id: Optional[str] = typer.Option(
61
- None,
62
- "--phone-number-id",
63
- help="Filter by phone number ID.",
64
- ),
65
- limit: Optional[int] = typer.Option(20, "--limit", help="Maximum results to return."),
66
- json_output: bool = typer.Option(False, "--json", help="Output JSON."),
67
- ) -> None:
68
- """List SMS conversations. GET /v1/sms/conversations."""
69
- state: AppState = ctx.obj or AppState()
70
- if not state.has_any_auth():
71
- auth_required_error(json_output=json_output or state.should_json())
72
-
73
- client = CommuneClient.from_state(state)
74
- try:
75
- r = client.get("/v1/sms/conversations", params={
76
- "phone_number_id": phone_number_id,
77
- "limit": limit,
78
- })
79
- except Exception as exc:
80
- network_error(exc, json_output=json_output or state.should_json())
81
-
82
- if not r.is_success:
83
- api_error(r, json_output=json_output or state.should_json())
84
-
85
- print_list(
86
- r.json(),
87
- json_output=json_output or state.should_json(),
88
- title="SMS Conversations",
89
- columns=[
90
- ("Remote Number", "remoteNumber"),
91
- ("Last Message", "lastMessage"),
92
- ("Direction", "lastDirection"),
93
- ("Updated", "updatedAt"),
94
- ],
95
- )
96
-
97
-
98
- @app.command("thread")
99
- def sms_thread(
100
- ctx: typer.Context,
101
- remote_number: str = typer.Argument(..., help="Remote phone number (E.164, e.g. +1234567890)."),
102
- phone_number_id: str = typer.Option(
103
- ...,
104
- "--phone-number-id",
105
- help="Phone number ID for the conversation.",
106
- ),
107
- json_output: bool = typer.Option(False, "--json", help="Output JSON."),
108
- ) -> None:
109
- """Get the message thread with a specific number. GET /v1/sms/conversations/{remoteNumber}."""
110
- state: AppState = ctx.obj or AppState()
111
- if not state.has_any_auth():
112
- auth_required_error(json_output=json_output or state.should_json())
113
-
114
- encoded = quote(remote_number, safe="")
115
- client = CommuneClient.from_state(state)
116
- try:
117
- r = client.get(
118
- f"/v1/sms/conversations/{encoded}",
119
- params={"phone_number_id": phone_number_id},
120
- )
121
- except Exception as exc:
122
- network_error(exc, json_output=json_output or state.should_json())
123
-
124
- if not r.is_success:
125
- api_error(r, json_output=json_output or state.should_json())
126
-
127
- data = r.json()
128
- if json_output or state.should_json():
129
- print_json(data)
130
- return
131
-
132
- messages = data if isinstance(data, list) else data.get("data", data)
133
- print_list(
134
- messages,
135
- json_output=False,
136
- title=f"Thread with {remote_number}",
137
- columns=[
138
- ("ID", "id"),
139
- ("Direction", "direction"),
140
- ("Body", "body"),
141
- ("Sent At", "createdAt"),
142
- ],
143
- )
144
-
145
-
146
- @app.command("search")
147
- def sms_search(
148
- ctx: typer.Context,
149
- query: str = typer.Argument(..., help="Search query."),
150
- phone_number_id: Optional[str] = typer.Option(
151
- None,
152
- "--phone-number-id",
153
- help="Scope search to a specific phone number.",
154
- ),
155
- limit: Optional[int] = typer.Option(20, "--limit", help="Maximum results to return."),
156
- json_output: bool = typer.Option(False, "--json", help="Output JSON."),
157
- ) -> None:
158
- """Search SMS messages. GET /v1/sms/search."""
159
- state: AppState = ctx.obj or AppState()
160
- if not state.has_any_auth():
161
- auth_required_error(json_output=json_output or state.should_json())
162
-
163
- client = CommuneClient.from_state(state)
164
- try:
165
- r = client.get("/v1/sms/search", params={
166
- "q": query,
167
- "phone_number_id": phone_number_id,
168
- "limit": limit,
169
- })
170
- except Exception as exc:
171
- network_error(exc, json_output=json_output or state.should_json())
172
-
173
- if not r.is_success:
174
- api_error(r, json_output=json_output or state.should_json())
175
-
176
- print_list(
177
- r.json(),
178
- json_output=json_output or state.should_json(),
179
- title=f"SMS Search: {query}",
180
- columns=[
181
- ("ID", "id"),
182
- ("From", "from"),
183
- ("To", "to"),
184
- ("Body", "body"),
185
- ("Date", "createdAt"),
186
- ],
187
- )
File without changes
File without changes