Skip to content

Commit

Permalink
feat: 添加查询余额的交互能力 (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
eryajf committed Mar 5, 2023
1 parent 510d9c5 commit 65d74be
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
19 changes: 11 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,19 @@ func ProcessRequest(rmsg public.ReceiveMsg) error {
logger.Warning(fmt.Errorf("send message error: %v", err))
}
case "余额":
rst, err := public.GetBalance()
if err != nil {
logger.Warning(fmt.Errorf("get balance error: %v", err))
return err
cacheMsg := public.UserService.GetUserMode("system_balance")
if cacheMsg == "" {
rst, err := public.GetBalance()
if err != nil {
logger.Warning(fmt.Errorf("get balance error: %v", err))
return err
}
t1 := time.Unix(int64(rst.Grants.Data[0].EffectiveAt), 0)
t2 := time.Unix(int64(rst.Grants.Data[0].ExpiresAt), 0)
cacheMsg = fmt.Sprintf("💵 已用: 💲%v\n💵 剩余: 💲%v\n⏳ 有效时间: 从 %v 到 %v\n", fmt.Sprintf("%.2f", rst.TotalUsed), fmt.Sprintf("%.2f", rst.TotalAvailable), t1.Format("2006-01-02 15:04:05"), t2.Format("2006-01-02 15:04:05"))
}
t1 := time.Unix(int64(rst.Grants.Data[0].EffectiveAt), 0)
t2 := time.Unix(int64(rst.Grants.Data[0].ExpiresAt), 0)
msg := fmt.Sprintf("💵 已用: 💲%v\n💵 剩余: 💲%v\n⏳ 有效时间: 从 %v 到 %v\n", fmt.Sprintf("%.2f", rst.TotalUsed), fmt.Sprintf("%.2f", rst.TotalAvailable), t1.Format("2006-01-02 15:04:05"), t2.Format("2006-01-02 15:04:05"))

_, err = rmsg.ReplyText(msg, rmsg.SenderStaffId)
_, err := rmsg.ReplyText(cacheMsg, rmsg.SenderStaffId)
if err != nil {
logger.Warning(fmt.Errorf("send message error: %v", err))
}
Expand Down
10 changes: 9 additions & 1 deletion public/gpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
)

func InitAiCli() *resty.Client {
return resty.New().SetTimeout(30*time.Second).SetHeader("Authorization", fmt.Sprintf("Bearer %s", config.LoadConfig().ApiKey)).SetProxy(config.LoadConfig().HttpProxy)
if config.LoadConfig().HttpProxy != "" {
return resty.New().SetTimeout(30*time.Second).SetHeader("Authorization", fmt.Sprintf("Bearer %s", config.LoadConfig().ApiKey)).SetProxy(config.LoadConfig().HttpProxy).SetRetryCount(3).SetRetryWaitTime(5 * time.Second)
}
return resty.New().SetTimeout(30*time.Second).SetHeader("Authorization", fmt.Sprintf("Bearer %s", config.LoadConfig().ApiKey)).SetRetryCount(3).SetRetryWaitTime(5 * time.Second)
}

type Billing struct {
Expand Down Expand Up @@ -42,5 +45,10 @@ func GetBalance() (Billing, error) {
if err != nil {
return data, err
}
t1 := time.Unix(int64(data.Grants.Data[0].EffectiveAt), 0)
t2 := time.Unix(int64(data.Grants.Data[0].ExpiresAt), 0)
msg := fmt.Sprintf("💵 已用: 💲%v\n💵 剩余: 💲%v\n⏳ 有效时间: 从 %v 到 %v\n", fmt.Sprintf("%.2f", data.TotalUsed), fmt.Sprintf("%.2f", data.TotalAvailable), t1.Format("2006-01-02 15:04:05"), t2.Format("2006-01-02 15:04:05"))
// 放入缓存
UserService.SetUserMode("system_balance", msg)
return data, nil
}
1 change: 1 addition & 0 deletions public/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var UserService service.UserServiceInterface
func InitSvc() {
config.LoadConfig()
UserService = service.NewUserService()
_, _ = GetBalance()
}

func FirstCheck(rmsg ReceiveMsg) bool {
Expand Down

0 comments on commit 65d74be

Please sign in to comment.