vkteams-bot-cli 0.7.6

High-performance VK Teams Bot API toolkit with CLI and MCP server support
Documentation
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
# VK Teams Bot API Cli

[![crates.io](https://img.shields.io/crates/v/vkteams-bot-cli)](https://crates.io/crates/vkteams-bot-cli)
[![codecov](https://codecov.io/github/bug-ops/vkteams-bot/graph/badge.svg?token=XV23ZKSZRA&flag=vkteams-bot-cli)](https://codecov.io/github/bug-ops/vkteams-bot)
[![Unsafe forbidden](https://img.shields.io/badge/unsafe-forbidden-success.svg)](https://github.com/rust-secure-code/safety-dance/)

🤖 **Powerful command-line interface for VK Teams Bot API** with interactive setup, progress bars, and comprehensive chat management features.

## ✨ Features

- 📤 **Message Operations**: Send text, files, and voice messages
- 💬 **Chat Management**: Get info, manage members, set titles and descriptions
- 🔧 **Message Editing**: Edit, delete, pin/unpin messages
- 📁 **File Handling**: Upload and download files with progress bars
- 🎯 **Event Monitoring**: Real-time event listening with long polling
-**Message Scheduling**: Schedule messages with cron expressions, intervals, or specific times
- 🤖 **Task Automation**: Background scheduler daemon with task management
- ⚙️ **Smart Configuration**: Interactive setup wizard and multiple config sources
- 🔍 **Diagnostics**: Built-in validation and connection testing
- 🎨 **User-Friendly**: Colored output, progress indicators, and helpful examples
- 📊 **Structured Output**: JSON output support for all commands for easy integration

## Table of Contents

- [Installation]#installation
- [Shell Completion]#shell-completion
- [Quick Start]#quick-start
- [Configuration]#configuration
- [Output Formats]#output-formats
- [Commands]#commands
- [Examples]#examples
- [Advanced Usage]#advanced-usage

## Installation

### From crates.io (Recommended)

```bash
cargo install vkteams-bot-cli
```

### From source

```bash
git clone https://github.com/bug-ops/vkteams-bot
cd vkteams-bot
cargo install --path crates/vkteams-bot-cli
```

## Shell Completion

Shell completion provides intelligent Tab completion for all commands, options, and file paths.

### 🚀 One-Command Setup

```bash
# Build CLI
cargo build --release

# Install completions using the CLI
vkteams-bot-cli completion bash --install
vkteams-bot-cli completion zsh --install
```

### What You Get

After installation, use Tab completion for:

- **Commands**: `vkteams-bot-cli s[Tab]``send-text`, `send-file`, `schedule`
- **Options**: `vkteams-bot-cli config --[Tab]``--show`, `--wizard`, `--help`
- **File paths**: `vkteams-bot-cli send-file -p /path/[Tab]` → auto-complete files

### Runtime Generation

```bash
# Generate completion for your shell
vkteams-bot-cli completion bash --output completion.bash
vkteams-bot-cli completion zsh --install

# Generate to stdout
vkteams-bot-cli completion bash
```

### Manual Installation

#### Bash

```bash
# Generate and source the completion script
vkteams-bot-cli completion bash > ~/.local/share/bash-completion/completions/vkteams-bot-cli

# Add to your ~/.bashrc
echo 'source ~/.local/share/bash-completion/completions/vkteams-bot-cli' >> ~/.bashrc
```

#### Zsh

```bash
# Generate completion script
vkteams-bot-cli completion zsh > ~/.local/share/zsh/site-functions/_vkteams-bot-cli

# Ensure completions are enabled in ~/.zshrc
echo 'autoload -Uz compinit && compinit' >> ~/.zshrc
```

#### Fish

```bash
# Fish automatically loads completions from this directory
vkteams-bot-cli completion fish > ~/.config/fish/completions/vkteams-bot-cli.fish
```

#### PowerShell

```powershell
# Generate completion script
vkteams-bot-cli completion powershell > vkteams-bot-cli-completion.ps1

# Add to your PowerShell profile
Add-Content $PROFILE ". $(pwd)\vkteams-bot-cli-completion.ps1"
```

### Build from Source with Completions

```bash
# Clone and build
git clone https://github.com/bug-ops/vkteams-bot
cd vkteams-bot/crates/vkteams-bot-cli
cargo build --release

# Install completions using the CLI
./target/release/vkteams-bot-cli completion bash --install
./target/release/vkteams-bot-cli completion zsh --install

# Completions are now available in your shell! 🎉
```

> 📋 **Build Guide**: See [BUILD.md]BUILD.md for detailed build instructions, troubleshooting, and advanced options.

## Quick Start

| `completion` | Generate shell completions | `vkteams-bot-cli completion bash` |

### 1. Get Your Bot Credentials

Follow the [VK Teams Bot API instructions](https://teams.vk.com/botapi/?lang=en) to create a bot and get your:

- **API Token**
- **API URL**

### 2. Interactive Setup

```bash
vkteams-bot-cli setup
```

This will guide you through the initial configuration process.

### 3. Test Your Setup

```bash
vkteams-bot-cli validate
```

### 4. Send Your First Message

```bash
vkteams-bot-cli send-text -u USER_ID -m "Hello from CLI!"
```

### 5. Set Up Shell Completion (Optional)

```bash
# Install for your shell
vkteams-bot-cli completion bash --install
vkteams-bot-cli completion zsh --install
vkteams-bot-cli completion fish --install

# Or generate to file
vkteams-bot-cli completion bash > ~/.local/share/bash-completion/completions/vkteams-bot-cli
```

## Configuration

The CLI supports multiple configuration methods (in order of precedence):

### 1. Environment Variables

```bash
# Required
export VKTEAMS_BOT_API_TOKEN=your_token_here
export VKTEAMS_BOT_API_URL=your_api_url_here

# Optional
export VKTEAMS_PROXY=http://proxy:8080
export VKTEAMS_LOG_LEVEL=info
export VKTEAMS_DOWNLOAD_DIR=/path/to/downloads
```

### 2. Configuration File

The CLI automatically looks for config files in:

- Current directory: `cli_config.toml`
- User config: `~/.config/vkteams-bot/cli_config.toml`
- System config: `/etc/vkteams-bot/cli_config.toml` (Unix only)

### 3. Interactive Configuration

```bash
# Initial setup wizard
vkteams-bot-cli setup

# Update existing configuration
vkteams-bot-cli config --wizard

# View current configuration
vkteams-bot-cli config --show
```

## Output Formats

The CLI supports multiple output formats for all commands:

### Default (Pretty) Output
By default, commands display human-readable formatted output with colors and emoji:

```bash
vkteams-bot-cli send-text -u USER_ID -m "Hello!"
# ✅ Message sent successfully (msgId: 123456789)
```

### JSON Output
For programmatic usage and integration with other tools, use `--output json`:

```bash
vkteams-bot-cli --output json send-text -u USER_ID -m "Hello!"
```

Output:
```json
{
  "success": true,
  "data": {
    "msgId": "123456789",
    "ok": true
  },
  "error": null,
  "timestamp": "2024-01-20T10:30:45Z",
  "command": "send-text"
}
```

### Other Output Formats
- `--output table`: Tabular format (where applicable)
- `--output quiet`: Minimal output, only errors

### Using JSON Output in Scripts

```bash
# Extract message ID using jq
MSG_ID=$(vkteams-bot-cli --output json send-text -u USER_ID -m "Test" | jq -r '.data.msgId')

# Check command success
if vkteams-bot-cli --output json health-check | jq -e '.success' > /dev/null; then
  echo "Bot is healthy"
fi

# Get scheduler task count
TASK_COUNT=$(vkteams-bot-cli --output json scheduler list | jq '.data.total')
```

📚 **[See detailed JSON Output documentation](docs/JSON_OUTPUT.md)** for more examples and integration guides.

## Commands

📖 **[Complete Command Reference](docs/COMMANDS.md)** - Detailed documentation for all commands with options and examples.

### 📤 Message Operations

| Command | Description | Example |
|---------|-------------|---------|
| `send-text` | Send text message | `vkteams-bot-cli send-text -u USER_ID -m "Hello!"` |
| `send-file` | Send file with progress | `vkteams-bot-cli send-file -u USER_ID -p file.pdf` |
| `send-voice` | Send voice message | `vkteams-bot-cli send-voice -u USER_ID -p voice.ogg` |
| `edit-message` | Edit existing message | `vkteams-bot-cli edit-message -c CHAT_ID -m MSG_ID -t "New text"` |
| `delete-message` | Delete message | `vkteams-bot-cli delete-message -c CHAT_ID -m MSG_ID` |
| `pin-message` | Pin message | `vkteams-bot-cli pin-message -c CHAT_ID -m MSG_ID` |
| `unpin-message` | Unpin message | `vkteams-bot-cli unpin-message -c CHAT_ID -m MSG_ID` |

### 💬 Chat Management

| Command | Description | Example |
|---------|-------------|---------|
| `get-chat-info` | Get chat details | `vkteams-bot-cli get-chat-info -c CHAT_ID` |
| `get-chat-members` | List chat members | `vkteams-bot-cli get-chat-members -c CHAT_ID` |
| `set-chat-title` | Change chat title | `vkteams-bot-cli set-chat-title -c CHAT_ID -t "New Title"` |
| `set-chat-about` | Set chat description | `vkteams-bot-cli set-chat-about -c CHAT_ID -a "Description"` |
| `send-action` | Send typing/looking action | `vkteams-bot-cli send-action -c CHAT_ID -a typing` |

### 📁 File Operations

| Command | Description | Example |
|---------|-------------|---------|
| `get-file` | Download file with progress | `vkteams-bot-cli get-file -f FILE_ID -p /downloads/` |

### 📡 Event Monitoring

| Command | Description | Example |
|---------|-------------|---------|
| `get-events` | Get events once | `vkteams-bot-cli get-events` |
| `get-events -l true` | Start long polling | `vkteams-bot-cli get-events -l true` |

### 🔍 Information & Diagnostics

| Command | Description | Example |
|---------|-------------|---------|
| `get-self` | Get bot information | `vkteams-bot-cli get-self` |
| `get-profile` | Get user profile | `vkteams-bot-cli get-profile -u USER_ID` |
| `validate` | Test configuration | `vkteams-bot-cli validate` |
| `examples` | Show usage examples | `vkteams-bot-cli examples` |
| `list-commands` | Show all commands | `vkteams-bot-cli list-commands` |

### ⏰ Message Scheduling

| Command | Description | Example |
|---------|-------------|---------|
| `schedule text` | Schedule text message | `vkteams-bot-cli schedule text -u CHAT_ID -m "Hello" -t "2024-01-01 10:00"` |
| `schedule file` | Schedule file message | `vkteams-bot-cli schedule file -u CHAT_ID -p file.pdf -c "0 9 * * *"` |
| `schedule voice` | Schedule voice message | `vkteams-bot-cli schedule voice -u CHAT_ID -p voice.ogg -i 3600` |
| `schedule action` | Schedule chat action | `vkteams-bot-cli schedule action -u CHAT_ID -a typing -t "30m"` |

### 🔧 Scheduler Management

| Command | Description | Example |
|---------|-------------|---------|
| `scheduler start` | Start scheduler daemon | `vkteams-bot-cli scheduler start` |
| `scheduler stop` | Stop scheduler daemon | `vkteams-bot-cli scheduler stop` |
| `scheduler status` | Show scheduler status | `vkteams-bot-cli scheduler status` |
| `scheduler list` | List all scheduled tasks | `vkteams-bot-cli scheduler list` |

### 📋 Task Management

| Command | Description | Example |
|---------|-------------|---------|
| `task show` | Show task details | `vkteams-bot-cli task show TASK_ID` |
| `task remove` | Remove scheduled task | `vkteams-bot-cli task remove TASK_ID` |
| `task enable` | Enable disabled task | `vkteams-bot-cli task enable TASK_ID` |
| `task disable` | Disable active task | `vkteams-bot-cli task disable TASK_ID` |
| `task run` | Run task immediately | `vkteams-bot-cli task run TASK_ID` |

### ⚙️ Configuration

| Command | Description | Example |
|---------|-------------|---------|
| `setup` | Interactive setup wizard | `vkteams-bot-cli setup` |
| `config --show` | Show current config | `vkteams-bot-cli config --show` |
| `config --wizard` | Update config interactively | `vkteams-bot-cli config --wizard` |
| `completion` | Generate shell completions | `vkteams-bot-cli completion bash` |

## Examples

### Basic Messaging

```bash
# Send a simple text message
vkteams-bot-cli send-text -u user123 -m "Hello, World!"

# Send a file with automatic progress bar
vkteams-bot-cli send-file -u user123 -p /path/to/document.pdf

# Send a voice message
vkteams-bot-cli send-voice -u user123 -p /path/to/audio.ogg
```

### Chat Management

```bash
# Get detailed chat information
vkteams-bot-cli get-chat-info -c chat456

# List all members in a chat
vkteams-bot-cli get-chat-members -c chat456

# Update chat settings
vkteams-bot-cli set-chat-title -c chat456 -t "New Project Chat"
vkteams-bot-cli set-chat-about -c chat456 -a "Discussion for the new project"
```

### Message Operations

```bash
# Edit a message
vkteams-bot-cli edit-message -c chat456 -m msg789 -t "Updated message content"

# Pin an important message
vkteams-bot-cli pin-message -c chat456 -m msg789

# Delete a message
vkteams-bot-cli delete-message -c chat456 -m msg789
```

### File Operations

```bash
# Download a file to specific directory
vkteams-bot-cli get-file -f file123 -p /downloads/

# The file will be saved with its original name in the specified directory
```

### Event Monitoring

```bash
# Get events once
vkteams-bot-cli get-events

# Start continuous event monitoring
vkteams-bot-cli get-events -l true

# Monitor events and filter for specific types
vkteams-bot-cli get-events -l true | jq '.events[] | select(.type == "newMessage")'

# Monitor events and search for keywords
vkteams-bot-cli get-events -l true | grep -i "urgent"
```

### Chat Interaction

```bash
# Send typing indicator
vkteams-bot-cli send-action -c chat456 -a typing

# Send looking indicator
vkteams-bot-cli send-action -c chat456 -a looking
```

### Message Scheduling

```bash
# Schedule a text message for a specific date and time
vkteams-bot-cli schedule text -u chat456 -m "Meeting reminder" -t "2024-01-15 09:00:00"

# Schedule a daily message using cron expression (9 AM every day)
vkteams-bot-cli schedule text -u chat456 -m "Daily standup time!" -c "0 9 * * *"

# Schedule a file to be sent every hour
vkteams-bot-cli schedule file -u chat456 -p "/path/to/report.pdf" -i 3600

# Schedule a voice message for tomorrow
vkteams-bot-cli schedule voice -u chat456 -p "/path/to/announcement.ogg" -t "tomorrow"

# Schedule a typing action in 30 minutes
vkteams-bot-cli schedule action -u chat456 -a typing -t "30m"

# Schedule a weekly reminder (every Monday at 10 AM)
vkteams-bot-cli schedule text -u chat456 -m "Weekly team meeting in 1 hour" -c "0 10 * * 1"

# Schedule a one-time message with limited runs
vkteams-bot-cli schedule text -u chat456 -m "Limited offer!" -t "2h" --max-runs 1
```

### Scheduler Management

```bash
# Start the scheduler daemon (runs in background)
vkteams-bot-cli scheduler start

# Check scheduler status and active tasks
vkteams-bot-cli scheduler status

# List all scheduled tasks with details
vkteams-bot-cli scheduler list

# Stop the scheduler daemon
vkteams-bot-cli scheduler stop
```

### Task Management

```bash
# Show detailed information about a specific task
vkteams-bot-cli task show a1b2c3d4-e5f6-7890-abcd-ef1234567890

# Run a scheduled task immediately (one-time execution)
vkteams-bot-cli task run a1b2c3d4-e5f6-7890-abcd-ef1234567890

# Temporarily disable a task
vkteams-bot-cli task disable a1b2c3d4-e5f6-7890-abcd-ef1234567890

# Re-enable a disabled task
vkteams-bot-cli task enable a1b2c3d4-e5f6-7890-abcd-ef1234567890

# Permanently remove a task
vkteams-bot-cli task remove a1b2c3d4-e5f6-7890-abcd-ef1234567890
```

### Advanced Scheduling Examples

```bash
# Business hours notifications (Monday to Friday, 9 AM to 5 PM)
vkteams-bot-cli schedule text -u team-chat -m "Working hours reminder" -c "0 9-17 * * 1-5"

# End of month reports (last day of every month at 6 PM)
vkteams-bot-cli schedule file -u reports-chat -p "/reports/monthly.pdf" -c "0 18 L * *"

# Hourly health checks during business hours
vkteams-bot-cli schedule text -u monitoring-chat -m "System health check" -c "0 9-17 * * 1-5"

# Multiple interval scheduling
vkteams-bot-cli schedule text -u alerts-chat -m "Every 5 minutes alert" -i 300
vkteams-bot-cli schedule text -u daily-chat -m "Every 30 minutes update" -i 1800

# Relative time scheduling
vkteams-bot-cli schedule text -u chat456 -m "In 1 hour" -t "1h"
vkteams-bot-cli schedule text -u chat456 -m "Tomorrow morning" -t "tomorrow"
vkteams-bot-cli schedule text -u chat456 -m "Next week" -t "7d"
```

### JSON Output Examples

```bash
# Get chat info in JSON format
vkteams-bot-cli --output json get-chat-info -c chat456

# Parse response and extract specific fields
vkteams-bot-cli --output json get-chat-info -c chat456 | jq '.data.title'

# Check if bot is healthy and get details
vkteams-bot-cli --output json health-check | jq '{healthy: .success, bot_id: .data.userId}'

# Get all scheduled tasks and filter active ones
vkteams-bot-cli --output json scheduler list | jq '.data.tasks[] | select(.enabled == true)'

# Create a monitoring script
#!/bin/bash
HEALTH=$(vkteams-bot-cli --output json health-check)
if [ $(echo $HEALTH | jq -r '.success') == "true" ]; then
  BOT_ID=$(echo $HEALTH | jq -r '.data.userId')
  echo "Bot $BOT_ID is healthy"
else
  ERROR=$(echo $HEALTH | jq -r '.error')
  echo "Bot check failed: $ERROR"
  exit 1
fi

# Batch operations with JSON output
for user in user1 user2 user3; do
  RESULT=$(vkteams-bot-cli --output json send-text -u $user -m "Batch message")
  MSG_ID=$(echo $RESULT | jq -r '.data.msgId')
  echo "Sent message $MSG_ID to $user"
done

# Store command results in files
vkteams-bot-cli --output json get-chat-members -c chat456 > members.json
vkteams-bot-cli --output json scheduler list > tasks.json

# Process multiple chats
CHATS=$(vkteams-bot-cli --output json list-chats | jq -r '.data[].chatId')
for chat in $CHATS; do
  vkteams-bot-cli --output json get-chat-info -c $chat | jq '.data.membersCount'
done
```

## Advanced Usage

### Configuration Management

```bash
# Save configuration to custom location
vkteams-bot-cli --save-config /path/to/custom-config.toml

# Use custom configuration file
vkteams-bot-cli --config /path/to/custom-config.toml send-text -u user123 -m "Hello"

# Initialize configuration with defaults
vkteams-bot-cli config --init

# Generate shell completions for better UX
vkteams-bot-cli completion bash --install
```

### Scripting and Automation

```bash
#!/bin/bash
# Example script for daily notifications

CHAT_ID="your_chat_id"
MESSAGE="Daily reminder: Check your tasks!"

# Send notification
vkteams-bot-cli send-text -u "$CHAT_ID" -m "$MESSAGE"

# Send typing action first for more natural interaction
vkteams-bot-cli send-action -c "$CHAT_ID" -a typing
sleep 2
vkteams-bot-cli send-text -u "$CHAT_ID" -m "$MESSAGE"
```

### Scheduler Automation Scripts

```bash
#!/bin/bash
# Setup automated notifications for a development team

TEAM_CHAT="dev-team-chat"
ALERTS_CHAT="alerts-chat"

# Daily standup reminder
vkteams-bot-cli schedule text \
  -u "$TEAM_CHAT" \
  -m "🏃‍♂️ Daily standup in 15 minutes! Please prepare your updates." \
  -c "0 9 * * 1-5"

# End of sprint reminders
vkteams-bot-cli schedule text \
  -u "$TEAM_CHAT" \
  -m "📊 Sprint ends tomorrow. Please update your task status." \
  -c "0 17 * * 4"

# Weekly retrospective
vkteams-bot-cli schedule text \
  -u "$TEAM_CHAT" \
  -m "🔄 Weekly retrospective today at 4 PM. What went well this week?" \
  -c "0 16 * * 5"

# System maintenance notifications
vkteams-bot-cli schedule text \
  -u "$ALERTS_CHAT" \
  -m "🔧 Scheduled maintenance window starts in 1 hour" \
  -t "2024-01-20 01:00:00"

echo "All scheduled notifications have been set up!"
```

```bash
#!/bin/bash
# Scheduler management script

case "$1" in
  start)
    echo "Starting VK Teams Bot scheduler..."
    vkteams-bot-cli scheduler start
    ;;
  stop)
    echo "Stopping VK Teams Bot scheduler..."
    vkteams-bot-cli scheduler stop
    ;;
  status)
    echo "Checking scheduler status..."
    vkteams-bot-cli scheduler status
    ;;
  list)
    echo "Listing all scheduled tasks..."
    vkteams-bot-cli scheduler list
    ;;
  *)
    echo "Usage: $0 {start|stop|status|list}"
    exit 1
    ;;
esac
```

### Environment-Specific Configuration

```bash
# Environment
export VKTEAMS_BOT_API_URL="https://api.example.com"
export VKTEAMS_LOG_LEVEL="info"

# Validate current setup
vkteams-bot-cli validate

# Set up completion for development environment
vkteams-bot-cli completion bash --install
```

### Troubleshooting

```bash
# Test your configuration
vkteams-bot-cli validate

# Get detailed bot information
vkteams-bot-cli get-self --detailed

# Check configuration
vkteams-bot-cli config --show

# View all available commands with descriptions
vkteams-bot-cli list-commands

# See usage examples
vkteams-bot-cli examples

# Install shell completion for better command discovery
vkteams-bot-cli completion bash --install
vkteams-bot-cli completion zsh --install
```

## Error Handling

The CLI provides clear error messages and appropriate exit codes:

- **Exit Code 0**: Success
- **Exit Code 64**: Usage error (invalid arguments)
- **Exit Code 69**: Service unavailable (API errors)
- **Exit Code 74**: I/O error (file operations)
- **Exit Code 70**: Software error (unexpected errors)

## Configuration File Format

Example `cli_config.toml`:

```toml
[api]
token = "your_bot_token"
url = "https://example.com"
timeout = 30
max_retries = 3

[files]
download_dir = "/home/user/downloads"
upload_dir = "/home/user/uploads"
max_file_size = 104857600  # 100MB
buffer_size = 65536        # 64KB

[logging]
level = "info"
format = "text"
colors = true

[ui]
show_progress = true
progress_style = "unicode"
progress_refresh_rate = 100

[proxy]
url = "http://proxy:8080"
# user = "username"     # optional
# password = "password" # optional

[rate_limit]
enabled = false        # Disable rate limiting for CLI usage
limit = 1000          # Maximum requests per time window
duration = 60         # Time window in seconds
retry_delay = 500     # Delay between retries in milliseconds
retry_attempts = 3    # Maximum retry attempts

[scheduler]
data_file = "/home/user/.config/vkteams-bot/scheduler_tasks.json"
check_interval = 60    # seconds
max_task_history = 100
```

## Scheduler Features

### Schedule Types

The CLI supports three types of scheduling:

1. **One-time scheduling** - Execute once at a specific time
2. **Cron-based scheduling** - Use cron expressions for complex recurring schedules
3. **Interval-based scheduling** - Repeat every N seconds/minutes/hours

### Time Formats

**Absolute time:**

- `2024-01-15 14:30:00` - Specific date and time
- `2024-01-15 14:30` - Date and time (seconds default to 00)
- `2024-01-15` - Date only (time defaults to 00:00:00)

**Relative time:**

- `30s` - 30 seconds from now
- `5m` - 5 minutes from now
- `2h` - 2 hours from now
- `1d` - 1 day from now
- `1w` - 1 week from now
- `tomorrow` - Tomorrow at the same time
- `now` - Right now

**Cron expressions:**

- `0 9 * * *` - Every day at 9:00 AM
- `0 */2 * * *` - Every 2 hours
- `0 9 * * 1-5` - Weekdays at 9:00 AM
- `0 0 1 * *` - First day of every month at midnight
- `*/30 * * * *` - Every 30 minutes

### Task Management

Each scheduled task has:

- **Unique ID** - UUID for task identification
- **Status** - Active/disabled state
- **Run count** - Number of times executed
- **Next run time** - When the task will run next
- **Maximum runs** - Optional limit on executions

### Scheduler Daemon

The scheduler runs as a background process that:

- Checks for due tasks every minute
- Executes tasks at their scheduled time
- Manages task state and run counts
- Automatically disables completed one-time tasks
- Persists task data between restarts