tgbot - fix chatid

This commit is contained in:
mhsanaei 2024-04-02 15:04:44 +03:30
parent f02c82677d
commit eb382cd5b4
3 changed files with 45 additions and 38 deletions

View file

@ -91,7 +91,7 @@ type Client struct {
TotalGB int64 `json:"totalGB" form:"totalGB"` TotalGB int64 `json:"totalGB" form:"totalGB"`
ExpiryTime int64 `json:"expiryTime" form:"expiryTime"` ExpiryTime int64 `json:"expiryTime" form:"expiryTime"`
Enable bool `json:"enable" form:"enable"` Enable bool `json:"enable" form:"enable"`
TgID string `json:"tgId" form:"tgId"` TgID int64 `json:"tgId" form:"tgId"`
SubID string `json:"subId" form:"subId"` SubID string `json:"subId" form:"subId"`
Reset int `json:"reset" form:"reset"` Reset int `json:"reset" form:"reset"`
} }

View file

@ -1186,7 +1186,7 @@ func (s *InboundService) GetClientByEmail(clientEmail string) (*xray.ClientTraff
return nil, nil, common.NewError("Client Not Found In Inbound For Email:", clientEmail) return nil, nil, common.NewError("Client Not Found In Inbound For Email:", clientEmail)
} }
func (s *InboundService) SetClientTelegramUserID(trafficId int, tgId string) (bool, error) { func (s *InboundService) SetClientTelegramUserID(trafficId int, tgId int64) (bool, error) {
traffic, inbound, err := s.GetClientInboundByTrafficID(trafficId) traffic, inbound, err := s.GetClientInboundByTrafficID(trafficId)
if err != nil { if err != nil {
return false, err return false, err
@ -1687,10 +1687,10 @@ func (s *InboundService) DelDepletedClients(id int) (err error) {
return err return err
} }
func (s *InboundService) GetClientTrafficTgBot(tgId string) ([]*xray.ClientTraffic, error) { func (s *InboundService) GetClientTrafficTgBot(tgId int64) ([]*xray.ClientTraffic, error) {
db := database.GetDB() db := database.GetDB()
var inbounds []*model.Inbound var inbounds []*model.Inbound
err := db.Model(model.Inbound{}).Where("settings like ?", fmt.Sprintf(`%%"tgId": "%s"%%`, tgId)).Find(&inbounds).Error err := db.Model(model.Inbound{}).Where("settings like ?", fmt.Sprintf(`%%"tgId": %d%%`, tgId)).Find(&inbounds).Error
if err != nil && err != gorm.ErrRecordNotFound { if err != nil && err != gorm.ErrRecordNotFound {
return nil, err return nil, err
} }

View file

@ -6,7 +6,6 @@ import (
"net" "net"
"net/url" "net/url"
"os" "os"
"slices"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -41,6 +40,7 @@ type LoginStatus byte
const ( const (
LoginSuccess LoginStatus = 1 LoginSuccess LoginStatus = 1
LoginFail LoginStatus = 0 LoginFail LoginStatus = 0
EmptyTelegramUserID = int64(0)
) )
type Tgbot struct { type Tgbot struct {
@ -207,11 +207,9 @@ func (t *Tgbot) OnReceive() {
botHandler.HandleMessage(func(_ *telego.Bot, message telego.Message) { botHandler.HandleMessage(func(_ *telego.Bot, message telego.Message) {
if message.UsersShared != nil { if message.UsersShared != nil {
if checkAdmin(message.From.ID) { if checkAdmin(message.From.ID) {
userIDsStr := "" userIDs := message.UsersShared.UserIDs
for _, userID := range message.UsersShared.UserIDs { for _, userID := range userIDs {
userIDsStr += strconv.FormatInt(userID, 10) + " " needRestart, err := t.inboundService.SetClientTelegramUserID(message.UsersShared.RequestID, userID)
}
needRestart, err := t.inboundService.SetClientTelegramUserID(message.UsersShared.RequestID, userIDsStr)
if needRestart { if needRestart {
t.xrayService.SetToNeedRestart() t.xrayService.SetToNeedRestart()
} }
@ -222,6 +220,7 @@ func (t *Tgbot) OnReceive() {
output += t.I18nBot("tgbot.messages.userSaved") output += t.I18nBot("tgbot.messages.userSaved")
} }
t.SendMsgToTgbot(message.Chat.ID, output, tu.ReplyKeyboardRemove()) t.SendMsgToTgbot(message.Chat.ID, output, tu.ReplyKeyboardRemove())
}
} else { } else {
t.SendMsgToTgbot(message.Chat.ID, t.I18nBot("tgbot.noResult"), tu.ReplyKeyboardRemove()) t.SendMsgToTgbot(message.Chat.ID, t.I18nBot("tgbot.noResult"), tu.ReplyKeyboardRemove())
} }
@ -259,7 +258,9 @@ func (t *Tgbot) answerCommand(message *telego.Message, chatId int64, isAdmin boo
if isAdmin { if isAdmin {
t.searchClient(chatId, commandArgs[0]) t.searchClient(chatId, commandArgs[0])
} else { } else {
t.getClientUsage(chatId, strconv.FormatInt(message.From.ID, 10), commandArgs[0]) // Convert message.From.ID to int64
fromID := int64(message.From.ID)
t.getClientUsage(chatId, fromID, commandArgs[0])
} }
} else { } else {
msg += t.I18nBot("tgbot.commands.usage") msg += t.I18nBot("tgbot.commands.usage")
@ -726,7 +727,7 @@ func (t *Tgbot) asnwerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation")) t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
return return
} }
needRestart, err := t.inboundService.SetClientTelegramUserID(traffic.Id, "") needRestart, err := t.inboundService.SetClientTelegramUserID(traffic.Id, EmptyTelegramUserID)
if needRestart { if needRestart {
t.xrayService.SetToNeedRestart() t.xrayService.SetToNeedRestart()
} }
@ -783,8 +784,9 @@ func (t *Tgbot) asnwerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.getBanLogs")) t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.getBanLogs"))
t.sendBanLogs(chatId, true) t.sendBanLogs(chatId, true)
case "client_traffic": case "client_traffic":
tgUserID := callbackQuery.From.ID
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.clientUsage")) t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.clientUsage"))
t.getClientUsage(chatId, strconv.FormatInt(callbackQuery.From.ID, 10)) t.getClientUsage(chatId, tgUserID)
case "client_commands": case "client_commands":
t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.commands")) t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.commands"))
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.commands.helpClientCommands")) t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.commands.helpClientCommands"))
@ -1135,7 +1137,7 @@ func (t *Tgbot) clientInfoMsg(
return output return output
} }
func (t *Tgbot) getClientUsage(chatId int64, tgUserID string, email ...string) { func (t *Tgbot) getClientUsage(chatId int64, tgUserID int64, email ...string) {
traffics, err := t.inboundService.GetClientTrafficTgBot(tgUserID) traffics, err := t.inboundService.GetClientTrafficTgBot(tgUserID)
if err != nil { if err != nil {
logger.Warning(err) logger.Warning(err)
@ -1145,7 +1147,7 @@ func (t *Tgbot) getClientUsage(chatId int64, tgUserID string, email ...string) {
} }
if len(traffics) == 0 { if len(traffics) == 0 {
t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.askToAddUserId", "TgUserID=="+tgUserID)) t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.askToAddUserId", "TgUserID=="+strconv.FormatInt(tgUserID, 10)))
return return
} }
@ -1218,8 +1220,8 @@ func (t *Tgbot) clientTelegramUserInfo(chatId int64, email string, messageID ...
return return
} }
tgId := "None" tgId := "None"
if len(client.TgID) > 0 { if client.TgID != 0 {
tgId = client.TgID tgId = strconv.FormatInt(client.TgID, 10)
} }
output := "" output := ""
@ -1452,20 +1454,16 @@ func (t *Tgbot) notifyExhausted() {
logger.Warning("Unable to load Inbounds", err) logger.Warning("Unable to load Inbounds", err)
} }
var chatIDsDone []string var chatIDsDone []int64
for _, inbound := range inbounds { for _, inbound := range inbounds {
if inbound.Enable { if inbound.Enable {
if len(inbound.ClientStats) > 0 { if len(inbound.ClientStats) > 0 {
clients, err := t.inboundService.GetClients(inbound) clients, err := t.inboundService.GetClients(inbound)
if err == nil { if err == nil {
for _, client := range clients { for _, client := range clients {
if client.TgID != "" { if client.TgID != 0 {
chatID, err := strconv.ParseInt(client.TgID, 10, 64) chatID := client.TgID
if err != nil { if !int64Contains(chatIDsDone, chatID) && !checkAdmin(chatID) {
logger.Warning("TgID is not a number: ", client.TgID)
continue
}
if !slices.Contains(chatIDsDone, client.TgID) && !checkAdmin(chatID) {
var disabledClients []xray.ClientTraffic var disabledClients []xray.ClientTraffic
var exhaustedClients []xray.ClientTraffic var exhaustedClients []xray.ClientTraffic
traffics, err := t.inboundService.GetClientTrafficTgBot(client.TgID) traffics, err := t.inboundService.GetClientTrafficTgBot(client.TgID)
@ -1498,7 +1496,7 @@ func (t *Tgbot) notifyExhausted() {
} }
t.SendMsgToTgbot(chatID, output) t.SendMsgToTgbot(chatID, output)
} }
chatIDsDone = append(chatIDsDone, client.TgID) chatIDsDone = append(chatIDsDone, chatID)
} }
} }
} }
@ -1509,6 +1507,15 @@ func (t *Tgbot) notifyExhausted() {
} }
} }
func int64Contains(slice []int64, item int64) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}
func (t *Tgbot) onlineClients(chatId int64, messageID ...int) { func (t *Tgbot) onlineClients(chatId int64, messageID ...int) {
if !p.IsRunning() { if !p.IsRunning() {
return return