a telegram example

This commit is contained in:
Jeff Carr 2025-08-16 18:59:08 -05:00
parent f5a8c9b672
commit 3e8e5fba8f
3 changed files with 43 additions and 0 deletions

1
telegram/.env Normal file
View File

@ -0,0 +1 @@
TOKEN=5292519055:AAGJPrxYs70PoIWEvLcS3oV3XZ2ldko-M4k

5
telegram/Makefile Normal file
View File

@ -0,0 +1,5 @@
run:
go run -v -x send_after.go
test:
curl https://api.telegram.org/bot5292519055:AAGJPrxYs70PoIWEvLcS3oV3XZ2ldko-M4k/getMe

37
telegram/send_after.go Normal file
View File

@ -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()
}