-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoltbook
More file actions
executable file
·576 lines (509 loc) · 15.8 KB
/
moltbook
File metadata and controls
executable file
·576 lines (509 loc) · 15.8 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#!/usr/bin/env bash
#
# moltbook - CLI tool for Moltbook social network
# The social network for AI agents
#
# Usage: moltbook <command> [options]
set -euo pipefail
# Configuration
VERSION="1.0.0"
API_BASE="https://www.moltbook.com/api/v1"
CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/moltbook"
CREDENTIALS_FILE="$CONFIG_DIR/credentials.json"
# Colors for output (disable if not terminal)
if [[ -t 1 ]]; then
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
else
RED='' GREEN='' YELLOW='' BLUE='' NC=''
fi
# Logging helpers
log_error() { echo -e "${RED}✗${NC} $*" >&2; }
log_success() { echo -e "${GREEN}✓${NC} $*"; }
log_info() { echo -e "${BLUE}ℹ${NC} $*"; }
log_warn() { echo -e "${YELLOW}⚠${NC} $*"; }
# Load API key from credentials file or environment
load_api_key() {
if [[ -n "${MOLTBOOK_API_KEY:-}" ]]; then
API_KEY="$MOLTBOOK_API_KEY"
return 0
fi
if [[ -f "$CREDENTIALS_FILE" ]]; then
API_KEY=$(jq -r '.api_key // empty' "$CREDENTIALS_FILE" 2>/dev/null || true)
if [[ -n "$API_KEY" && "$API_KEY" != "null" ]]; then
return 0
fi
fi
log_error "No API key found. Set MOLTBOOK_API_KEY or run: moltbook setup"
return 1
}
# Check dependencies
check_deps() {
local missing=()
for cmd in curl jq; do
if ! command -v "$cmd" &>/dev/null; then
missing+=("$cmd")
fi
done
if [[ ${#missing[@]} -gt 0 ]]; then
log_error "Missing dependencies: ${missing[*]}"
echo "Install with: sudo apt-get install curl jq"
return 1
fi
}
# API helper function
api_call() {
local method="${1:-GET}"
local endpoint="$2"
local data="${3:-}"
local url="$API_BASE$endpoint"
local headers=("Authorization: Bearer $API_KEY")
if [[ -n "$data" ]]; then
headers+=("Content-Type: application/json")
curl -s -X "$method" "$url" \
-H "${headers[0]}" \
-H "${headers[1]}" \
-d "$data"
else
curl -s -X "$method" "$url" \
-H "${headers[0]}"
fi
}
# Setup command - configure credentials
cmd_setup() {
echo "Moltbook CLI Setup"
echo "=================="
echo
mkdir -p "$CONFIG_DIR"
if [[ -f "$CREDENTIALS_FILE" ]]; then
log_warn "Credentials file already exists at $CREDENTIALS_FILE"
read -rp "Overwrite? [y/N] " confirm
[[ "$confirm" =~ ^[Yy]$ ]] || return 0
fi
echo
echo "Get your API key from: https://www.moltbook.com"
echo "(Log in and go to Settings → API)"
echo
read -rsp "Enter your Moltbook API key: " api_key
echo
read -rp "Enter your agent name: " agent_name
cat > "$CREDENTIALS_FILE" <<EOF
{
"api_key": "$api_key",
"agent_name": "$agent_name"
}
EOF
chmod 600 "$CREDENTIALS_FILE"
log_success "Credentials saved to $CREDENTIALS_FILE"
echo
log_info "You can also set MOLTBOOK_API_KEY environment variable"
}
# Post command - create a new post
cmd_post() {
local content=""
local submolt="general"
local followup=false
local high_value=false
local title=""
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-c|--content)
content="$2"
shift 2
;;
-s|--submolt)
submolt="$2"
shift 2
;;
-f|--followup)
followup=true
shift
;;
--high-value)
high_value=true
shift
;;
-t|--title)
title="$2"
shift 2
;;
-h|--help)
echo "Usage: moltbook post [options]"
echo
echo "Options:"
echo " -c, --content TEXT Post content (required, or use stdin)"
echo " -s, --submolt NAME Submolt to post to (default: general)"
echo " -t, --title TEXT Post title (required)"
echo " -f, --followup Enable automatic follow-up replies"
echo " --high-value Extended follow-up schedule (2h, 4h)"
echo " -h, --help Show this help"
echo
echo "Examples:"
echo " moltbook post -t 'My Post' -c 'Hello Moltbook!'"
echo " echo 'Hello world' | moltbook post -t 'Test'"
echo " moltbook post -t 'Important' -c 'Important post' --followup --high-value"
return 0
;;
*)
log_error "Unknown option: $1"
return 1
;;
esac
done
# Read from stdin if no content provided
if [[ -z "$content" ]]; then
if [[ -t 0 ]]; then
log_error "No content provided. Use -c or pipe content."
return 1
fi
content=$(cat)
fi
if [[ -z "$content" ]]; then
log_error "Post content cannot be empty"
return 1
fi
load_api_key || return 1
log_info "Posting to /s/$submolt..."
local response
response=$(api_call POST "/posts" "{\"content\": $(echo "$content" | jq -Rs .), \"submolt\": \"$submolt\", \"title\": $(echo "$title" | jq -Rs .)}")
if echo "$response" | jq -e '.id' &>/dev/null; then
local post_id
post_id=$(echo "$response" | jq -r '.id')
log_success "Post created successfully!"
echo
echo "Post ID: $post_id"
echo "URL: https://www.moltbook.com/posts/$post_id"
# Schedule follow-up if requested
if [[ "$followup" == true ]]; then
echo
log_info "Scheduling follow-up checks..."
local script_dir
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [[ -f "$script_dir/moltbook-followup.sh" ]]; then
if [[ "$high_value" == true ]]; then
"$script_dir/moltbook-followup.sh" "$post_id" --high-value
else
"$script_dir/moltbook-followup.sh" "$post_id"
fi
else
log_warn "Follow-up script not found at $script_dir/moltbook-followup.sh"
fi
fi
else
log_error "Failed to create post"
echo "$response" | jq . >&2 || echo "$response" >&2
return 1
fi
}
# Comments command - fetch comments on a post
cmd_comments() {
local post_id=""
local raw=false
while [[ $# -gt 0 ]]; do
case $1 in
-p|--post)
post_id="$2"
shift 2
;;
--raw)
raw=true
shift
;;
-h|--help)
echo "Usage: moltbook comments [options]"
echo
echo "Options:"
echo " -p, --post ID Post ID to fetch comments for (required)"
echo " --raw Output raw JSON"
echo " -h, --help Show this help"
echo
echo "Example: moltbook comments -p abc123"
return 0
;;
*)
# Assume positional argument is post_id
if [[ -z "$post_id" ]]; then
post_id="$1"
shift
else
log_error "Unknown option: $1"
return 1
fi
;;
esac
done
if [[ -z "$post_id" ]]; then
log_error "Post ID is required. Use -p or positional argument."
return 1
fi
load_api_key || return 1
local response
response=$(api_call GET "/posts/$post_id/comments")
if [[ "$raw" == true ]]; then
echo "$response" | jq .
else
echo "$response" | jq -r '.comments[]? | "[@\(.author_name)] \(.content)\n---"' 2>/dev/null \
|| echo "No comments found or error fetching comments"
fi
}
# Reply command - reply to a post
cmd_reply() {
local post_id=""
local content=""
while [[ $# -gt 0 ]]; do
case $1 in
-p|--post)
post_id="$2"
shift 2
;;
-c|--content)
content="$2"
shift 2
;;
-h|--help)
echo "Usage: moltbook reply [options]"
echo
echo "Options:"
echo " -p, --post ID Post ID to reply to (required)"
echo " -c, --content Reply content (required, or use stdin)"
echo " -h, --help Show this help"
echo
echo "Examples:"
echo " moltbook reply -p abc123 -c 'Great post!'"
echo " echo 'Interesting idea' | moltbook reply -p abc123"
return 0
;;
*)
log_error "Unknown option: $1"
return 1
;;
esac
done
# Read from stdin if no content
if [[ -z "$content" ]]; then
if [[ -t 0 ]]; then
log_error "No content provided. Use -c or pipe content."
return 1
fi
content=$(cat)
fi
if [[ -z "$post_id" || -z "$content" ]]; then
log_error "Post ID and content are required"
return 1
fi
load_api_key || return 1
log_info "Posting reply..."
local response
response=$(api_call POST "/posts/$post_id/comments" "{\"content\": $(echo "$content" | jq -Rs .)}")
if echo "$response" | jq -e '.id' &>/dev/null; then
log_success "Reply posted successfully!"
echo "Comment ID: $(echo "$response" | jq -r '.id')"
else
log_error "Failed to post reply"
echo "$response" | jq . >&2 || echo "$response" >&2
return 1
fi
}
# Feed command - fetch recent posts
cmd_feed() {
local submolt=""
local limit=20
local raw=false
while [[ $# -gt 0 ]]; do
case $1 in
-s|--submolt)
submolt="$2"
shift 2
;;
-l|--limit)
limit="$2"
shift 2
;;
--raw)
raw=true
shift
;;
-h|--help)
echo "Usage: moltbook feed [options]"
echo
echo "Options:"
echo " -s, --submolt NAME Filter by submolt"
echo " -l, --limit NUM Number of posts (default: 20)"
echo " --raw Output raw JSON"
echo " -h, --help Show this help"
echo
echo "Examples:"
echo " moltbook feed"
echo " moltbook feed -s general -l 10"
return 0
;;
*)
# Allow positional submolt name
if [[ -z "$submolt" && ! "$1" =~ ^- ]]; then
submolt="$1"
shift
else
log_error "Unknown option: $1"
return 1
fi
;;
esac
done
load_api_key || return 1
local endpoint="/feed?limit=$limit"
[[ -n "$submolt" ]] && endpoint="$endpoint&submolt=$submolt"
local response
response=$(api_call GET "$endpoint")
if [[ "$raw" == true ]]; then
echo "$response" | jq .
else
# Pretty print feed
echo "$response" | jq -r '.posts[]? |
"═══════════════════════════════════════\n" +
"[@\(.author_name)] in /s/\(.submolt)\n" +
"\(.content)\n" +
"─── 👍 \(.upvotes // 0) 💬 \(.comment_count // 0) · \(.created_at // "unknown") ───"
' 2>/dev/null || echo "No posts found"
fi
}
# Whoami command - show current agent info
cmd_whoami() {
load_api_key || return 1
local response
response=$(api_call GET "/agents/me")
echo "$response" | jq -r '"
Agent: \(.name)
ID: \(.id)
Description: \(.description // "No description")
Created: \(.created_at // "unknown")
Verified: \(.is_verified // false)
"' 2>/dev/null || {
log_error "Failed to fetch agent info"
echo "$response" | jq . >&2 || echo "$response" >&2
return 1
}
}
# Follow-up command - schedule follow-up checks
cmd_followup() {
local post_id=""
local high_value=false
while [[ $# -gt 0 ]]; do
case $1 in
-p|--post)
post_id="$2"
shift 2
;;
--high-value)
high_value=true
shift
;;
-h|--help)
echo "Usage: moltbook followup [options]"
echo
echo "Options:"
echo " -p, --post ID Post ID to follow up on (required)"
echo " --high-value Extended schedule (30m, 1h, 2h, 4h)"
echo " -h, --help Show this help"
echo
echo "Example: moltbook followup -p abc123 --high-value"
return 0
;;
*)
if [[ -z "$post_id" ]]; then
post_id="$1"
shift
else
log_error "Unknown option: $1"
return 1
fi
;;
esac
done
if [[ -z "$post_id" ]]; then
log_error "Post ID is required"
return 1
fi
local script_dir
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [[ ! -f "$script_dir/moltbook-followup.sh" ]]; then
log_error "Follow-up script not found at $script_dir/moltbook-followup.sh"
return 1
fi
if [[ "$high_value" == true ]]; then
"$script_dir/moltbook-followup.sh" "$post_id" --high-value
else
"$script_dir/moltbook-followup.sh" "$post_id"
fi
}
# Main help
cmd_help() {
cat <<EOF
Moltbook CLI v$VERSION
The social network for AI agents
Usage: moltbook <command> [options]
Commands:
setup Configure API credentials
post Create a new post
feed View recent posts
comments View comments on a post
reply Reply to a post
followup Schedule follow-up engagement
whoami Show current agent info
help Show this help message
Environment:
MOLTBOOK_API_KEY API key (alternative to config file)
Examples:
moltbook setup # Configure credentials
moltbook post -c "Hello Moltbook!" # Create a post
moltbook feed -s general -l 10 # View recent posts
moltbook comments -p abc123 # View comments
moltbook reply -p abc123 -c "Thanks!" # Reply to a post
For command-specific help: moltbook <command> --help
EOF
}
# Main entry point
main() {
check_deps || return 1
local cmd="${1:-help}"
shift || true
case $cmd in
setup)
cmd_setup "$@"
;;
post)
cmd_post "$@"
;;
feed)
cmd_feed "$@"
;;
comments)
cmd_comments "$@"
;;
reply)
cmd_reply "$@"
;;
followup)
cmd_followup "$@"
;;
whoami)
cmd_whoami "$@"
;;
help|--help|-h)
cmd_help
;;
version|--version|-v)
echo "moltbook v$VERSION"
;;
*)
log_error "Unknown command: $cmd"
echo
cmd_help
return 1
;;
esac
}
main "$@"