summaryrefslogtreecommitdiff
path: root/telegram/send_after.go
blob: 8ae72c2806b7c1e9f44c0f880150577f10e8b9fc (plain)
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
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()
}