Files
miti99bot/internal/modules/misc/misc.go
T
tiennm99 f3b9891a54 refactor: rename module to miti99bot, canonicalize AWS deploy path
Rename:
- Go module github.com/tiennm99/miti99bot-go → github.com/tiennm99/miti99bot
- CloudFormation stack miti99bot-aws-port → miti99bot
- Drop "port", "Cloud Run", "GCP", "cutover", "Phase NN" framing from
  active code and docs — project reads as canonical AWS-Lambda from now on.

AWS deploy guide + flow fix:
- New docs/deploy-aws-free-tier-guide.md — Ubuntu 24.04 ARM64 onboarding
  with project-local venv (pip awscli + sam-cli), SSM secrets via read -s,
  idempotent OIDC provider + role creation, $1 budget alarm.
- Drop sam build from the pipeline — provided.al2023 + makefile builder
  expects a Makefile in CodeUri (build/lambda/, the output dir), so the
  step always fails. sam deploy --template-file template.yaml now reads
  the raw template and zips build/lambda/ directly.
- Rollback section rewritten — use continue-update-rollback /
  cancel-update-stack / git-SHA redeploy. Drop the broken
  --use-previous-template recipe.
- DynamoDB free-tier row corrected (on-demand is 2.5M read / 1M write
  request units, not 25 RCU/WCU).

Updated:
- README.md fully rewritten (drops port/legacy framing, lists modules,
  points new users at the free-tier guide).
- aws/README.md retitled "AWS account setup", phase numbers stripped.
- Makefile / .github/workflows/deploy.yml — sam deploy flow.
- samconfig.toml — stack_name = "miti99bot".
- Go comments — Cloud Run → Lambda, Cloud Scheduler → EventBridge
  Scheduler, Cloud Logging → CloudWatch Logs.
- Struct field GCPProject → FirestoreProject (env GOOGLE_CLOUD_PROJECT
  unchanged).

Plus advisory reports under plans/reports/ from the code-reviewer +
researcher passes that informed the fixes.

Verified: go vet ./..., go build ./..., go test ./... all green.
2026-05-13 22:05:38 +07:00

99 lines
3.0 KiB
Go

// Package misc is a small stub module that proves the framework end-to-end:
// /ping (public, exercises KV write), /mstats (protected, exercises KV read),
// /fortytwo (private easter egg).
package misc
import (
"context"
"errors"
"fmt"
"time"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
"github.com/tiennm99/miti99bot/internal/log"
"github.com/tiennm99/miti99bot/internal/modules"
"github.com/tiennm99/miti99bot/internal/modules/util/chathelper"
"github.com/tiennm99/miti99bot/internal/storage"
)
// lastPingKey is the per-module KV key /ping writes and /mstats reads.
const lastPingKey = "last_ping"
// lastPing mirrors the JS bot's wire format: { at: <ms-since-epoch number> }.
// Stored as int64 ms-epoch (not time.Time → RFC3339) so a future cross-runtime
// KV export/import migration round-trips byte-for-byte.
type lastPing struct {
At int64 `json:"at"`
}
// New is the module Factory. Captures the per-module Deps via closure so each
// command handler has direct access to its KV store.
func New(deps modules.Deps) modules.Module {
return modules.Module{
Commands: []modules.Command{
pingCommand(deps),
mstatsCommand(deps),
fortytwoCommand(),
},
}
}
func pingCommand(deps modules.Deps) modules.Command {
return modules.Command{
Name: "ping",
Visibility: modules.VisibilityPublic,
Description: "Health check — replies pong and records last ping",
Handler: func(ctx context.Context, b *bot.Bot, update *models.Update) error {
if update.Message == nil {
return nil
}
// Best-effort write — if KV is unavailable, still reply.
payload := lastPing{At: chathelper.NowMillis()}
if err := deps.KV.PutJSON(ctx, lastPingKey, payload); err != nil {
log.Error("kv put failed", "module", "misc", "command", "ping", "key", lastPingKey, "err", err)
}
return chathelper.Reply(ctx, b, update.Message.Chat.ID, "pong")
},
}
}
func mstatsCommand(deps modules.Deps) modules.Command {
return modules.Command{
Name: "mstats",
Visibility: modules.VisibilityProtected,
Description: "Show the timestamp of the last /ping",
Handler: func(ctx context.Context, b *bot.Bot, update *models.Update) error {
if update.Message == nil {
return nil
}
var last lastPing
text := "last ping: never"
err := deps.KV.GetJSON(ctx, lastPingKey, &last)
switch {
case err == nil && last.At > 0:
text = fmt.Sprintf("last ping: %s",
time.UnixMilli(last.At).UTC().Format(time.RFC3339))
case err != nil && !errors.Is(err, storage.ErrNotFound):
return fmt.Errorf("misc /mstats: %w", err)
}
return chathelper.Reply(ctx, b, update.Message.Chat.ID, text)
},
}
}
func fortytwoCommand() modules.Command {
return modules.Command{
Name: "fortytwo",
Visibility: modules.VisibilityPrivate,
Description: "Easter egg — the answer",
Handler: func(ctx context.Context, b *bot.Bot, update *models.Update) error {
if update.Message == nil {
return nil
}
return chathelper.Reply(ctx, b, update.Message.Chat.ID, "The answer.")
},
}
}