tgbot.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. package service
  2. import (
  3. "fmt"
  4. "net"
  5. "os"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "x-ui/config"
  10. "x-ui/database/model"
  11. "x-ui/logger"
  12. "x-ui/util/common"
  13. "x-ui/xray"
  14. tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
  15. )
  16. var bot *tgbotapi.BotAPI
  17. var adminIds []int64
  18. var isRunning bool
  19. type LoginStatus byte
  20. const (
  21. LoginSuccess LoginStatus = 1
  22. LoginFail LoginStatus = 0
  23. )
  24. type Tgbot struct {
  25. inboundService InboundService
  26. settingService SettingService
  27. serverService ServerService
  28. lastStatus *Status
  29. }
  30. func (t *Tgbot) NewTgbot() *Tgbot {
  31. return new(Tgbot)
  32. }
  33. func (t *Tgbot) Start() error {
  34. tgBottoken, err := t.settingService.GetTgBotToken()
  35. if err != nil || tgBottoken == "" {
  36. logger.Warning("Get TgBotToken failed:", err)
  37. return err
  38. }
  39. tgBotid, err := t.settingService.GetTgBotChatId()
  40. if err != nil {
  41. logger.Warning("Get GetTgBotChatId failed:", err)
  42. return err
  43. }
  44. for _, adminId := range strings.Split(tgBotid, ",") {
  45. id, err := strconv.Atoi(adminId)
  46. if err != nil {
  47. logger.Warning("Failed to get IDs from GetTgBotChatId:", err)
  48. return err
  49. }
  50. adminIds = append(adminIds, int64(id))
  51. }
  52. bot, err = tgbotapi.NewBotAPI(tgBottoken)
  53. if err != nil {
  54. fmt.Println("Get tgbot's api error:", err)
  55. return err
  56. }
  57. bot.Debug = false
  58. // listen for TG bot income messages
  59. if !isRunning {
  60. logger.Info("Starting Telegram receiver ...")
  61. go t.OnReceive()
  62. isRunning = true
  63. }
  64. return nil
  65. }
  66. func (t *Tgbot) IsRunnging() bool {
  67. return isRunning
  68. }
  69. func (t *Tgbot) Stop() {
  70. bot.StopReceivingUpdates()
  71. logger.Info("Stop Telegram receiver ...")
  72. isRunning = false
  73. adminIds = nil
  74. }
  75. func (t *Tgbot) OnReceive() {
  76. u := tgbotapi.NewUpdate(0)
  77. u.Timeout = 10
  78. updates := bot.GetUpdatesChan(u)
  79. for update := range updates {
  80. tgId := update.FromChat().ID
  81. chatId := update.FromChat().ChatConfig().ChatID
  82. isAdmin := checkAdmin(tgId)
  83. if update.Message == nil {
  84. if update.CallbackQuery != nil {
  85. t.asnwerCallback(update.CallbackQuery, isAdmin)
  86. }
  87. } else {
  88. if update.Message.IsCommand() {
  89. t.answerCommand(update.Message, chatId, isAdmin)
  90. } else {
  91. t.aswerChat(update.Message.Text, chatId, isAdmin)
  92. }
  93. }
  94. }
  95. }
  96. func (t *Tgbot) answerCommand(message *tgbotapi.Message, chatId int64, isAdmin bool) {
  97. msg := ""
  98. // Extract the command from the Message.
  99. switch message.Command() {
  100. case "help":
  101. msg = "This bot is providing you some specefic data from the server.\n\n Please choose:"
  102. case "start":
  103. msg = "Hello <i>" + message.From.FirstName + "</i> 👋"
  104. if isAdmin {
  105. hostname, _ := os.Hostname()
  106. msg += "\nWelcome to <b>" + hostname + "</b> management bot"
  107. }
  108. msg += "\n\nI can do some magics for you, please choose:"
  109. case "status":
  110. msg = "bot is ok ✅"
  111. case "usage":
  112. if len(message.CommandArguments()) > 1 {
  113. if isAdmin {
  114. t.searchClient(chatId, message.CommandArguments())
  115. } else {
  116. t.searchForClient(chatId, message.CommandArguments())
  117. }
  118. } else {
  119. msg = "❗Please provide a text for search!"
  120. }
  121. default:
  122. msg = "❗ Unknown command"
  123. }
  124. t.SendAnswer(chatId, msg, isAdmin)
  125. }
  126. func (t *Tgbot) aswerChat(message string, chatId int64, isAdmin bool) {
  127. t.SendAnswer(chatId, "❗ Unknown message", isAdmin)
  128. }
  129. func (t *Tgbot) asnwerCallback(callbackQuery *tgbotapi.CallbackQuery, isAdmin bool) {
  130. // Respond to the callback query, telling Telegram to show the user
  131. // a message with the data received.
  132. callback := tgbotapi.NewCallback(callbackQuery.ID, callbackQuery.Data)
  133. if _, err := bot.Request(callback); err != nil {
  134. logger.Warning(err)
  135. }
  136. switch callbackQuery.Data {
  137. case "get_usage":
  138. t.SendMsgToTgbot(callbackQuery.From.ID, t.getServerUsage())
  139. case "inbounds":
  140. t.SendMsgToTgbot(callbackQuery.From.ID, t.getInboundUsages())
  141. case "exhausted_soon":
  142. t.SendMsgToTgbot(callbackQuery.From.ID, t.getExhausted())
  143. case "get_backup":
  144. t.sendBackup(callbackQuery.From.ID)
  145. case "client_traffic":
  146. t.getClientUsage(callbackQuery.From.ID, callbackQuery.From.UserName)
  147. case "client_commands":
  148. t.SendMsgToTgbot(callbackQuery.From.ID, "To search for statistics, just use folowing command:\r\n \r\n<code>/usage [UID|Passowrd]</code>\r\n \r\nUse UID for vmess and vless and Password for Trojan.")
  149. case "commands":
  150. t.SendMsgToTgbot(callbackQuery.From.ID, "To search for a client email, just use folowing command:\r\n \r\n<code>/usage email</code>")
  151. }
  152. }
  153. func checkAdmin(tgId int64) bool {
  154. for _, adminId := range adminIds {
  155. if adminId == tgId {
  156. return true
  157. }
  158. }
  159. return false
  160. }
  161. func (t *Tgbot) SendAnswer(chatId int64, msg string, isAdmin bool) {
  162. var numericKeyboard = tgbotapi.NewInlineKeyboardMarkup(
  163. tgbotapi.NewInlineKeyboardRow(
  164. tgbotapi.NewInlineKeyboardButtonData("Server Usage", "get_usage"),
  165. tgbotapi.NewInlineKeyboardButtonData("Get DB Backup", "get_backup"),
  166. ),
  167. tgbotapi.NewInlineKeyboardRow(
  168. tgbotapi.NewInlineKeyboardButtonData("Get Inbounds", "inbounds"),
  169. tgbotapi.NewInlineKeyboardButtonData("Exhausted soon", "exhausted_soon"),
  170. ),
  171. tgbotapi.NewInlineKeyboardRow(
  172. tgbotapi.NewInlineKeyboardButtonData("Commands", "commands"),
  173. ),
  174. )
  175. var numericKeyboardClient = tgbotapi.NewInlineKeyboardMarkup(
  176. tgbotapi.NewInlineKeyboardRow(
  177. tgbotapi.NewInlineKeyboardButtonData("Get Usage", "client_traffic"),
  178. tgbotapi.NewInlineKeyboardButtonData("Commands", "client_commands"),
  179. ),
  180. )
  181. msgConfig := tgbotapi.NewMessage(chatId, msg)
  182. msgConfig.ParseMode = "HTML"
  183. if isAdmin {
  184. msgConfig.ReplyMarkup = numericKeyboard
  185. } else {
  186. msgConfig.ReplyMarkup = numericKeyboardClient
  187. }
  188. _, err := bot.Send(msgConfig)
  189. if err != nil {
  190. logger.Warning("Error sending telegram message :", err)
  191. }
  192. }
  193. func (t *Tgbot) SendMsgToTgbot(tgid int64, msg string) {
  194. var allMessages []string
  195. limit := 2000
  196. // paging message if it is big
  197. if len(msg) > limit {
  198. messages := strings.Split(msg, "\r\n \r\n")
  199. lastIndex := -1
  200. for _, message := range messages {
  201. if (len(allMessages) == 0) || (len(allMessages[lastIndex])+len(message) > limit) {
  202. allMessages = append(allMessages, message)
  203. lastIndex++
  204. } else {
  205. allMessages[lastIndex] += "\r\n \r\n" + message
  206. }
  207. }
  208. } else {
  209. allMessages = append(allMessages, msg)
  210. }
  211. for _, message := range allMessages {
  212. info := tgbotapi.NewMessage(tgid, message)
  213. info.ParseMode = "HTML"
  214. _, err := bot.Send(info)
  215. if err != nil {
  216. logger.Warning("Error sending telegram message :", err)
  217. }
  218. time.Sleep(500 * time.Millisecond)
  219. }
  220. }
  221. func (t *Tgbot) SendMsgToTgbotAdmins(msg string) {
  222. for _, adminId := range adminIds {
  223. t.SendMsgToTgbot(adminId, msg)
  224. }
  225. }
  226. func (t *Tgbot) SendReport() {
  227. runTime, err := t.settingService.GetTgbotRuntime()
  228. if err == nil && len(runTime) > 0 {
  229. t.SendMsgToTgbotAdmins("🕰 Scheduled reports: " + runTime + "\r\nDate-Time: " + time.Now().Format("2006-01-02 15:04:05"))
  230. }
  231. info := t.getServerUsage()
  232. t.SendMsgToTgbotAdmins(info)
  233. exhausted := t.getExhausted()
  234. t.SendMsgToTgbotAdmins(exhausted)
  235. backupEnable, err := t.settingService.GetTgBotBackup()
  236. if err == nil && backupEnable {
  237. for _, adminId := range adminIds {
  238. t.sendBackup(int64(adminId))
  239. }
  240. }
  241. }
  242. func (t *Tgbot) getServerUsage() string {
  243. var info string
  244. //get hostname
  245. name, err := os.Hostname()
  246. if err != nil {
  247. logger.Error("get hostname error:", err)
  248. name = ""
  249. }
  250. info = fmt.Sprintf("💻 Hostname: %s\r\n", name)
  251. //get ip address
  252. var ip string
  253. var ipv6 string
  254. netInterfaces, err := net.Interfaces()
  255. if err != nil {
  256. logger.Error("net.Interfaces failed, err:", err.Error())
  257. info += "🌐 IP: Unknown\r\n \r\n"
  258. } else {
  259. for i := 0; i < len(netInterfaces); i++ {
  260. if (netInterfaces[i].Flags & net.FlagUp) != 0 {
  261. addrs, _ := netInterfaces[i].Addrs()
  262. for _, address := range addrs {
  263. if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
  264. if ipnet.IP.To4() != nil {
  265. ip += ipnet.IP.String() + " "
  266. } else if ipnet.IP.To16() != nil && !ipnet.IP.IsLinkLocalUnicast() {
  267. ipv6 += ipnet.IP.String() + " "
  268. }
  269. }
  270. }
  271. }
  272. }
  273. info += fmt.Sprintf("🌐IP: %s\r\n🌐IPv6: %s\r\n", ip, ipv6)
  274. }
  275. // get latest status of server
  276. t.lastStatus = t.serverService.GetStatus(t.lastStatus)
  277. info += fmt.Sprintf("🔌Server Uptime: %d days\r\n", int(t.lastStatus.Uptime/86400))
  278. info += fmt.Sprintf("📈Server Load: %.1f, %.1f, %.1f\r\n", t.lastStatus.Loads[0], t.lastStatus.Loads[1], t.lastStatus.Loads[2])
  279. info += fmt.Sprintf("📋Server Memory: %s/%s\r\n", common.FormatTraffic(int64(t.lastStatus.Mem.Current)), common.FormatTraffic(int64(t.lastStatus.Mem.Total)))
  280. info += fmt.Sprintf("🔹TcpCount: %d\r\n", t.lastStatus.TcpCount)
  281. info += fmt.Sprintf("🔸UdpCount: %d\r\n", t.lastStatus.UdpCount)
  282. info += fmt.Sprintf("🚦Traffic: %s (↑%s,↓%s)\r\n", common.FormatTraffic(int64(t.lastStatus.NetTraffic.Sent+t.lastStatus.NetTraffic.Recv)), common.FormatTraffic(int64(t.lastStatus.NetTraffic.Sent)), common.FormatTraffic(int64(t.lastStatus.NetTraffic.Recv)))
  283. info += fmt.Sprintf("ℹXray status: %s", t.lastStatus.Xray.State)
  284. return info
  285. }
  286. func (t *Tgbot) UserLoginNotify(username string, ip string, time string, status LoginStatus) {
  287. if username == "" || ip == "" || time == "" {
  288. logger.Warning("UserLoginNotify failed,invalid info")
  289. return
  290. }
  291. var msg string
  292. // Get hostname
  293. name, err := os.Hostname()
  294. if err != nil {
  295. logger.Warning("get hostname error:", err)
  296. return
  297. }
  298. if status == LoginSuccess {
  299. msg = fmt.Sprintf("✅ Successfully logged-in to the panel\r\nHostname:%s\r\n", name)
  300. } else if status == LoginFail {
  301. msg = fmt.Sprintf("❗ Login to the panel was unsuccessful\r\nHostname:%s\r\n", name)
  302. }
  303. msg += fmt.Sprintf("⏰ Time:%s\r\n", time)
  304. msg += fmt.Sprintf("🆔 Username:%s\r\n", username)
  305. msg += fmt.Sprintf("🌐 IP:%s\r\n", ip)
  306. t.SendMsgToTgbotAdmins(msg)
  307. }
  308. func (t *Tgbot) getInboundUsages() string {
  309. info := ""
  310. // get traffic
  311. inbouds, err := t.inboundService.GetAllInbounds()
  312. if err != nil {
  313. logger.Warning("GetAllInbounds run failed:", err)
  314. info += "❌ Failed to get inbounds"
  315. } else {
  316. // NOTE:If there no any sessions here,need to notify here
  317. // TODO:Sub-node push, automatic conversion format
  318. for _, inbound := range inbouds {
  319. info += fmt.Sprintf("📍Inbound:%s\r\nPort:%d\r\n", inbound.Remark, inbound.Port)
  320. info += fmt.Sprintf("Traffic: %s (↑%s,↓%s)\r\n", common.FormatTraffic((inbound.Up + inbound.Down)), common.FormatTraffic(inbound.Up), common.FormatTraffic(inbound.Down))
  321. if inbound.ExpiryTime == 0 {
  322. info += "Expire date: ♾ Unlimited\r\n \r\n"
  323. } else {
  324. info += fmt.Sprintf("Expire date:%s\r\n \r\n", time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05"))
  325. }
  326. }
  327. }
  328. return info
  329. }
  330. func (t *Tgbot) getClientUsage(chatId int64, tgUserName string) {
  331. traffics, err := t.inboundService.GetClientTrafficTgBot(tgUserName)
  332. if err != nil {
  333. logger.Warning(err)
  334. msg := "❌ Something went wrong!"
  335. t.SendMsgToTgbot(chatId, msg)
  336. return
  337. }
  338. if len(traffics) == 0 {
  339. msg := "Your configuration is not found!\nPlease ask your Admin to use your telegram username in your configuration(s).\n\nYour username: <b>@" + tgUserName + "</b>"
  340. t.SendMsgToTgbot(chatId, msg)
  341. }
  342. for _, traffic := range traffics {
  343. expiryTime := ""
  344. if traffic.ExpiryTime == 0 {
  345. expiryTime = "♾Unlimited"
  346. } else {
  347. expiryTime = time.Unix((traffic.ExpiryTime / 1000), 0).Format("2006-01-02 15:04:05")
  348. }
  349. total := ""
  350. if traffic.Total == 0 {
  351. total = "♾Unlimited"
  352. } else {
  353. total = common.FormatTraffic((traffic.Total))
  354. }
  355. 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",
  356. traffic.Enable, traffic.Email, common.FormatTraffic(traffic.Up), common.FormatTraffic(traffic.Down), common.FormatTraffic((traffic.Up + traffic.Down)),
  357. total, expiryTime)
  358. t.SendMsgToTgbot(chatId, output)
  359. }
  360. t.SendAnswer(chatId, "Please choose:", false)
  361. }
  362. func (t *Tgbot) searchClient(chatId int64, email string) {
  363. traffics, err := t.inboundService.GetClientTrafficByEmail(email)
  364. if err != nil {
  365. logger.Warning(err)
  366. msg := "❌ Something went wrong!"
  367. t.SendMsgToTgbot(chatId, msg)
  368. return
  369. }
  370. if len(traffics) == 0 {
  371. msg := "No result!"
  372. t.SendMsgToTgbot(chatId, msg)
  373. return
  374. }
  375. for _, traffic := range traffics {
  376. expiryTime := ""
  377. if traffic.ExpiryTime == 0 {
  378. expiryTime = "♾Unlimited"
  379. } else {
  380. expiryTime = time.Unix((traffic.ExpiryTime / 1000), 0).Format("2006-01-02 15:04:05")
  381. }
  382. total := ""
  383. if traffic.Total == 0 {
  384. total = "♾Unlimited"
  385. } else {
  386. total = common.FormatTraffic((traffic.Total))
  387. }
  388. 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",
  389. traffic.Enable, traffic.Email, common.FormatTraffic(traffic.Up), common.FormatTraffic(traffic.Down), common.FormatTraffic((traffic.Up + traffic.Down)),
  390. total, expiryTime)
  391. t.SendMsgToTgbot(chatId, output)
  392. }
  393. }
  394. func (t *Tgbot) searchForClient(chatId int64, query string) {
  395. traffic, err := t.inboundService.SearchClientTraffic(query)
  396. if err != nil {
  397. logger.Warning(err)
  398. msg := "❌ Something went wrong!"
  399. t.SendMsgToTgbot(chatId, msg)
  400. return
  401. }
  402. if traffic == nil {
  403. msg := "No result!"
  404. t.SendMsgToTgbot(chatId, msg)
  405. return
  406. }
  407. expiryTime := ""
  408. if traffic.ExpiryTime == 0 {
  409. expiryTime = "♾Unlimited"
  410. } else {
  411. expiryTime = time.Unix((traffic.ExpiryTime / 1000), 0).Format("2006-01-02 15:04:05")
  412. }
  413. total := ""
  414. if traffic.Total == 0 {
  415. total = "♾Unlimited"
  416. } else {
  417. total = common.FormatTraffic((traffic.Total))
  418. }
  419. 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",
  420. traffic.Enable, traffic.Email, common.FormatTraffic(traffic.Up), common.FormatTraffic(traffic.Down), common.FormatTraffic((traffic.Up + traffic.Down)),
  421. total, expiryTime)
  422. t.SendMsgToTgbot(chatId, output)
  423. }
  424. func (t *Tgbot) getExhausted() string {
  425. trDiff := int64(0)
  426. exDiff := int64(0)
  427. now := time.Now().Unix() * 1000
  428. var exhaustedInbounds []model.Inbound
  429. var exhaustedClients []xray.ClientTraffic
  430. var disabledInbounds []model.Inbound
  431. var disabledClients []xray.ClientTraffic
  432. output := ""
  433. TrafficThreshold, err := t.settingService.GetTgTrafficDiff()
  434. if err == nil && TrafficThreshold > 0 {
  435. trDiff = int64(TrafficThreshold) * 1073741824
  436. }
  437. ExpireThreshold, err := t.settingService.GetTgExpireDiff()
  438. if err == nil && ExpireThreshold > 0 {
  439. exDiff = int64(ExpireThreshold) * 84600
  440. }
  441. inbounds, err := t.inboundService.GetAllInbounds()
  442. if err != nil {
  443. logger.Warning("Unable to load Inbounds", err)
  444. }
  445. for _, inbound := range inbounds {
  446. if inbound.Enable {
  447. if (inbound.ExpiryTime > 0 && (now-inbound.ExpiryTime < exDiff)) ||
  448. (inbound.Total > 0 && (inbound.Total-inbound.Up+inbound.Down < trDiff)) {
  449. exhaustedInbounds = append(exhaustedInbounds, *inbound)
  450. }
  451. if len(inbound.ClientStats) > 0 {
  452. for _, client := range inbound.ClientStats {
  453. if client.Enable {
  454. if (client.ExpiryTime > 0 && (now-client.ExpiryTime < exDiff)) ||
  455. (client.Total > 0 && (client.Total-client.Up+client.Down < trDiff)) {
  456. exhaustedClients = append(exhaustedClients, client)
  457. }
  458. } else {
  459. disabledClients = append(disabledClients, client)
  460. }
  461. }
  462. }
  463. } else {
  464. disabledInbounds = append(disabledInbounds, *inbound)
  465. }
  466. }
  467. output += fmt.Sprintf("Exhausted Inbounds count:\r\n🛑 Disabled: %d\r\n🔜 Exhaust soon: %d\r\n \r\n", len(disabledInbounds), len(exhaustedInbounds))
  468. if len(disabledInbounds)+len(exhaustedInbounds) > 0 {
  469. output += "Exhausted Inbounds:\r\n"
  470. for _, inbound := range exhaustedInbounds {
  471. output += fmt.Sprintf("📍Inbound:%s\r\nPort:%d\r\nTraffic: %s (↑%s,↓%s)\r\n", inbound.Remark, inbound.Port, common.FormatTraffic((inbound.Up + inbound.Down)), common.FormatTraffic(inbound.Up), common.FormatTraffic(inbound.Down))
  472. if inbound.ExpiryTime == 0 {
  473. output += "Expire date: ♾Unlimited\r\n \r\n"
  474. } else {
  475. output += fmt.Sprintf("Expire date:%s\r\n \r\n", time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05"))
  476. }
  477. }
  478. }
  479. output += fmt.Sprintf("Exhausted Clients count:\r\n🛑 Disabled: %d\r\n🔜 Exhaust soon: %d\r\n \r\n", len(disabledClients), len(exhaustedClients))
  480. if len(disabledClients)+len(exhaustedClients) > 0 {
  481. output += "Exhausted Clients:\r\n"
  482. for _, traffic := range exhaustedClients {
  483. expiryTime := ""
  484. if traffic.ExpiryTime == 0 {
  485. expiryTime = "♾Unlimited"
  486. } else {
  487. expiryTime = time.Unix((traffic.ExpiryTime / 1000), 0).Format("2006-01-02 15:04:05")
  488. }
  489. total := ""
  490. if traffic.Total == 0 {
  491. total = "♾Unlimited"
  492. } else {
  493. total = common.FormatTraffic((traffic.Total))
  494. }
  495. 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",
  496. traffic.Enable, traffic.Email, common.FormatTraffic(traffic.Up), common.FormatTraffic(traffic.Down), common.FormatTraffic((traffic.Up + traffic.Down)),
  497. total, expiryTime)
  498. }
  499. }
  500. return output
  501. }
  502. func (t *Tgbot) sendBackup(chatId int64) {
  503. sendingTime := time.Now().Format("2006-01-02 15:04:05")
  504. t.SendMsgToTgbot(chatId, "Backup time: "+sendingTime)
  505. file := tgbotapi.FilePath(config.GetDBPath())
  506. msg := tgbotapi.NewDocument(chatId, file)
  507. _, err := bot.Send(msg)
  508. if err != nil {
  509. logger.Warning("Error in uploading backup: ", err)
  510. }
  511. }