Skip to content

Commit 6422489

Browse files
danizordclaude
andcommitted
feat: improve polling behavior with infinite loop by default
- `tg updates poll` without --timeout now loops indefinitely with 50s internal timeout until updates arrive - `tg updates poll --timeout N` performs a single poll (for hooks/scripts) - Updated skill documentation and hook scripts 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 7d87d8d commit 6422489

6 files changed

Lines changed: 46 additions & 18 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.2.0] - 2026-01-04
9+
10+
### Changed
11+
12+
- `tg updates poll` without `--timeout` now loops indefinitely with 50s internal timeout until updates arrive
13+
- `tg updates poll --timeout N` performs a single poll (useful for hooks and scripts)
14+
- Updated polling documentation and examples to reflect new behavior
15+
816
## [0.1.0] - 2026-01-04
917

1018
### Added
@@ -37,4 +45,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3745
- GitHub Actions CI workflow with type checking, linting, and tests
3846
- Modern tooling: tsgo, oxlint, oxfmt
3947

48+
[0.2.0]: https://github.com/danizord/telecli/releases/tag/v0.2.0
4049
[0.1.0]: https://github.com/danizord/telecli/releases/tag/v0.1.0

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "telecli",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "Thin CLI wrapper around the Telegram Bot API",
55
"type": "module",
66
"bin": {

plugin/commands/telegram-poll.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ Start or stop the Telegram polling loop.
2525
2. Confirm polling has started
2626

2727
3. Begin the polling loop:
28-
- Run `tg updates poll --timeout 30` (or with `--offset` if resuming)
28+
- Run `tg updates poll` (or with `--offset` if resuming)
29+
- The command will poll indefinitely with 50s timeout until updates arrive
2930
- Process any incoming messages (respond, react, etc.)
3031
- Update `last_offset` in state file with highest update_id + 1
31-
- Continue polling
32+
- Run the poll command again to wait for more updates
3233

3334
## Stopping Polling
3435

plugin/hooks/scripts/check-updates.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ if echo "$result" | grep -qF '"update_id"'; then
4040
{
4141
"decision": "block",
4242
"reason": "New Telegram messages received",
43-
"systemMessage": "New Telegram updates received. Process these messages:\n\n\`\`\`json\n$updates\n\`\`\`\n\nRespond to any messages that need a response, then continue polling with: tg updates poll --timeout 30 --offset $new_offset"
43+
"systemMessage": "New Telegram updates received. Process these messages:\n\n\`\`\`json\n$updates\n\`\`\`\n\nRespond to any messages that need a response, then continue polling with: tg updates poll --offset $new_offset"
4444
}
4545
EOF
4646
else

plugin/skills/telecli/SKILL.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,18 @@ tg config path # Show config file paths
3737
### Polling for Updates
3838

3939
```bash
40-
# Basic poll (returns immediately if no updates)
40+
# Wait indefinitely for updates (loops with 50s timeout until updates arrive)
4141
tg updates poll
4242

43-
# Long polling with timeout (waits up to N seconds)
44-
tg updates poll --timeout 30
43+
# With offset (skip already-processed updates)
44+
tg updates poll --offset 729538157
4545

46-
# Poll with offset (skip already-processed updates)
47-
tg updates poll --timeout 30 --offset 729538157
46+
# Single poll with explicit timeout (for scripts/hooks)
47+
tg updates poll --timeout 5
4848
```
4949

5050
**Polling loop pattern:**
51-
1. Call `tg updates poll --timeout 30`
51+
1. Call `tg updates poll` (blocks until updates arrive)
5252
2. Process returned messages
5353
3. Calculate next offset: `max(update_id) + 1`
5454
4. Repeat with `--offset <next_offset>`
@@ -178,7 +178,8 @@ tg message send "$chat_id" "You said: $text" --reply-to "$message_id"
178178
```bash
179179
offset=""
180180
while true; do
181-
result=$(tg updates poll --timeout 30 $offset)
181+
# Blocks until updates arrive (no --timeout = infinite polling)
182+
result=$(tg updates poll $offset)
182183

183184
# Process updates
184185
echo "$result" | jq -c '.result[]' | while read update; do

src/index.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -495,13 +495,30 @@ async function main() {
495495
const subcommand = positional[0];
496496
switch (subcommand) {
497497
case "poll": {
498-
output(
499-
await getUpdates({
500-
offset: flags.offset ? parseInt(flags.offset) : undefined,
501-
limit: flags.limit ? parseInt(flags.limit) : undefined,
502-
timeout: flags.timeout ? parseInt(flags.timeout) : undefined,
503-
}),
504-
);
498+
const offset = flags.offset ? parseInt(flags.offset) : undefined;
499+
const limit = flags.limit ? parseInt(flags.limit) : undefined;
500+
501+
// If --timeout is explicitly provided, do a single poll (for hooks)
502+
// If omitted, loop forever with 50s timeout until updates arrive
503+
if (flags.timeout !== undefined) {
504+
output(
505+
await getUpdates({
506+
offset,
507+
limit,
508+
timeout: parseInt(flags.timeout),
509+
}),
510+
);
511+
} else {
512+
// Loop forever until we get updates
513+
while (true) {
514+
const result = await getUpdates({ offset, limit, timeout: 50 });
515+
if (result.ok && result.result && result.result.length > 0) {
516+
output(result);
517+
break;
518+
}
519+
// If no updates, continue polling (don't output empty results)
520+
}
521+
}
505522
break;
506523
}
507524
case "webhook": {

0 commit comments

Comments
 (0)