1
0

tgbot.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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 isAdmin {
  113. t.searchClient(chatId, message.CommandArguments())
  114. } else {
  115. t.searchForClient(chatId, message.CommandArguments())
  116. }
  117. default:
  118. msg = "❗ Unknown command"
  119. }
  120. t.SendAnswer(chatId, msg, isAdmin)
  121. }
  122. func (t *Tgbot) aswerChat(message string, chatId int64, isAdmin bool) {
  123. t.SendAnswer(chatId, "❗ Unknown message", isAdmin)
  124. }
  125. func (t *Tgbot) asnwerCallback(callbackQuery *tgbotapi.CallbackQuery, isAdmin bool) {
  126. // Respond to the callback query, telling Telegram to show the user
  127. // a message with the data received.
  128. callback := tgbotapi.NewCallback(callbackQuery.ID, callbackQuery.Data)
  129. if _, err := bot.Request(callback); err != nil {
  130. logger.Warning(err)
  131. }
  132. switch callbackQuery.Data {
  133. case "get_usage":
  134. t.SendMsgToTgbot(callbackQuery.From.ID, t.getServerUsage())
  135. case "inbounds":
  136. t.SendMsgToTgbot(callbackQuery.From.ID, t.getInboundUsages())
  137. case "exhausted_soon":
  138. t.SendMsgToTgbot(callbackQuery.From.ID, t.getExhausted())
  139. case "get_backup":
  140. t.sendBackup(callbackQuery.From.ID)
  141. case "client_traffic":
  142. t.getClientUsage(callbackQuery.From.ID, callbackQuery.From.UserName)
  143. case "client_commands":
  144. 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.")
  145. case "commands":
  146. t.SendMsgToTgbot(callbackQuery.From.ID, "To search for a client email, just use folowing command:\r\n \r\n<code>/usage email</code>")
  147. }
  148. }
  149. func checkAdmin(tgId int64) bool {
  150. for _, adminId := range adminIds {
  151. if adminId == tgId {
  152. return true
  153. }
  154. }
  155. return false
  156. }
  157. func (t *Tgbot) SendAnswer(chatId int64, msg string, isAdmin bool) {
  158. var numericKeyboard = tgbotapi.NewInlineKeyboardMarkup(
  159. tgbotapi.NewInlineKeyboardRow(
  160. tgbotapi.NewInlineKeyboardButtonData("Server Usage", "get_usage"),
  161. tgbotapi.NewInlineKeyboardButtonData("Get DB Backup", "get_backup"),
  162. ),
  163. tgbotapi.NewInlineKeyboardRow(
  164. tgbotapi.NewInlineKeyboardButtonData("Get Inbounds", "inbounds"),
  165. tgbotapi.NewInlineKeyboardButtonData("Exhausted soon", "exhausted_soon"),
  166. ),
  167. tgbotapi.NewInlineKeyboardRow(
  168. tgbotapi.NewInlineKeyboardButtonData("Commands", "commands"),
  169. ),
  170. )
  171. var numericKeyboardClient = tgbotapi.NewInlineKeyboardMarkup(
  172. tgbotapi.NewInlineKeyboardRow(
  173. tgbotapi.NewInlineKeyboardButtonData("Get Usage", "client_traffic"),
  174. tgbotapi.NewInlineKeyboardButtonData("Commands", "client_commands"),
  175. ),
  176. )
  177. msgConfig := tgbotapi.NewMessage(chatId, msg)
  178. msgConfig.ParseMode = "HTML"
  179. if isAdmin {
  180. msgConfig.ReplyMarkup = numericKeyboard
  181. } else {
  182. msgConfig.ReplyMarkup = numericKeyboardClient
  183. }
  184. _, err := bot.Send(msgConfig)
  185. if err != nil {
  186. logger.Warning("Error sending telegram message :", err)
  187. }
  188. }
  189. func (t *Tgbot) SendMsgToTgbot(tgid int64, msg string) {
  190. var allMessages []string
  191. limit := 2000
  192. // paging message if it is big
  193. if len(msg) > limit {
  194. messages := strings.Split(msg, "\r\n \r\n")
  195. lastIndex := -1
  196. for _, message := range messages {
  197. if (len(allMessages) == 0) || (len(allMessages[lastIndex])+len(message) > limit) {
  198. allMessages = append(allMessages, message)
  199. lastIndex++
  200. } else {
  201. allMessages[lastIndex] += "\r\n \r\n" + message
  202. }
  203. }
  204. } else {
  205. allMessages = append(allMessages, msg)
  206. }
  207. for _, message := range allMessages {
  208. info := tgbotapi.NewMessage(tgid, message)
  209. info.ParseMode = "HTML"
  210. _, err := bot.Send(info)
  211. if err != nil {
  212. logger.Warning("Error sending telegram message :", err)
  213. }
  214. time.Sleep(500 * time.Millisecond)
  215. }
  216. }
  217. func (t *Tgbot) SendMsgToTgbotAdmins(msg string) {
  218. for _, adminId := range adminIds {
  219. t.SendMsgToTgbot(adminId, msg)
  220. }
  221. }
  222. func (t *Tgbot) SendReport() {
  223. runTime, err := t.settingService.GetTgbotRuntime()
  224. if err == nil && len(runTime) > 0 {
  225. t.SendMsgToTgbotAdmins("🕰 Scheduled reports: " + runTime + "\r\nDate-Time: " + time.Now().Format("2006-01-02 15:04:05"))
  226. }
  227. info := t.getServerUsage()
  228. t.SendMsgToTgbotAdmins(info)
  229. exhausted := t.getExhausted()
  230. t.SendMsgToTgbotAdmins(exhausted)
  231. backupEnable, err := t.settingService.GetTgBotBackup()
  232. if err == nil && backupEnable {
  233. for _, adminId := range adminIds {
  234. t.sendBackup(int64(adminId))
  235. }
  236. }
  237. }
  238. func (t *Tgbot) getServerUsage() string {
  239. var info string
  240. //get hostname
  241. name, err := os.Hostname()
  242. if err != nil {
  243. logger.Error("get hostname error:", err)
  244. name = ""
  245. }
  246. info = fmt.Sprintf("💻 Hostname: %s\r\n", name)
  247. //get ip address
  248. var ip string
  249. var ipv6 string
  250. netInterfaces, err := net.Interfaces()
  251. if err != nil {
  252. logger.Error("net.Interfaces failed, err:", err.Error())
  253. info += "🌐 IP: Unknown\r\n \r\n"
  254. } else {
  255. for i := 0; i < len(netInterfaces); i++ {
  256. if (netInterfaces[i].Flags & net.FlagUp) != 0 {
  257. addrs, _ := netInterfaces[i].Addrs()
  258. for _, address := range addrs {
  259. if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
  260. if ipnet.IP.To4() != nil {
  261. ip += ipnet.IP.String() + " "
  262. } else if ipnet.IP.To16() != nil && !ipnet.IP.IsLinkLocalUnicast() {
  263. ipv6 += ipnet.IP.String() + " "
  264. }
  265. }
  266. }
  267. }
  268. }
  269. info += fmt.Sprintf("🌐IP: %s\r\n🌐IPv6: %s\r\n", ip, ipv6)
  270. }
  271. // get latest status of server
  272. t.lastStatus = t.serverService.GetStatus(t.lastStatus)
  273. info += fmt.Sprintf("🔌Server Uptime: %d days\r\n", int(t.lastStatus.Uptime/86400))
  274. info += fmt.Sprintf("📈Server Load: %.1f, %.1f, %.1f\r\n", t.lastStatus.Loads[0], t.lastStatus.Loads[1], t.lastStatus.Loads[2])
  275. info += fmt.Sprintf("📋Server Memory: %s/%s\r\n", common.FormatTraffic(int64(t.lastStatus.Mem.Current)), common.FormatTraffic(int64(t.lastStatus.Mem.Total)))
  276. info += fmt.Sprintf("🔹TcpCount: %d\r\n", t.lastStatus.TcpCount)
  277. info += fmt.Sprintf("🔸UdpCount: %d\r\n", t.lastStatus.UdpCount)
  278. 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)))
  279. info += fmt.Sprintf("ℹXray status: %s", t.lastStatus.Xray.State)
  280. return info
  281. }
  282. func (t *Tgbot) UserLoginNotify(username string, ip string, time string, status LoginStatus) {
  283. if username == "" || ip == "" || time == "" {
  284. logger.Warning("UserLoginNotify failed,invalid info")
  285. return
  286. }
  287. var msg string
  288. // Get hostname
  289. name, err := os.Hostname()
  290. if err != nil {
  291. logger.Warning("get hostname error:", err)
  292. return
  293. }
  294. if status == LoginSuccess {
  295. msg = fmt.Sprintf("✅ Successfully logged-in to the panel\r\nHostname:%s\r\n", name)
  296. } else if status == LoginFail {
  297. msg = fmt.Sprintf("❗ Login to the panel was unsuccessful\r\nHostname:%s\r\n", name)
  298. }
  299. msg += fmt.Sprintf("⏰ Time:%s\r\n", time)
  300. msg += fmt.Sprintf("🆔 Username:%s\r\n", username)
  301. msg += fmt.Sprintf("🌐 IP:%s\r\n", ip)
  302. t.SendMsgToTgbotAdmins(msg)
  303. }
  304. func (t *Tgbot) getInboundUsages() string {
  305. info := ""
  306. // get traffic
  307. inbouds, err := t.inboundService.GetAllInbounds()
  308. if err != nil {
  309. logger.Warning("GetAllInbounds run failed:", err)
  310. info += "❌ Failed to get inbounds"
  311. } else {
  312. // NOTE:If there no any sessions here,need to notify here
  313. // TODO:Sub-node push, automatic conversion format
  314. for _, inbound := range inbouds {
  315. info += fmt.Sprintf("📍Inbound:%s\r\nPort:%d\r\n", inbound.Remark, inbound.Port)
  316. info += fmt.Sprintf("Traffic: %s (↑%s,↓%s)\r\n", common.FormatTraffic((inbound.Up + inbound.Down)), common.FormatTraffic(inbound.Up), common.FormatTraffic(inbound.Down))
  317. if inbound.ExpiryTime == 0 {
  318. info += "Expire date: ♾ Unlimited\r\n \r\n"
  319. } else {
  320. info += fmt.Sprintf("Expire date:%s\r\n \r\n", time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05"))
  321. }
  322. }
  323. }
  324. return info
  325. }
  326. func (t *Tgbot) getClientUsage(chatId int64, tgUserName string) {
  327. traffics, err := t.inboundService.GetClientTrafficTgBot(tgUserName)
  328. if err != nil {
  329. logger.Warning(err)
  330. msg := "❌ Something went wrong!"
  331. t.SendMsgToTgbot(chatId, msg)
  332. return
  333. }
  334. if len(traffics) == 0 {
  335. 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>"
  336. t.SendMsgToTgbot(chatId, msg)
  337. }
  338. for _, traffic := range traffics {
  339. expiryTime := ""
  340. if traffic.ExpiryTime == 0 {
  341. expiryTime = "♾Unlimited"
  342. } else {
  343. expiryTime = time.Unix((traffic.ExpiryTime / 1000), 0).Format("2006-01-02 15:04:05")
  344. }
  345. total := ""
  346. if traffic.Total == 0 {
  347. total = "♾Unlimited"
  348. } else {
  349. total = common.FormatTraffic((traffic.Total))
  350. }
  351. 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",
  352. traffic.Enable, traffic.Email, common.FormatTraffic(traffic.Up), common.FormatTraffic(traffic.Down), common.FormatTraffic((traffic.Up + traffic.Down)),
  353. total, expiryTime)
  354. t.SendMsgToTgbot(chatId, output)
  355. }
  356. t.SendAnswer(chatId, "Please choose:", false)
  357. }
  358. func (t *Tgbot) searchClient(chatId int64, email string) {
  359. traffics, err := t.inboundService.GetClientTrafficByEmail(email)
  360. if err != nil {
  361. logger.Warning(err)
  362. msg := "❌ Something went wrong!"
  363. t.SendMsgToTgbot(chatId, msg)
  364. return
  365. }
  366. if len(traffics) == 0 {
  367. msg := "No result!"
  368. t.SendMsgToTgbot(chatId, msg)
  369. return
  370. }
  371. for _, traffic := range traffics {
  372. expiryTime := ""
  373. if traffic.ExpiryTime == 0 {
  374. expiryTime = "♾Unlimited"
  375. } else {
  376. expiryTime = time.Unix((traffic.ExpiryTime / 1000), 0).Format("2006-01-02 15:04:05")
  377. }
  378. total := ""
  379. if traffic.Total == 0 {
  380. total = "♾Unlimited"
  381. } else {
  382. total = common.FormatTraffic((traffic.Total))
  383. }
  384. 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",
  385. traffic.Enable, traffic.Email, common.FormatTraffic(traffic.Up), common.FormatTraffic(traffic.Down), common.FormatTraffic((traffic.Up + traffic.Down)),
  386. total, expiryTime)
  387. t.SendMsgToTgbot(chatId, output)
  388. }
  389. }
  390. func (t *Tgbot) searchForClient(chatId int64, query string) {
  391. traffic, err := t.inboundService.SearchClientTraffic(query)
  392. if err != nil {
  393. logger.Warning(err)
  394. msg := "❌ Something went wrong!"
  395. t.SendMsgToTgbot(chatId, msg)
  396. return
  397. }
  398. if traffic == nil {
  399. msg := "No result!"
  400. t.SendMsgToTgbot(chatId, msg)
  401. return
  402. }
  403. expiryTime := ""
  404. if traffic.ExpiryTime == 0 {
  405. expiryTime = "♾Unlimited"
  406. } else {
  407. expiryTime = time.Unix((traffic.ExpiryTime / 1000), 0).Format("2006-01-02 15:04:05")
  408. }
  409. total := ""
  410. if traffic.Total == 0 {
  411. total = "♾Unlimited"
  412. } else {
  413. total = common.FormatTraffic((traffic.Total))
  414. }
  415. 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",
  416. traffic.Enable, traffic.Email, common.FormatTraffic(traffic.Up), common.FormatTraffic(traffic.Down), common.FormatTraffic((traffic.Up + traffic.Down)),
  417. total, expiryTime)
  418. t.SendMsgToTgbot(chatId, output)
  419. }
  420. func (t *Tgbot) getExhausted() string {
  421. trDiff := int64(0)
  422. exDiff := int64(0)
  423. now := time.Now().Unix() * 1000
  424. var exhaustedInbounds []model.Inbound
  425. var exhaustedClients []xray.ClientTraffic
  426. var disabledInbounds []model.Inbound
  427. var disabledClients []xray.ClientTraffic
  428. output := ""
  429. TrafficThreshold, err := t.settingService.GetTgTrafficDiff()
  430. if err == nil && TrafficThreshold > 0 {
  431. trDiff = int64(TrafficThreshold) * 1073741824
  432. }
  433. ExpireThreshold, err := t.settingService.GetTgExpireDiff()
  434. if err == nil && ExpireThreshold > 0 {
  435. exDiff = int64(ExpireThreshold) * 84600
  436. }
  437. inbounds, err := t.inboundService.GetAllInbounds()
  438. if err != nil {
  439. logger.Warning("Unable to load Inbounds", err)
  440. }
  441. for _, inbound := range inbounds {
  442. if inbound.Enable {
  443. if (inbound.ExpiryTime > 0 && (now-inbound.ExpiryTime < exDiff)) ||
  444. (inbound.Total > 0 && (inbound.Total-inbound.Up+inbound.Down < trDiff)) {
  445. exhaustedInbounds = append(exhaustedInbounds, *inbound)
  446. }
  447. if len(inbound.ClientStats) > 0 {
  448. for _, client := range inbound.ClientStats {
  449. if client.Enable {
  450. if (client.ExpiryTime > 0 && (now-client.ExpiryTime < exDiff)) ||
  451. (client.Total > 0 && (client.Total-client.Up+client.Down < trDiff)) {
  452. exhaustedClients = append(exhaustedClients, client)
  453. }
  454. } else {
  455. disabledClients = append(disabledClients, client)
  456. }
  457. }
  458. }
  459. } else {
  460. disabledInbounds = append(disabledInbounds, *inbound)
  461. }
  462. }
  463. output += fmt.Sprintf("Exhausted Inbounds count:\r\n🛑 Disabled: %d\r\n🔜 Exhaust soon: %d\r\n \r\n", len(disabledInbounds), len(exhaustedInbounds))
  464. if len(disabledInbounds)+len(exhaustedInbounds) > 0 {
  465. output += "Exhausted Inbounds:\r\n"
  466. for _, inbound := range exhaustedInbounds {
  467. 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))
  468. if inbound.ExpiryTime == 0 {
  469. output += "Expire date: ♾Unlimited\r\n \r\n"
  470. } else {
  471. output += fmt.Sprintf("Expire date:%s\r\n \r\n", time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05"))
  472. }
  473. }
  474. }
  475. output += fmt.Sprintf("Exhausted Clients count:\r\n🛑 Disabled: %d\r\n🔜 Exhaust soon: %d\r\n \r\n", len(disabledClients), len(exhaustedClients))
  476. if len(disabledClients)+len(exhaustedClients) > 0 {
  477. output += "Exhausted Clients:\r\n"
  478. for _, traffic := range exhaustedClients {
  479. expiryTime := ""
  480. if traffic.ExpiryTime == 0 {
  481. expiryTime = "♾Unlimited"
  482. } else {
  483. expiryTime = time.Unix((traffic.ExpiryTime / 1000), 0).Format("2006-01-02 15:04:05")
  484. }
  485. total := ""
  486. if traffic.Total == 0 {
  487. total = "♾Unlimited"
  488. } else {
  489. total = common.FormatTraffic((traffic.Total))
  490. }
  491. 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",
  492. traffic.Enable, traffic.Email, common.FormatTraffic(traffic.Up), common.FormatTraffic(traffic.Down), common.FormatTraffic((traffic.Up + traffic.Down)),
  493. total, expiryTime)
  494. }
  495. }
  496. return output
  497. }
  498. func (t *Tgbot) sendBackup(chatId int64) {
  499. sendingTime := time.Now().Format("2006-01-02 15:04:05")
  500. t.SendMsgToTgbot(chatId, "Backup time: "+sendingTime)
  501. file := tgbotapi.FilePath(config.GetDBPath())
  502. msg := tgbotapi.NewDocument(chatId, file)
  503. _, err := bot.Send(msg)
  504. if err != nil {
  505. logger.Warning("Error in uploading backup: ", err)
  506. }
  507. }