diff options
| author | Jeff Carr <[email protected]> | 2025-08-16 18:59:08 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-08-16 18:59:08 -0500 |
| commit | 3e8e5fba8fe3aacd07b45ccf9860dcd3fd8e9d94 (patch) | |
| tree | 49339c100ed6553e8790f8e6239c69f3d853ba35 /telegram/send_after.go | |
| parent | f5a8c9b67285fd9b788d84aade361f1190128066 (diff) | |
a telegram examplev0.0.68v0.0.67v0.0.66v0.0.65v0.0.64v0.0.63v0.0.62v0.0.61v0.0.60v0.0.59v0.0.58v0.0.57
Diffstat (limited to 'telegram/send_after.go')
| -rw-r--r-- | telegram/send_after.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/telegram/send_after.go b/telegram/send_after.go new file mode 100644 index 0000000..8ae72c2 --- /dev/null +++ b/telegram/send_after.go @@ -0,0 +1,37 @@ +package main + +import ( + "time" + "fmt" + + . "github.com/enetx/g" + "github.com/enetx/tg/bot" + "github.com/enetx/tg/ctx" +) + +func main() { + fmt.Println("started...") + // Read the bot token from the .env file + token := NewFile(".env").Read().Ok().Trim().Split("=").Collect().Last().Some() + b := bot.New(token).Build().Unwrap() + + // Register a command handler for /start + b.Command("start", func(ctx *ctx.Context) error { + // Send an immediate message so Telegram considers the update as "handled" + ctx.SendMessage("Preparing self-destruct...").Send() + + // Schedule a second message to be sent after 3 seconds, + // and automatically delete it 5 seconds after it is sent + ctx.SendMessage("This message will self-destruct in 5 seconds."). + After(3 * time.Second). // Delay sending by 3 seconds + DeleteAfter(5 * time.Second). // Delete 5 seconds after it is sent + Send() + + // Delete the original /start message (from the user) + // This should be done after responding to avoid Telegram resending the update + return ctx.DeleteMessage().Send().Err() + }) + + // Start polling for updates and drop any pending ones from before startup + b.Polling().DropPendingUpdates().Start() +} |
