Skip to content

Commit

Permalink
fix: improve error messages and update reply handling in bot commands
Browse files Browse the repository at this point in the history
chore: upgrade deps
  • Loading branch information
krau committed Dec 2, 2024
1 parent f360052 commit a166ff1
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 34 deletions.
2 changes: 1 addition & 1 deletion bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func Init() {

select {
case <-ctx.Done():
logger.L.Fatal("Failed to initialize client")
logger.L.Fatal("Failed to initialize client: timeout")
os.Exit(1)
case result := <-resultChan:
if result.err != nil {
Expand Down
34 changes: 17 additions & 17 deletions bot/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const noPermissionText string = `
func checkPermission(ctx *ext.Context, update *ext.Update) error {
userID := update.GetUserChat().GetID()
if !slice.Contain(config.Cfg.Telegram.Admins, userID) {
ctx.Reply(update, noPermissionText, nil)
ctx.Reply(update, ext.ReplyTextString(noPermissionText), nil)
return dispatcher.EndGroups
}
return dispatcher.ContinueGroups
Expand All @@ -68,7 +68,7 @@ SaveAny Bot - 转存你的 Telegram 文件
`

func help(ctx *ext.Context, update *ext.Update) error {
ctx.Reply(update, helpText, nil)
ctx.Reply(update, ext.ReplyTextString(helpText), nil)
return dispatcher.EndGroups
}

Expand All @@ -83,18 +83,18 @@ func silent(ctx *ext.Context, update *ext.Update) error {
logger.L.Errorf("Failed to update user: %s", err)
return dispatcher.EndGroups
}
ctx.Reply(update, fmt.Sprintf("已%s静默模式", func() string {
ctx.Reply(update, ext.ReplyTextString(fmt.Sprintf("已%s静默模式", func() string {
if user.Silent {
return "开启"
}
return "关闭"
}()), nil)
}())), nil)
return dispatcher.EndGroups
}

func setDefaultStorage(ctx *ext.Context, update *ext.Update) error {
if len(storage.Storages) == 0 {
ctx.Reply(update, "未配置存储", nil)
ctx.Reply(update, ext.ReplyTextString("未配置存储"), nil)
return dispatcher.EndGroups
}
args := strings.Split(update.EffectiveMessage.Text, " ")
Expand All @@ -107,12 +107,12 @@ func setDefaultStorage(ctx *ext.Context, update *ext.Update) error {
text = append(text, styling.Plain("\n"))
text = append(text, styling.Code(name))
}
ctx.Reply(update, text, nil)
ctx.Reply(update, ext.ReplyTextStyledTextArray(text), nil)
return dispatcher.EndGroups
}
storageName := args[1]
if !slice.Contain(avaliableStorages, storageName) {
ctx.Reply(update, "存储位置不存在", nil)
ctx.Reply(update, ext.ReplyTextString("存储位置不存在"), nil)
return dispatcher.EndGroups
}
user, err := dao.GetUserByUserID(update.GetUserChat().GetID())
Expand All @@ -125,31 +125,31 @@ func setDefaultStorage(ctx *ext.Context, update *ext.Update) error {
logger.L.Errorf("Failed to update user: %s", err)
return dispatcher.EndGroups
}
ctx.Reply(update, fmt.Sprintf("已设置默认存储位置为 %s", storageName), nil)
ctx.Reply(update, ext.ReplyTextString(fmt.Sprintf("已设置默认存储位置为 %s", storageName)), nil)
return dispatcher.EndGroups
}

func saveCmd(ctx *ext.Context, update *ext.Update) error {
res, ok := update.EffectiveMessage.GetReplyTo()
if !ok || res == nil {
ctx.Reply(update, "请回复要保存的文件", nil)
ctx.Reply(update, ext.ReplyTextString("请回复要保存的文件"), nil)
return dispatcher.EndGroups
}
replyHeader, ok := res.(*tg.MessageReplyHeader)
if !ok {
ctx.Reply(update, "请回复要保存的文件", nil)
ctx.Reply(update, ext.ReplyTextString("请回复要保存的文件"), nil)
return dispatcher.EndGroups
}
replyToMsgID, ok := replyHeader.GetReplyToMsgID()
if !ok {
ctx.Reply(update, "请回复要保存的文件", nil)
ctx.Reply(update, ext.ReplyTextString("请回复要保存的文件"), nil)
return dispatcher.EndGroups
}
msg, err := GetTGMessage(ctx, Client, replyToMsgID)

supported, _ := supportedMediaFilter(msg)
if !supported {
ctx.Reply(update, "不支持的消息类型或消息中没有文件", nil)
ctx.Reply(update, ext.ReplyTextString("不支持的消息类型或消息中没有文件"), nil)
return dispatcher.EndGroups
}

Expand All @@ -159,7 +159,7 @@ func saveCmd(ctx *ext.Context, update *ext.Update) error {
return dispatcher.EndGroups
}

replied, err := ctx.Reply(update, "正在获取文件信息...", nil)
replied, err := ctx.Reply(update, ext.ReplyTextString("正在获取文件信息..."), nil)
if err != nil {
logger.L.Errorf("Failed to reply: %s", err)
return dispatcher.EndGroups
Expand Down Expand Up @@ -213,7 +213,7 @@ func saveCmd(ctx *ext.Context, update *ext.Update) error {
}

if user.DefaultStorage == "" {
ctx.Reply(update, "请先使用 /storage 设置默认存储位置", nil)
ctx.Reply(update, ext.ReplyTextString("请先使用 /storage 设置默认存储位置"), nil)
return dispatcher.EndGroups
}
queue.AddTask(types.Task{
Expand Down Expand Up @@ -251,7 +251,7 @@ func handleFileMessage(ctx *ext.Context, update *ext.Update) error {
return dispatcher.EndGroups
}

msg, err := ctx.Reply(update, "正在获取文件信息...", nil)
msg, err := ctx.Reply(update, ext.ReplyTextString("正在获取文件信息..."), nil)
if err != nil {
logger.L.Errorf("Failed to reply: %s", err)
return dispatcher.EndGroups
Expand All @@ -260,11 +260,11 @@ func handleFileMessage(ctx *ext.Context, update *ext.Update) error {
file, err := FileFromMedia(media)
if err != nil {
logger.L.Errorf("Failed to get file from media: %s", err)
ctx.Reply(update, "无法获取文件", nil)
ctx.Reply(update, ext.ReplyTextString("无法获取文件"), nil)
return dispatcher.EndGroups
}
if file.FileName == "" {
ctx.Reply(update, "无法获取文件名", nil)
ctx.Reply(update, ext.ReplyTextString("无法获取文件名"), nil)
return dispatcher.EndGroups
}

Expand Down
34 changes: 18 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@ go 1.23.2

require (
github.com/blang/semver v3.5.1+incompatible
github.com/celestix/gotgproto v1.0.0-beta18
github.com/celestix/gotgproto v1.0.0-beta18.0.20241129092818-923e7843fffc
github.com/gookit/slog v0.5.7
github.com/gotd/contrib v0.20.0
github.com/gotd/td v0.111.2
github.com/gotd/td v0.115.0
github.com/imroc/req/v3 v3.48.0
github.com/rhysd/go-github-selfupdate v1.2.3
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/studio-b12/gowebdav v0.9.0
golang.org/x/time v0.5.0
golang.org/x/time v0.8.0
)

require (
github.com/AnimeKaizoku/cacher v1.0.2 // indirect
github.com/caarlos0/env/v11 v11.2.2 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudflare/circl v1.5.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/glebarez/go-sqlite v1.22.0 // indirect
Expand All @@ -29,7 +30,7 @@ require (
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/google/go-github/v30 v30.1.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/pprof v0.0.0-20241101162523-b92577c0c142 // indirect
github.com/google/pprof v0.0.0-20241128161848-dc51965c6481 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gotd/ige v0.2.2 // indirect
github.com/gotd/neo v0.1.5 // indirect
Expand All @@ -40,30 +41,31 @@ require (
github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/onsi/ginkgo/v2 v2.21.0 // indirect
github.com/onsi/ginkgo/v2 v2.22.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/quic-go/qpack v0.5.1 // indirect
github.com/quic-go/quic-go v0.48.1 // indirect
github.com/quic-go/quic-go v0.48.2 // indirect
github.com/refraction-networking/utls v1.6.7 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/tcnksm/go-gitconfig v0.1.2 // indirect
github.com/ulikunitz/xz v0.5.12 // indirect
go.opentelemetry.io/otel v1.31.0 // indirect
go.opentelemetry.io/otel/trace v1.31.0 // indirect
go.opentelemetry.io/otel v1.32.0 // indirect
go.opentelemetry.io/otel/trace v1.32.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/mock v0.5.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
golang.org/x/tools v0.26.0 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/oauth2 v0.24.0 // indirect
golang.org/x/tools v0.27.0 // indirect
google.golang.org/protobuf v1.35.1 // indirect
modernc.org/libc v1.61.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
modernc.org/libc v1.61.3 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.8.0 // indirect
modernc.org/sqlite v1.33.1 // indirect
modernc.org/sqlite v1.34.1 // indirect
nhooyr.io/websocket v1.8.17 // indirect
rsc.io/qr v0.2.0 // indirect
)
Expand Down Expand Up @@ -93,7 +95,7 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
Expand Down
Loading

0 comments on commit a166ff1

Please sign in to comment.