Skip to content

Commit ad7b954

Browse files
authored
[tgBot] TG bot command to get client info by user (#110)
* [tgBot] TG bot command to get client info by user * remove test log warning
1 parent 4421fec commit ad7b954

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

web/job/stats_notify_job.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,114 @@ func (j *StatsNotifyJob) UserLoginNotify(username string, ip string, time string
135135
msg += fmt.Sprintf("IP:%s\r\n", ip)
136136
j.SendMsgToTgbot(msg)
137137
}
138+
139+
140+
var numericKeyboard = tgbotapi.NewInlineKeyboardMarkup(
141+
tgbotapi.NewInlineKeyboardRow(
142+
tgbotapi.NewInlineKeyboardButtonData("Get Usage", "get_usage"),
143+
),
144+
)
145+
146+
func (j *StatsNotifyJob) OnReceive() *StatsNotifyJob {
147+
tgBottoken, err := j.settingService.GetTgBotToken()
148+
if err != nil || tgBottoken == "" {
149+
logger.Warning("sendMsgToTgbot failed,GetTgBotToken fail:", err)
150+
return j
151+
}
152+
bot, err := tgbotapi.NewBotAPI(tgBottoken)
153+
if err != nil {
154+
fmt.Println("get tgbot error:", err)
155+
return j
156+
}
157+
bot.Debug = false
158+
u := tgbotapi.NewUpdate(0)
159+
u.Timeout = 10
160+
161+
updates := bot.GetUpdatesChan(u)
162+
163+
for update := range updates {
164+
if update.Message == nil {
165+
166+
if update.CallbackQuery != nil {
167+
// Respond to the callback query, telling Telegram to show the user
168+
// a message with the data received.
169+
callback := tgbotapi.NewCallback(update.CallbackQuery.ID, update.CallbackQuery.Data)
170+
if _, err := bot.Request(callback); err != nil {
171+
logger.Warning(err)
172+
}
173+
174+
// And finally, send a message containing the data received.
175+
msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, "")
176+
177+
switch update.CallbackQuery.Data {
178+
case "get_usage":
179+
msg.Text = "for get your usage send command like this : \n <code>/usage uuid | id</code> \n example : <code>/usage fc3239ed-8f3b-4151-ff51-b183d5182142</code>"
180+
msg.ParseMode = "HTML"
181+
}
182+
if _, err := bot.Send(msg); err != nil {
183+
logger.Warning(err)
184+
}
185+
}
186+
187+
continue
188+
}
189+
190+
if !update.Message.IsCommand() { // ignore any non-command Messages
191+
continue
192+
}
193+
194+
// Create a new MessageConfig. We don't have text yet,
195+
// so we leave it empty.
196+
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
197+
198+
// Extract the command from the Message.
199+
switch update.Message.Command() {
200+
case "help":
201+
msg.Text = "What you need?"
202+
msg.ReplyMarkup = numericKeyboard
203+
case "start":
204+
msg.Text = "Hi :) \n What you need?"
205+
msg.ReplyMarkup = numericKeyboard
206+
207+
case "status":
208+
msg.Text = "bot is ok."
209+
210+
case "usage":
211+
msg.Text = j.getClientUsage(update.Message.CommandArguments())
212+
default:
213+
msg.Text = "I don't know that command, /help"
214+
msg.ReplyMarkup = numericKeyboard
215+
216+
}
217+
218+
if _, err := bot.Send(msg); err != nil {
219+
logger.Warning(err)
220+
}
221+
}
222+
return j
223+
224+
}
225+
func (j *StatsNotifyJob) getClientUsage(id string) string {
226+
traffic , err := j.inboundService.GetClientTrafficById(id)
227+
if err != nil {
228+
logger.Warning(err)
229+
return "something wrong!"
230+
}
231+
expiryTime := ""
232+
if traffic.ExpiryTime == 0 {
233+
expiryTime = fmt.Sprintf("unlimited")
234+
} else {
235+
expiryTime = fmt.Sprintf("%s", time.Unix((traffic.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05"))
236+
}
237+
total := ""
238+
if traffic.Total == 0 {
239+
total = fmt.Sprintf("unlimited")
240+
} else {
241+
total = fmt.Sprintf("%s", common.FormatTraffic((traffic.Total)))
242+
}
243+
output := fmt.Sprintf("💡 Active: %t\r\n📧 Email: %s\r\n🔼 Upload↑: %s\r\n🔽 Download↓: %s\r\n🔄 Total: %s / %s\r\n📅 Expire in: %s\r\n",
244+
traffic.Enable, traffic.Email, common.FormatTraffic(traffic.Up), common.FormatTraffic(traffic.Down), common.FormatTraffic((traffic.Up + traffic.Down)),
245+
total, expiryTime)
246+
247+
return output
248+
}

web/service/inbound.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,33 @@ func (s *InboundService) ResetClientTraffic(clientEmail string) (error) {
408408
}
409409
return nil
410410
}
411+
func (s *InboundService) GetClientTrafficById(uuid string) (traffic *xray.ClientTraffic, err error) {
412+
db := database.GetDB()
413+
inbound := &model.Inbound{}
414+
traffic = &xray.ClientTraffic{}
415+
416+
err = db.Model(model.Inbound{}).Where("settings like ?", "%" + uuid + "%").First(inbound).Error
417+
if err != nil {
418+
if err == gorm.ErrRecordNotFound {
419+
logger.Warning(err)
420+
return nil, err
421+
}
422+
}
423+
traffic.InboundId = inbound.Id
424+
425+
// get settings clients
426+
settings := map[string][]model.Client{}
427+
json.Unmarshal([]byte(inbound.Settings), &settings)
428+
clients := settings["clients"]
429+
for _, client := range clients {
430+
if uuid == client.ID {
431+
traffic.Email = client.Email
432+
}
433+
}
434+
err = db.Model(xray.ClientTraffic{}).Where("email = ?", traffic.Email).First(traffic).Error
435+
if err != nil {
436+
logger.Warning(err)
437+
return nil, err
438+
}
439+
return traffic, err
440+
}

web/web.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@ func (s *Server) startTask() {
328328
logger.Warning("Add NewStatsNotifyJob error", err)
329329
return
330330
}
331+
// listen for TG bot income messages
332+
go job.NewStatsNotifyJob().OnReceive()
331333
} else {
332334
s.cron.Remove(entry)
333335
}

0 commit comments

Comments
 (0)