1
0

tgbot.go 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861
  1. package service
  2. import (
  3. "embed"
  4. "errors"
  5. "fmt"
  6. "net"
  7. "net/url"
  8. "os"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "x-ui/config"
  13. "x-ui/database"
  14. "x-ui/database/model"
  15. "x-ui/logger"
  16. "x-ui/util/common"
  17. "x-ui/web/global"
  18. "x-ui/web/locale"
  19. "x-ui/xray"
  20. "github.com/mymmrac/telego"
  21. th "github.com/mymmrac/telego/telegohandler"
  22. tu "github.com/mymmrac/telego/telegoutil"
  23. "github.com/valyala/fasthttp"
  24. "github.com/valyala/fasthttp/fasthttpproxy"
  25. )
  26. var (
  27. bot *telego.Bot
  28. botHandler *th.BotHandler
  29. adminIds []int64
  30. isRunning bool
  31. hostname string
  32. hashStorage *global.HashStorage
  33. )
  34. type LoginStatus byte
  35. const (
  36. LoginSuccess LoginStatus = 1
  37. LoginFail LoginStatus = 0
  38. EmptyTelegramUserID = int64(0)
  39. )
  40. type Tgbot struct {
  41. inboundService InboundService
  42. settingService SettingService
  43. serverService ServerService
  44. xrayService XrayService
  45. lastStatus *Status
  46. }
  47. func (t *Tgbot) NewTgbot() *Tgbot {
  48. return new(Tgbot)
  49. }
  50. func (t *Tgbot) I18nBot(name string, params ...string) string {
  51. return locale.I18n(locale.Bot, name, params...)
  52. }
  53. func (t *Tgbot) GetHashStorage() *global.HashStorage {
  54. return hashStorage
  55. }
  56. func (t *Tgbot) Start(i18nFS embed.FS) error {
  57. // Initialize localizer
  58. err := locale.InitLocalizer(i18nFS, &t.settingService)
  59. if err != nil {
  60. return err
  61. }
  62. // Initialize hash storage to store callback queries
  63. hashStorage = global.NewHashStorage(20 * time.Minute)
  64. t.SetHostname()
  65. // Get Telegram bot token
  66. tgBotToken, err := t.settingService.GetTgBotToken()
  67. if err != nil || tgBotToken == "" {
  68. logger.Warning("Failed to get Telegram bot token:", err)
  69. return err
  70. }
  71. // Get Telegram bot chat ID(s)
  72. tgBotID, err := t.settingService.GetTgBotChatId()
  73. if err != nil {
  74. logger.Warning("Failed to get Telegram bot chat ID:", err)
  75. return err
  76. }
  77. // Parse admin IDs from comma-separated string
  78. if tgBotID != "" {
  79. for _, adminID := range strings.Split(tgBotID, ",") {
  80. id, err := strconv.Atoi(adminID)
  81. if err != nil {
  82. logger.Warning("Failed to parse admin ID from Telegram bot chat ID:", err)
  83. return err
  84. }
  85. adminIds = append(adminIds, int64(id))
  86. }
  87. }
  88. // Get Telegram bot proxy URL
  89. tgBotProxy, err := t.settingService.GetTgBotProxy()
  90. if err != nil {
  91. logger.Warning("Failed to get Telegram bot proxy URL:", err)
  92. }
  93. // Get Telegram bot API server URL
  94. tgBotAPIServer, err := t.settingService.GetTgBotAPIServer()
  95. if err != nil {
  96. logger.Warning("Failed to get Telegram bot API server URL:", err)
  97. }
  98. // Create new Telegram bot instance
  99. bot, err = t.NewBot(tgBotToken, tgBotProxy, tgBotAPIServer)
  100. if err != nil {
  101. logger.Error("Failed to initialize Telegram bot API:", err)
  102. return err
  103. }
  104. // Start receiving Telegram bot messages
  105. if !isRunning {
  106. logger.Info("Telegram bot receiver started")
  107. go t.OnReceive()
  108. isRunning = true
  109. }
  110. return nil
  111. }
  112. func (t *Tgbot) NewBot(token string, proxyUrl string, apiServerUrl string) (*telego.Bot, error) {
  113. if proxyUrl == "" && apiServerUrl == "" {
  114. return telego.NewBot(token)
  115. }
  116. if proxyUrl != "" {
  117. if !strings.HasPrefix(proxyUrl, "socks5://") {
  118. logger.Warning("Invalid socks5 URL, using default")
  119. return telego.NewBot(token)
  120. }
  121. _, err := url.Parse(proxyUrl)
  122. if err != nil {
  123. logger.Warningf("Can't parse proxy URL, using default instance for tgbot: %v", err)
  124. return telego.NewBot(token)
  125. }
  126. return telego.NewBot(token, telego.WithFastHTTPClient(&fasthttp.Client{
  127. Dial: fasthttpproxy.FasthttpSocksDialer(proxyUrl),
  128. }))
  129. }
  130. if !strings.HasPrefix(apiServerUrl, "http") {
  131. logger.Warning("Invalid http(s) URL, using default")
  132. return telego.NewBot(token)
  133. }
  134. _, err := url.Parse(apiServerUrl)
  135. if err != nil {
  136. logger.Warningf("Can't parse API server URL, using default instance for tgbot: %v", err)
  137. return telego.NewBot(token)
  138. }
  139. return telego.NewBot(token, telego.WithAPIServer(apiServerUrl))
  140. }
  141. func (t *Tgbot) IsRunning() bool {
  142. return isRunning
  143. }
  144. func (t *Tgbot) SetHostname() {
  145. host, err := os.Hostname()
  146. if err != nil {
  147. logger.Error("get hostname error:", err)
  148. hostname = ""
  149. return
  150. }
  151. hostname = host
  152. }
  153. func (t *Tgbot) Stop() {
  154. botHandler.Stop()
  155. bot.StopLongPolling()
  156. logger.Info("Stop Telegram receiver ...")
  157. isRunning = false
  158. adminIds = nil
  159. }
  160. func (t *Tgbot) encodeQuery(query string) string {
  161. // NOTE: we only need to hash for more than 64 chars
  162. if len(query) <= 64 {
  163. return query
  164. }
  165. return hashStorage.SaveHash(query)
  166. }
  167. func (t *Tgbot) decodeQuery(query string) (string, error) {
  168. if !hashStorage.IsMD5(query) {
  169. return query, nil
  170. }
  171. decoded, exists := hashStorage.GetValue(query)
  172. if !exists {
  173. return "", common.NewError("hash not found in storage!")
  174. }
  175. return decoded, nil
  176. }
  177. func (t *Tgbot) OnReceive() {
  178. params := telego.GetUpdatesParams{
  179. Timeout: 10,
  180. }
  181. updates, _ := bot.UpdatesViaLongPolling(&params)
  182. botHandler, _ = th.NewBotHandler(bot, updates)
  183. botHandler.HandleMessage(func(_ *telego.Bot, message telego.Message) {
  184. t.SendMsgToTgbot(message.Chat.ID, t.I18nBot("tgbot.keyboardClosed"), tu.ReplyKeyboardRemove())
  185. }, th.TextEqual(t.I18nBot("tgbot.buttons.closeKeyboard")))
  186. botHandler.HandleMessage(func(_ *telego.Bot, message telego.Message) {
  187. t.answerCommand(&message, message.Chat.ID, checkAdmin(message.From.ID))
  188. }, th.AnyCommand())
  189. botHandler.HandleCallbackQuery(func(_ *telego.Bot, query telego.CallbackQuery) {
  190. t.answerCallback(&query, checkAdmin(query.From.ID))
  191. }, th.AnyCallbackQueryWithMessage())
  192. botHandler.HandleMessage(func(_ *telego.Bot, message telego.Message) {
  193. if message.UsersShared != nil {
  194. if checkAdmin(message.From.ID) {
  195. for _, sharedUser := range message.UsersShared.Users {
  196. userID := sharedUser.UserID
  197. needRestart, err := t.inboundService.SetClientTelegramUserID(message.UsersShared.RequestID, userID)
  198. if needRestart {
  199. t.xrayService.SetToNeedRestart()
  200. }
  201. output := ""
  202. if err != nil {
  203. output += t.I18nBot("tgbot.messages.selectUserFailed")
  204. } else {
  205. output += t.I18nBot("tgbot.messages.userSaved")
  206. }
  207. t.SendMsgToTgbot(message.Chat.ID, output, tu.ReplyKeyboardRemove())
  208. }
  209. } else {
  210. t.SendMsgToTgbot(message.Chat.ID, t.I18nBot("tgbot.noResult"), tu.ReplyKeyboardRemove())
  211. }
  212. }
  213. }, th.AnyMessage())
  214. botHandler.Start()
  215. }
  216. func (t *Tgbot) answerCommand(message *telego.Message, chatId int64, isAdmin bool) {
  217. msg, onlyMessage := "", false
  218. command, _, commandArgs := tu.ParseCommand(message.Text)
  219. // Helper function to handle unknown commands.
  220. handleUnknownCommand := func() {
  221. msg += t.I18nBot("tgbot.commands.unknown")
  222. }
  223. // Handle the command.
  224. switch command {
  225. case "help":
  226. msg += t.I18nBot("tgbot.commands.help")
  227. msg += t.I18nBot("tgbot.commands.pleaseChoose")
  228. case "start":
  229. msg += t.I18nBot("tgbot.commands.start", "Firstname=="+message.From.FirstName)
  230. if isAdmin {
  231. msg += t.I18nBot("tgbot.commands.welcome", "Hostname=="+hostname)
  232. }
  233. msg += "\n\n" + t.I18nBot("tgbot.commands.pleaseChoose")
  234. case "status":
  235. onlyMessage = true
  236. msg += t.I18nBot("tgbot.commands.status")
  237. case "id":
  238. onlyMessage = true
  239. msg += t.I18nBot("tgbot.commands.getID", "ID=="+strconv.FormatInt(message.From.ID, 10))
  240. case "usage":
  241. onlyMessage = true
  242. if len(commandArgs) > 0 {
  243. if isAdmin {
  244. t.searchClient(chatId, commandArgs[0])
  245. } else {
  246. t.getClientUsage(chatId, int64(message.From.ID), commandArgs[0])
  247. }
  248. } else {
  249. msg += t.I18nBot("tgbot.commands.usage")
  250. }
  251. case "inbound":
  252. onlyMessage = true
  253. if isAdmin && len(commandArgs) > 0 {
  254. t.searchInbound(chatId, commandArgs[0])
  255. } else {
  256. handleUnknownCommand()
  257. }
  258. case "restart":
  259. onlyMessage = true
  260. if isAdmin {
  261. if len(commandArgs) == 0 {
  262. msg += t.I18nBot("tgbot.commands.restartUsage")
  263. } else if strings.ToLower(commandArgs[0]) == "force" {
  264. if t.xrayService.IsXrayRunning() {
  265. err := t.xrayService.RestartXray(true)
  266. if err != nil {
  267. msg += t.I18nBot("tgbot.commands.restartFailed", "Error=="+err.Error())
  268. } else {
  269. msg += t.I18nBot("tgbot.commands.restartSuccess")
  270. }
  271. } else {
  272. msg += t.I18nBot("tgbot.commands.xrayNotRunning")
  273. }
  274. } else {
  275. handleUnknownCommand()
  276. msg += t.I18nBot("tgbot.commands.restartUsage")
  277. }
  278. } else {
  279. handleUnknownCommand()
  280. }
  281. default:
  282. handleUnknownCommand()
  283. }
  284. if msg != "" {
  285. t.sendResponse(chatId, msg, onlyMessage, isAdmin)
  286. }
  287. }
  288. // Helper function to send the message based on onlyMessage flag.
  289. func (t *Tgbot) sendResponse(chatId int64, msg string, onlyMessage, isAdmin bool) {
  290. if onlyMessage {
  291. t.SendMsgToTgbot(chatId, msg)
  292. } else {
  293. t.SendAnswer(chatId, msg, isAdmin)
  294. }
  295. }
  296. func (t *Tgbot) answerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool) {
  297. chatId := callbackQuery.Message.GetChat().ID
  298. if isAdmin {
  299. // get query from hash storage
  300. decodedQuery, err := t.decodeQuery(callbackQuery.Data)
  301. if err != nil {
  302. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.noQuery"))
  303. return
  304. }
  305. dataArray := strings.Split(decodedQuery, " ")
  306. if len(dataArray) >= 2 && len(dataArray[1]) > 0 {
  307. email := dataArray[1]
  308. switch dataArray[0] {
  309. case "client_get_usage":
  310. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.messages.email", "Email=="+email))
  311. t.searchClient(chatId, email)
  312. case "client_refresh":
  313. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.clientRefreshSuccess", "Email=="+email))
  314. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  315. case "client_cancel":
  316. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.canceled", "Email=="+email))
  317. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  318. case "ips_refresh":
  319. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.IpRefreshSuccess", "Email=="+email))
  320. t.searchClientIps(chatId, email, callbackQuery.Message.GetMessageID())
  321. case "ips_cancel":
  322. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.canceled", "Email=="+email))
  323. t.searchClientIps(chatId, email, callbackQuery.Message.GetMessageID())
  324. case "tgid_refresh":
  325. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.TGIdRefreshSuccess", "Email=="+email))
  326. t.clientTelegramUserInfo(chatId, email, callbackQuery.Message.GetMessageID())
  327. case "tgid_cancel":
  328. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.canceled", "Email=="+email))
  329. t.clientTelegramUserInfo(chatId, email, callbackQuery.Message.GetMessageID())
  330. case "reset_traffic":
  331. inlineKeyboard := tu.InlineKeyboard(
  332. tu.InlineKeyboardRow(
  333. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancelReset")).WithCallbackData(t.encodeQuery("client_cancel "+email)),
  334. ),
  335. tu.InlineKeyboardRow(
  336. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmResetTraffic")).WithCallbackData(t.encodeQuery("reset_traffic_c "+email)),
  337. ),
  338. )
  339. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  340. case "reset_traffic_c":
  341. err := t.inboundService.ResetClientTrafficByEmail(email)
  342. if err == nil {
  343. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.resetTrafficSuccess", "Email=="+email))
  344. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  345. } else {
  346. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  347. }
  348. case "limit_traffic":
  349. inlineKeyboard := tu.InlineKeyboard(
  350. tu.InlineKeyboardRow(
  351. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("client_cancel "+email)),
  352. ),
  353. tu.InlineKeyboardRow(
  354. tu.InlineKeyboardButton(t.I18nBot("tgbot.unlimited")).WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 0")),
  355. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.custom")).WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" 0")),
  356. ),
  357. tu.InlineKeyboardRow(
  358. tu.InlineKeyboardButton("1 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 1")),
  359. tu.InlineKeyboardButton("5 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 5")),
  360. tu.InlineKeyboardButton("10 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 10")),
  361. ),
  362. tu.InlineKeyboardRow(
  363. tu.InlineKeyboardButton("20 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 20")),
  364. tu.InlineKeyboardButton("30 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 30")),
  365. tu.InlineKeyboardButton("40 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 40")),
  366. ),
  367. tu.InlineKeyboardRow(
  368. tu.InlineKeyboardButton("50 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 50")),
  369. tu.InlineKeyboardButton("60 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 60")),
  370. tu.InlineKeyboardButton("80 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 80")),
  371. ),
  372. tu.InlineKeyboardRow(
  373. tu.InlineKeyboardButton("100 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 100")),
  374. tu.InlineKeyboardButton("150 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 150")),
  375. tu.InlineKeyboardButton("200 GB").WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" 200")),
  376. ),
  377. )
  378. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  379. case "limit_traffic_c":
  380. if len(dataArray) == 3 {
  381. limitTraffic, err := strconv.Atoi(dataArray[2])
  382. if err == nil {
  383. needRestart, err := t.inboundService.ResetClientTrafficLimitByEmail(email, limitTraffic)
  384. if needRestart {
  385. t.xrayService.SetToNeedRestart()
  386. }
  387. if err == nil {
  388. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.setTrafficLimitSuccess", "Email=="+email))
  389. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  390. return
  391. }
  392. }
  393. }
  394. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  395. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  396. case "limit_traffic_in":
  397. if len(dataArray) >= 3 {
  398. oldInputNumber, err := strconv.Atoi(dataArray[2])
  399. inputNumber := oldInputNumber
  400. if err == nil {
  401. if len(dataArray) == 4 {
  402. num, err := strconv.Atoi(dataArray[3])
  403. if err == nil {
  404. if num == -2 {
  405. inputNumber = 0
  406. } else if num == -1 {
  407. if inputNumber > 0 {
  408. inputNumber = (inputNumber / 10)
  409. }
  410. } else {
  411. inputNumber = (inputNumber * 10) + num
  412. }
  413. }
  414. if inputNumber == oldInputNumber {
  415. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
  416. return
  417. }
  418. if inputNumber >= 999999 {
  419. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  420. return
  421. }
  422. }
  423. inlineKeyboard := tu.InlineKeyboard(
  424. tu.InlineKeyboardRow(
  425. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("client_cancel "+email)),
  426. ),
  427. tu.InlineKeyboardRow(
  428. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmNumberAdd", "Num=="+strconv.Itoa(inputNumber))).WithCallbackData(t.encodeQuery("limit_traffic_c "+email+" "+strconv.Itoa(inputNumber))),
  429. ),
  430. tu.InlineKeyboardRow(
  431. tu.InlineKeyboardButton("1").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 1")),
  432. tu.InlineKeyboardButton("2").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 2")),
  433. tu.InlineKeyboardButton("3").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 3")),
  434. ),
  435. tu.InlineKeyboardRow(
  436. tu.InlineKeyboardButton("4").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 4")),
  437. tu.InlineKeyboardButton("5").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 5")),
  438. tu.InlineKeyboardButton("6").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 6")),
  439. ),
  440. tu.InlineKeyboardRow(
  441. tu.InlineKeyboardButton("7").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 7")),
  442. tu.InlineKeyboardButton("8").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 8")),
  443. tu.InlineKeyboardButton("9").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 9")),
  444. ),
  445. tu.InlineKeyboardRow(
  446. tu.InlineKeyboardButton("🔄").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" -2")),
  447. tu.InlineKeyboardButton("0").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" 0")),
  448. tu.InlineKeyboardButton("⬅️").WithCallbackData(t.encodeQuery("limit_traffic_in "+email+" "+strconv.Itoa(inputNumber)+" -1")),
  449. ),
  450. )
  451. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  452. return
  453. }
  454. }
  455. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  456. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  457. case "reset_exp":
  458. inlineKeyboard := tu.InlineKeyboard(
  459. tu.InlineKeyboardRow(
  460. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancelReset")).WithCallbackData(t.encodeQuery("client_cancel "+email)),
  461. ),
  462. tu.InlineKeyboardRow(
  463. tu.InlineKeyboardButton(t.I18nBot("tgbot.unlimited")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 0")),
  464. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.custom")).WithCallbackData(t.encodeQuery("reset_exp_in "+email+" 0")),
  465. ),
  466. tu.InlineKeyboardRow(
  467. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 7 "+t.I18nBot("tgbot.days")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 7")),
  468. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 10 "+t.I18nBot("tgbot.days")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 10")),
  469. ),
  470. tu.InlineKeyboardRow(
  471. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 14 "+t.I18nBot("tgbot.days")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 14")),
  472. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 20 "+t.I18nBot("tgbot.days")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 20")),
  473. ),
  474. tu.InlineKeyboardRow(
  475. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 1 "+t.I18nBot("tgbot.month")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 30")),
  476. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 3 "+t.I18nBot("tgbot.months")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 90")),
  477. ),
  478. tu.InlineKeyboardRow(
  479. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 6 "+t.I18nBot("tgbot.months")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 180")),
  480. tu.InlineKeyboardButton(t.I18nBot("tgbot.add")+" 12 "+t.I18nBot("tgbot.months")).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" 365")),
  481. ),
  482. )
  483. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  484. case "reset_exp_c":
  485. if len(dataArray) == 3 {
  486. days, err := strconv.Atoi(dataArray[2])
  487. if err == nil {
  488. var date int64 = 0
  489. if days > 0 {
  490. traffic, err := t.inboundService.GetClientTrafficByEmail(email)
  491. if err != nil {
  492. logger.Warning(err)
  493. msg := t.I18nBot("tgbot.wentWrong")
  494. t.SendMsgToTgbot(chatId, msg)
  495. return
  496. }
  497. if traffic == nil {
  498. msg := t.I18nBot("tgbot.noResult")
  499. t.SendMsgToTgbot(chatId, msg)
  500. return
  501. }
  502. if traffic.ExpiryTime > 0 {
  503. if traffic.ExpiryTime-time.Now().Unix()*1000 < 0 {
  504. date = -int64(days * 24 * 60 * 60000)
  505. } else {
  506. date = traffic.ExpiryTime + int64(days*24*60*60000)
  507. }
  508. } else {
  509. date = traffic.ExpiryTime - int64(days*24*60*60000)
  510. }
  511. }
  512. needRestart, err := t.inboundService.ResetClientExpiryTimeByEmail(email, date)
  513. if needRestart {
  514. t.xrayService.SetToNeedRestart()
  515. }
  516. if err == nil {
  517. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.expireResetSuccess", "Email=="+email))
  518. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  519. return
  520. }
  521. }
  522. }
  523. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  524. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  525. case "reset_exp_in":
  526. if len(dataArray) >= 3 {
  527. oldInputNumber, err := strconv.Atoi(dataArray[2])
  528. inputNumber := oldInputNumber
  529. if err == nil {
  530. if len(dataArray) == 4 {
  531. num, err := strconv.Atoi(dataArray[3])
  532. if err == nil {
  533. if num == -2 {
  534. inputNumber = 0
  535. } else if num == -1 {
  536. if inputNumber > 0 {
  537. inputNumber = (inputNumber / 10)
  538. }
  539. } else {
  540. inputNumber = (inputNumber * 10) + num
  541. }
  542. }
  543. if inputNumber == oldInputNumber {
  544. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
  545. return
  546. }
  547. if inputNumber >= 999999 {
  548. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  549. return
  550. }
  551. }
  552. inlineKeyboard := tu.InlineKeyboard(
  553. tu.InlineKeyboardRow(
  554. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("client_cancel "+email)),
  555. ),
  556. tu.InlineKeyboardRow(
  557. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmNumber", "Num=="+strconv.Itoa(inputNumber))).WithCallbackData(t.encodeQuery("reset_exp_c "+email+" "+strconv.Itoa(inputNumber))),
  558. ),
  559. tu.InlineKeyboardRow(
  560. tu.InlineKeyboardButton("1").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 1")),
  561. tu.InlineKeyboardButton("2").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 2")),
  562. tu.InlineKeyboardButton("3").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 3")),
  563. ),
  564. tu.InlineKeyboardRow(
  565. tu.InlineKeyboardButton("4").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 4")),
  566. tu.InlineKeyboardButton("5").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 5")),
  567. tu.InlineKeyboardButton("6").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 6")),
  568. ),
  569. tu.InlineKeyboardRow(
  570. tu.InlineKeyboardButton("7").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 7")),
  571. tu.InlineKeyboardButton("8").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 8")),
  572. tu.InlineKeyboardButton("9").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 9")),
  573. ),
  574. tu.InlineKeyboardRow(
  575. tu.InlineKeyboardButton("🔄").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" -2")),
  576. tu.InlineKeyboardButton("0").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" 0")),
  577. tu.InlineKeyboardButton("⬅️").WithCallbackData(t.encodeQuery("reset_exp_in "+email+" "+strconv.Itoa(inputNumber)+" -1")),
  578. ),
  579. )
  580. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  581. return
  582. }
  583. }
  584. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  585. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  586. case "ip_limit":
  587. inlineKeyboard := tu.InlineKeyboard(
  588. tu.InlineKeyboardRow(
  589. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancelIpLimit")).WithCallbackData(t.encodeQuery("client_cancel "+email)),
  590. ),
  591. tu.InlineKeyboardRow(
  592. tu.InlineKeyboardButton(t.I18nBot("tgbot.unlimited")).WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 0")),
  593. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.custom")).WithCallbackData(t.encodeQuery("ip_limit_in "+email+" 0")),
  594. ),
  595. tu.InlineKeyboardRow(
  596. tu.InlineKeyboardButton("1").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 1")),
  597. tu.InlineKeyboardButton("2").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 2")),
  598. ),
  599. tu.InlineKeyboardRow(
  600. tu.InlineKeyboardButton("3").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 3")),
  601. tu.InlineKeyboardButton("4").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 4")),
  602. ),
  603. tu.InlineKeyboardRow(
  604. tu.InlineKeyboardButton("5").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 5")),
  605. tu.InlineKeyboardButton("6").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 6")),
  606. tu.InlineKeyboardButton("7").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 7")),
  607. ),
  608. tu.InlineKeyboardRow(
  609. tu.InlineKeyboardButton("8").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 8")),
  610. tu.InlineKeyboardButton("9").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 9")),
  611. tu.InlineKeyboardButton("10").WithCallbackData(t.encodeQuery("ip_limit_c "+email+" 10")),
  612. ),
  613. )
  614. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  615. case "ip_limit_c":
  616. if len(dataArray) == 3 {
  617. count, err := strconv.Atoi(dataArray[2])
  618. if err == nil {
  619. needRestart, err := t.inboundService.ResetClientIpLimitByEmail(email, count)
  620. if needRestart {
  621. t.xrayService.SetToNeedRestart()
  622. }
  623. if err == nil {
  624. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.resetIpSuccess", "Email=="+email, "Count=="+strconv.Itoa(count)))
  625. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  626. return
  627. }
  628. }
  629. }
  630. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  631. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  632. case "ip_limit_in":
  633. if len(dataArray) >= 3 {
  634. oldInputNumber, err := strconv.Atoi(dataArray[2])
  635. inputNumber := oldInputNumber
  636. if err == nil {
  637. if len(dataArray) == 4 {
  638. num, err := strconv.Atoi(dataArray[3])
  639. if err == nil {
  640. if num == -2 {
  641. inputNumber = 0
  642. } else if num == -1 {
  643. if inputNumber > 0 {
  644. inputNumber = (inputNumber / 10)
  645. }
  646. } else {
  647. inputNumber = (inputNumber * 10) + num
  648. }
  649. }
  650. if inputNumber == oldInputNumber {
  651. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
  652. return
  653. }
  654. if inputNumber >= 999999 {
  655. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  656. return
  657. }
  658. }
  659. inlineKeyboard := tu.InlineKeyboard(
  660. tu.InlineKeyboardRow(
  661. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("client_cancel "+email)),
  662. ),
  663. tu.InlineKeyboardRow(
  664. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmNumber", "Num=="+strconv.Itoa(inputNumber))).WithCallbackData(t.encodeQuery("ip_limit_c "+email+" "+strconv.Itoa(inputNumber))),
  665. ),
  666. tu.InlineKeyboardRow(
  667. tu.InlineKeyboardButton("1").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 1")),
  668. tu.InlineKeyboardButton("2").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 2")),
  669. tu.InlineKeyboardButton("3").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 3")),
  670. ),
  671. tu.InlineKeyboardRow(
  672. tu.InlineKeyboardButton("4").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 4")),
  673. tu.InlineKeyboardButton("5").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 5")),
  674. tu.InlineKeyboardButton("6").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 6")),
  675. ),
  676. tu.InlineKeyboardRow(
  677. tu.InlineKeyboardButton("7").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 7")),
  678. tu.InlineKeyboardButton("8").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 8")),
  679. tu.InlineKeyboardButton("9").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 9")),
  680. ),
  681. tu.InlineKeyboardRow(
  682. tu.InlineKeyboardButton("🔄").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" -2")),
  683. tu.InlineKeyboardButton("0").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" 0")),
  684. tu.InlineKeyboardButton("⬅️").WithCallbackData(t.encodeQuery("ip_limit_in "+email+" "+strconv.Itoa(inputNumber)+" -1")),
  685. ),
  686. )
  687. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  688. return
  689. }
  690. }
  691. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  692. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  693. case "clear_ips":
  694. inlineKeyboard := tu.InlineKeyboard(
  695. tu.InlineKeyboardRow(
  696. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("ips_cancel "+email)),
  697. ),
  698. tu.InlineKeyboardRow(
  699. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmClearIps")).WithCallbackData(t.encodeQuery("clear_ips_c "+email)),
  700. ),
  701. )
  702. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  703. case "clear_ips_c":
  704. err := t.inboundService.ClearClientIps(email)
  705. if err == nil {
  706. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.clearIpSuccess", "Email=="+email))
  707. t.searchClientIps(chatId, email, callbackQuery.Message.GetMessageID())
  708. } else {
  709. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  710. }
  711. case "ip_log":
  712. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.getIpLog", "Email=="+email))
  713. t.searchClientIps(chatId, email)
  714. case "tg_user":
  715. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.getUserInfo", "Email=="+email))
  716. t.clientTelegramUserInfo(chatId, email)
  717. case "tgid_remove":
  718. inlineKeyboard := tu.InlineKeyboard(
  719. tu.InlineKeyboardRow(
  720. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("tgid_cancel "+email)),
  721. ),
  722. tu.InlineKeyboardRow(
  723. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmRemoveTGUser")).WithCallbackData(t.encodeQuery("tgid_remove_c "+email)),
  724. ),
  725. )
  726. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  727. case "tgid_remove_c":
  728. traffic, err := t.inboundService.GetClientTrafficByEmail(email)
  729. if err != nil || traffic == nil {
  730. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  731. return
  732. }
  733. needRestart, err := t.inboundService.SetClientTelegramUserID(traffic.Id, EmptyTelegramUserID)
  734. if needRestart {
  735. t.xrayService.SetToNeedRestart()
  736. }
  737. if err == nil {
  738. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.removedTGUserSuccess", "Email=="+email))
  739. t.clientTelegramUserInfo(chatId, email, callbackQuery.Message.GetMessageID())
  740. } else {
  741. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  742. }
  743. case "toggle_enable":
  744. inlineKeyboard := tu.InlineKeyboard(
  745. tu.InlineKeyboardRow(
  746. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.cancel")).WithCallbackData(t.encodeQuery("client_cancel "+email)),
  747. ),
  748. tu.InlineKeyboardRow(
  749. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.confirmToggle")).WithCallbackData(t.encodeQuery("toggle_enable_c "+email)),
  750. ),
  751. )
  752. t.editMessageCallbackTgBot(chatId, callbackQuery.Message.GetMessageID(), inlineKeyboard)
  753. case "toggle_enable_c":
  754. enabled, needRestart, err := t.inboundService.ToggleClientEnableByEmail(email)
  755. if needRestart {
  756. t.xrayService.SetToNeedRestart()
  757. }
  758. if err == nil {
  759. if enabled {
  760. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.enableSuccess", "Email=="+email))
  761. } else {
  762. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.disableSuccess", "Email=="+email))
  763. }
  764. t.searchClient(chatId, email, callbackQuery.Message.GetMessageID())
  765. } else {
  766. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.errorOperation"))
  767. }
  768. case "get_clients":
  769. inboundId := dataArray[1]
  770. inboundIdInt, err := strconv.Atoi(inboundId)
  771. if err != nil {
  772. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  773. return
  774. }
  775. inbound, err := t.inboundService.GetInbound(inboundIdInt)
  776. if err != nil {
  777. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  778. return
  779. }
  780. clients, err := t.getInboundClients(inboundIdInt)
  781. if err != nil {
  782. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  783. return
  784. }
  785. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.chooseClient", "Inbound=="+inbound.Remark), clients)
  786. }
  787. return
  788. } else {
  789. switch callbackQuery.Data {
  790. case "get_inbounds":
  791. inbounds, err := t.getInbounds()
  792. if err != nil {
  793. t.sendCallbackAnswerTgBot(callbackQuery.ID, err.Error())
  794. return
  795. }
  796. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.allClients"))
  797. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.chooseInbound"), inbounds)
  798. }
  799. }
  800. }
  801. switch callbackQuery.Data {
  802. case "get_usage":
  803. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.serverUsage"))
  804. t.getServerUsage(chatId)
  805. case "usage_refresh":
  806. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
  807. t.getServerUsage(chatId, callbackQuery.Message.GetMessageID())
  808. case "inbounds":
  809. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.getInbounds"))
  810. t.SendMsgToTgbot(chatId, t.getInboundUsages())
  811. case "deplete_soon":
  812. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.depleteSoon"))
  813. t.getExhausted(chatId)
  814. case "get_backup":
  815. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.dbBackup"))
  816. t.sendBackup(chatId)
  817. case "get_banlogs":
  818. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.getBanLogs"))
  819. t.sendBanLogs(chatId, true)
  820. case "client_traffic":
  821. tgUserID := callbackQuery.From.ID
  822. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.clientUsage"))
  823. t.getClientUsage(chatId, tgUserID)
  824. case "client_commands":
  825. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.commands"))
  826. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.commands.helpClientCommands"))
  827. case "onlines":
  828. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.onlines"))
  829. t.onlineClients(chatId)
  830. case "onlines_refresh":
  831. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.answers.successfulOperation"))
  832. t.onlineClients(chatId, callbackQuery.Message.GetMessageID())
  833. case "commands":
  834. t.sendCallbackAnswerTgBot(callbackQuery.ID, t.I18nBot("tgbot.buttons.commands"))
  835. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.commands.helpAdminCommands"))
  836. }
  837. }
  838. func checkAdmin(tgId int64) bool {
  839. for _, adminId := range adminIds {
  840. if adminId == tgId {
  841. return true
  842. }
  843. }
  844. return false
  845. }
  846. func (t *Tgbot) SendAnswer(chatId int64, msg string, isAdmin bool) {
  847. numericKeyboard := tu.InlineKeyboard(
  848. tu.InlineKeyboardRow(
  849. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.serverUsage")).WithCallbackData(t.encodeQuery("get_usage")),
  850. ),
  851. tu.InlineKeyboardRow(
  852. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.dbBackup")).WithCallbackData(t.encodeQuery("get_backup")),
  853. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.getBanLogs")).WithCallbackData(t.encodeQuery("get_banlogs")),
  854. ),
  855. tu.InlineKeyboardRow(
  856. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.getInbounds")).WithCallbackData(t.encodeQuery("inbounds")),
  857. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.depleteSoon")).WithCallbackData(t.encodeQuery("deplete_soon")),
  858. ),
  859. tu.InlineKeyboardRow(
  860. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.commands")).WithCallbackData(t.encodeQuery("commands")),
  861. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.onlines")).WithCallbackData(t.encodeQuery("onlines")),
  862. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.allClients")).WithCallbackData(t.encodeQuery("get_inbounds")),
  863. ),
  864. // TODOOOOOOOOOOOOOO: Add restart button here.
  865. )
  866. numericKeyboardClient := tu.InlineKeyboard(
  867. tu.InlineKeyboardRow(
  868. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.clientUsage")).WithCallbackData(t.encodeQuery("client_traffic")),
  869. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.commands")).WithCallbackData(t.encodeQuery("client_commands")),
  870. ),
  871. )
  872. var ReplyMarkup telego.ReplyMarkup
  873. if isAdmin {
  874. ReplyMarkup = numericKeyboard
  875. } else {
  876. ReplyMarkup = numericKeyboardClient
  877. }
  878. t.SendMsgToTgbot(chatId, msg, ReplyMarkup)
  879. }
  880. func (t *Tgbot) SendMsgToTgbot(chatId int64, msg string, replyMarkup ...telego.ReplyMarkup) {
  881. if !isRunning {
  882. return
  883. }
  884. if msg == "" {
  885. logger.Info("[tgbot] message is empty!")
  886. return
  887. }
  888. var allMessages []string
  889. limit := 2000
  890. // paging message if it is big
  891. if len(msg) > limit {
  892. messages := strings.Split(msg, "\r\n\r\n")
  893. lastIndex := -1
  894. for _, message := range messages {
  895. if (len(allMessages) == 0) || (len(allMessages[lastIndex])+len(message) > limit) {
  896. allMessages = append(allMessages, message)
  897. lastIndex++
  898. } else {
  899. allMessages[lastIndex] += "\r\n\r\n" + message
  900. }
  901. }
  902. if strings.TrimSpace(allMessages[len(allMessages)-1]) == "" {
  903. allMessages = allMessages[:len(allMessages)-1]
  904. }
  905. } else {
  906. allMessages = append(allMessages, msg)
  907. }
  908. for n, message := range allMessages {
  909. params := telego.SendMessageParams{
  910. ChatID: tu.ID(chatId),
  911. Text: message,
  912. ParseMode: "HTML",
  913. }
  914. // only add replyMarkup to last message
  915. if len(replyMarkup) > 0 && n == (len(allMessages)-1) {
  916. params.ReplyMarkup = replyMarkup[0]
  917. }
  918. _, err := bot.SendMessage(&params)
  919. if err != nil {
  920. logger.Warning("Error sending telegram message :", err)
  921. }
  922. time.Sleep(500 * time.Millisecond)
  923. }
  924. }
  925. func (t *Tgbot) SendMsgToTgbotAdmins(msg string, replyMarkup ...telego.ReplyMarkup) {
  926. if len(replyMarkup) > 0 {
  927. for _, adminId := range adminIds {
  928. t.SendMsgToTgbot(adminId, msg, replyMarkup[0])
  929. }
  930. } else {
  931. for _, adminId := range adminIds {
  932. t.SendMsgToTgbot(adminId, msg)
  933. }
  934. }
  935. }
  936. func (t *Tgbot) SendReport() {
  937. runTime, err := t.settingService.GetTgbotRuntime()
  938. if err == nil && len(runTime) > 0 {
  939. msg := ""
  940. msg += t.I18nBot("tgbot.messages.report", "RunTime=="+runTime)
  941. msg += t.I18nBot("tgbot.messages.datetime", "DateTime=="+time.Now().Format("2006-01-02 15:04:05"))
  942. t.SendMsgToTgbotAdmins(msg)
  943. }
  944. info := t.sendServerUsage()
  945. t.SendMsgToTgbotAdmins(info)
  946. t.sendExhaustedToAdmins()
  947. t.notifyExhausted()
  948. backupEnable, err := t.settingService.GetTgBotBackup()
  949. if err == nil && backupEnable {
  950. t.SendBackupToAdmins()
  951. }
  952. }
  953. func (t *Tgbot) SendBackupToAdmins() {
  954. if !t.IsRunning() {
  955. return
  956. }
  957. for _, adminId := range adminIds {
  958. t.sendBackup(int64(adminId))
  959. }
  960. }
  961. func (t *Tgbot) sendExhaustedToAdmins() {
  962. if !t.IsRunning() {
  963. return
  964. }
  965. for _, adminId := range adminIds {
  966. t.getExhausted(int64(adminId))
  967. }
  968. }
  969. func (t *Tgbot) getServerUsage(chatId int64, messageID ...int) string {
  970. info := t.prepareServerUsageInfo()
  971. keyboard := tu.InlineKeyboard(tu.InlineKeyboardRow(
  972. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.refresh")).WithCallbackData(t.encodeQuery("usage_refresh"))))
  973. if len(messageID) > 0 {
  974. t.editMessageTgBot(chatId, messageID[0], info, keyboard)
  975. } else {
  976. t.SendMsgToTgbot(chatId, info, keyboard)
  977. }
  978. return info
  979. }
  980. // Send server usage without an inline keyboard
  981. func (t *Tgbot) sendServerUsage() string {
  982. info := t.prepareServerUsageInfo()
  983. return info
  984. }
  985. func (t *Tgbot) prepareServerUsageInfo() string {
  986. info, ipv4, ipv6 := "", "", ""
  987. // get latest status of server
  988. t.lastStatus = t.serverService.GetStatus(t.lastStatus)
  989. onlines := p.GetOnlineClients()
  990. info += t.I18nBot("tgbot.messages.hostname", "Hostname=="+hostname)
  991. info += t.I18nBot("tgbot.messages.version", "Version=="+config.GetVersion())
  992. info += t.I18nBot("tgbot.messages.xrayVersion", "XrayVersion=="+fmt.Sprint(t.lastStatus.Xray.Version))
  993. // get ip address
  994. netInterfaces, err := net.Interfaces()
  995. if err != nil {
  996. logger.Error("net.Interfaces failed, err: ", err.Error())
  997. info += t.I18nBot("tgbot.messages.ip", "IP=="+t.I18nBot("tgbot.unknown"))
  998. info += "\r\n"
  999. } else {
  1000. for i := 0; i < len(netInterfaces); i++ {
  1001. if (netInterfaces[i].Flags & net.FlagUp) != 0 {
  1002. addrs, _ := netInterfaces[i].Addrs()
  1003. for _, address := range addrs {
  1004. if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
  1005. if ipnet.IP.To4() != nil {
  1006. ipv4 += ipnet.IP.String() + " "
  1007. } else if ipnet.IP.To16() != nil && !ipnet.IP.IsLinkLocalUnicast() {
  1008. ipv6 += ipnet.IP.String() + " "
  1009. }
  1010. }
  1011. }
  1012. }
  1013. }
  1014. info += t.I18nBot("tgbot.messages.ipv4", "IPv4=="+ipv4)
  1015. info += t.I18nBot("tgbot.messages.ipv6", "IPv6=="+ipv6)
  1016. }
  1017. info += t.I18nBot("tgbot.messages.serverUpTime", "UpTime=="+strconv.FormatUint(t.lastStatus.Uptime/86400, 10), "Unit=="+t.I18nBot("tgbot.days"))
  1018. info += t.I18nBot("tgbot.messages.serverLoad", "Load1=="+strconv.FormatFloat(t.lastStatus.Loads[0], 'f', 2, 64), "Load2=="+strconv.FormatFloat(t.lastStatus.Loads[1], 'f', 2, 64), "Load3=="+strconv.FormatFloat(t.lastStatus.Loads[2], 'f', 2, 64))
  1019. info += t.I18nBot("tgbot.messages.serverMemory", "Current=="+common.FormatTraffic(int64(t.lastStatus.Mem.Current)), "Total=="+common.FormatTraffic(int64(t.lastStatus.Mem.Total)))
  1020. info += t.I18nBot("tgbot.messages.onlinesCount", "Count=="+fmt.Sprint(len(onlines)))
  1021. info += t.I18nBot("tgbot.messages.tcpCount", "Count=="+strconv.Itoa(t.lastStatus.TcpCount))
  1022. info += t.I18nBot("tgbot.messages.udpCount", "Count=="+strconv.Itoa(t.lastStatus.UdpCount))
  1023. info += t.I18nBot("tgbot.messages.traffic", "Total=="+common.FormatTraffic(int64(t.lastStatus.NetTraffic.Sent+t.lastStatus.NetTraffic.Recv)), "Upload=="+common.FormatTraffic(int64(t.lastStatus.NetTraffic.Sent)), "Download=="+common.FormatTraffic(int64(t.lastStatus.NetTraffic.Recv)))
  1024. info += t.I18nBot("tgbot.messages.xrayStatus", "State=="+fmt.Sprint(t.lastStatus.Xray.State))
  1025. return info
  1026. }
  1027. func (t *Tgbot) UserLoginNotify(username string, password string, ip string, time string, status LoginStatus) {
  1028. if !t.IsRunning() {
  1029. return
  1030. }
  1031. if username == "" || ip == "" || time == "" {
  1032. logger.Warning("UserLoginNotify failed, invalid info!")
  1033. return
  1034. }
  1035. loginNotifyEnabled, err := t.settingService.GetTgBotLoginNotify()
  1036. if err != nil || !loginNotifyEnabled {
  1037. return
  1038. }
  1039. msg := ""
  1040. if status == LoginSuccess {
  1041. msg += t.I18nBot("tgbot.messages.loginSuccess")
  1042. msg += t.I18nBot("tgbot.messages.hostname", "Hostname=="+hostname)
  1043. } else if status == LoginFail {
  1044. msg += t.I18nBot("tgbot.messages.loginFailed")
  1045. msg += t.I18nBot("tgbot.messages.hostname", "Hostname=="+hostname)
  1046. msg += t.I18nBot("tgbot.messages.password", "Password=="+password)
  1047. }
  1048. msg += t.I18nBot("tgbot.messages.username", "Username=="+username)
  1049. msg += t.I18nBot("tgbot.messages.ip", "IP=="+ip)
  1050. msg += t.I18nBot("tgbot.messages.time", "Time=="+time)
  1051. t.SendMsgToTgbotAdmins(msg)
  1052. }
  1053. func (t *Tgbot) getInboundUsages() string {
  1054. info := ""
  1055. // get traffic
  1056. inbounds, err := t.inboundService.GetAllInbounds()
  1057. if err != nil {
  1058. logger.Warning("GetAllInbounds run failed:", err)
  1059. info += t.I18nBot("tgbot.answers.getInboundsFailed")
  1060. } else {
  1061. // NOTE:If there no any sessions here,need to notify here
  1062. // TODO:Sub-node push, automatic conversion format
  1063. for _, inbound := range inbounds {
  1064. info += t.I18nBot("tgbot.messages.inbound", "Remark=="+inbound.Remark)
  1065. info += t.I18nBot("tgbot.messages.port", "Port=="+strconv.Itoa(inbound.Port))
  1066. info += t.I18nBot("tgbot.messages.traffic", "Total=="+common.FormatTraffic((inbound.Up+inbound.Down)), "Upload=="+common.FormatTraffic(inbound.Up), "Download=="+common.FormatTraffic(inbound.Down))
  1067. if inbound.ExpiryTime == 0 {
  1068. info += t.I18nBot("tgbot.messages.expire", "Time=="+t.I18nBot("tgbot.unlimited"))
  1069. } else {
  1070. info += t.I18nBot("tgbot.messages.expire", "Time=="+time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05"))
  1071. }
  1072. info += "\r\n"
  1073. }
  1074. }
  1075. return info
  1076. }
  1077. func (t *Tgbot) getInbounds() (*telego.InlineKeyboardMarkup, error) {
  1078. inbounds, err := t.inboundService.GetAllInbounds()
  1079. var buttons []telego.InlineKeyboardButton
  1080. if err != nil {
  1081. logger.Warning("GetAllInbounds run failed:", err)
  1082. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  1083. } else {
  1084. if len(inbounds) > 0 {
  1085. for _, inbound := range inbounds {
  1086. status := "❌"
  1087. if inbound.Enable {
  1088. status = "✅"
  1089. }
  1090. buttons = append(buttons, tu.InlineKeyboardButton(fmt.Sprintf("%v - %v", inbound.Remark, status)).WithCallbackData(t.encodeQuery("get_clients "+strconv.Itoa(inbound.Id))))
  1091. }
  1092. } else {
  1093. logger.Warning("GetAllInbounds run failed:", err)
  1094. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  1095. }
  1096. }
  1097. cols := 0
  1098. if len(buttons) < 6 {
  1099. cols = 3
  1100. } else {
  1101. cols = 2
  1102. }
  1103. keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...))
  1104. return keyboard, nil
  1105. }
  1106. func (t *Tgbot) getInboundClients(id int) (*telego.InlineKeyboardMarkup, error) {
  1107. inbound, err := t.inboundService.GetInbound(id)
  1108. if err != nil {
  1109. logger.Warning("getIboundClients run failed:", err)
  1110. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  1111. }
  1112. clients, err := t.inboundService.GetClients(inbound)
  1113. var buttons []telego.InlineKeyboardButton
  1114. if err != nil {
  1115. logger.Warning("GetInboundClients run failed:", err)
  1116. return nil, errors.New(t.I18nBot("tgbot.answers.getInboundsFailed"))
  1117. } else {
  1118. if len(clients) > 0 {
  1119. for _, client := range clients {
  1120. buttons = append(buttons, tu.InlineKeyboardButton(client.Email).WithCallbackData(t.encodeQuery("client_get_usage "+client.Email)))
  1121. }
  1122. } else {
  1123. return nil, errors.New(t.I18nBot("tgbot.answers.getClientsFailed"))
  1124. }
  1125. }
  1126. cols := 0
  1127. if len(buttons) < 6 {
  1128. cols = 3
  1129. } else {
  1130. cols = 2
  1131. }
  1132. keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...))
  1133. return keyboard, nil
  1134. }
  1135. func (t *Tgbot) clientInfoMsg(
  1136. traffic *xray.ClientTraffic,
  1137. printEnabled bool,
  1138. printOnline bool,
  1139. printActive bool,
  1140. printDate bool,
  1141. printTraffic bool,
  1142. printRefreshed bool,
  1143. ) string {
  1144. now := time.Now().Unix()
  1145. expiryTime := ""
  1146. flag := false
  1147. diff := traffic.ExpiryTime/1000 - now
  1148. if traffic.ExpiryTime == 0 {
  1149. expiryTime = t.I18nBot("tgbot.unlimited")
  1150. } else if diff > 172800 || !traffic.Enable {
  1151. expiryTime = time.Unix((traffic.ExpiryTime / 1000), 0).Format("2006-01-02 15:04:05")
  1152. } else if traffic.ExpiryTime < 0 {
  1153. expiryTime = fmt.Sprintf("%d %s", traffic.ExpiryTime/-86400000, t.I18nBot("tgbot.days"))
  1154. flag = true
  1155. } else {
  1156. expiryTime = fmt.Sprintf("%d %s", diff/3600, t.I18nBot("tgbot.hours"))
  1157. flag = true
  1158. }
  1159. total := ""
  1160. if traffic.Total == 0 {
  1161. total = t.I18nBot("tgbot.unlimited")
  1162. } else {
  1163. total = common.FormatTraffic((traffic.Total))
  1164. }
  1165. enabled := ""
  1166. isEnabled, err := t.inboundService.checkIsEnabledByEmail(traffic.Email)
  1167. if err != nil {
  1168. logger.Warning(err)
  1169. enabled = t.I18nBot("tgbot.wentWrong")
  1170. } else if isEnabled {
  1171. enabled = t.I18nBot("tgbot.messages.yes")
  1172. } else {
  1173. enabled = t.I18nBot("tgbot.messages.no")
  1174. }
  1175. active := ""
  1176. if traffic.Enable {
  1177. active = t.I18nBot("tgbot.messages.yes")
  1178. } else {
  1179. active = t.I18nBot("tgbot.messages.no")
  1180. }
  1181. status := t.I18nBot("tgbot.offline")
  1182. if p.IsRunning() {
  1183. for _, online := range p.GetOnlineClients() {
  1184. if online == traffic.Email {
  1185. status = t.I18nBot("tgbot.online")
  1186. break
  1187. }
  1188. }
  1189. }
  1190. output := ""
  1191. output += t.I18nBot("tgbot.messages.email", "Email=="+traffic.Email)
  1192. if printEnabled {
  1193. output += t.I18nBot("tgbot.messages.enabled", "Enable=="+enabled)
  1194. }
  1195. if printOnline {
  1196. output += t.I18nBot("tgbot.messages.online", "Status=="+status)
  1197. }
  1198. if printActive {
  1199. output += t.I18nBot("tgbot.messages.active", "Enable=="+active)
  1200. }
  1201. if printDate {
  1202. if flag {
  1203. output += t.I18nBot("tgbot.messages.expireIn", "Time=="+expiryTime)
  1204. } else {
  1205. output += t.I18nBot("tgbot.messages.expire", "Time=="+expiryTime)
  1206. }
  1207. }
  1208. if printTraffic {
  1209. output += t.I18nBot("tgbot.messages.upload", "Upload=="+common.FormatTraffic(traffic.Up))
  1210. output += t.I18nBot("tgbot.messages.download", "Download=="+common.FormatTraffic(traffic.Down))
  1211. output += t.I18nBot("tgbot.messages.total", "UpDown=="+common.FormatTraffic((traffic.Up+traffic.Down)), "Total=="+total)
  1212. }
  1213. if printRefreshed {
  1214. output += t.I18nBot("tgbot.messages.refreshedOn", "Time=="+time.Now().Format("2006-01-02 15:04:05"))
  1215. }
  1216. return output
  1217. }
  1218. func (t *Tgbot) getClientUsage(chatId int64, tgUserID int64, email ...string) {
  1219. traffics, err := t.inboundService.GetClientTrafficTgBot(tgUserID)
  1220. if err != nil {
  1221. logger.Warning(err)
  1222. msg := t.I18nBot("tgbot.wentWrong")
  1223. t.SendMsgToTgbot(chatId, msg)
  1224. return
  1225. }
  1226. if len(traffics) == 0 {
  1227. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.answers.askToAddUserId", "TgUserID=="+strconv.FormatInt(tgUserID, 10)))
  1228. return
  1229. }
  1230. output := ""
  1231. if len(traffics) > 0 {
  1232. if len(email) > 0 {
  1233. for _, traffic := range traffics {
  1234. if traffic.Email == email[0] {
  1235. output := t.clientInfoMsg(traffic, true, true, true, true, true, true)
  1236. t.SendMsgToTgbot(chatId, output)
  1237. return
  1238. }
  1239. }
  1240. msg := t.I18nBot("tgbot.noResult")
  1241. t.SendMsgToTgbot(chatId, msg)
  1242. return
  1243. } else {
  1244. for _, traffic := range traffics {
  1245. output += t.clientInfoMsg(traffic, true, true, true, true, true, false)
  1246. output += "\r\n"
  1247. }
  1248. }
  1249. }
  1250. output += t.I18nBot("tgbot.messages.refreshedOn", "Time=="+time.Now().Format("2006-01-02 15:04:05"))
  1251. t.SendMsgToTgbot(chatId, output)
  1252. output = t.I18nBot("tgbot.commands.pleaseChoose")
  1253. t.SendAnswer(chatId, output, false)
  1254. }
  1255. func (t *Tgbot) searchClientIps(chatId int64, email string, messageID ...int) {
  1256. ips, err := t.inboundService.GetInboundClientIps(email)
  1257. if err != nil || len(ips) == 0 {
  1258. ips = t.I18nBot("tgbot.noIpRecord")
  1259. }
  1260. output := ""
  1261. output += t.I18nBot("tgbot.messages.email", "Email=="+email)
  1262. output += t.I18nBot("tgbot.messages.ips", "IPs=="+ips)
  1263. output += t.I18nBot("tgbot.messages.refreshedOn", "Time=="+time.Now().Format("2006-01-02 15:04:05"))
  1264. inlineKeyboard := tu.InlineKeyboard(
  1265. tu.InlineKeyboardRow(
  1266. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.refresh")).WithCallbackData(t.encodeQuery("ips_refresh "+email)),
  1267. ),
  1268. tu.InlineKeyboardRow(
  1269. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.clearIPs")).WithCallbackData(t.encodeQuery("clear_ips "+email)),
  1270. ),
  1271. )
  1272. if len(messageID) > 0 {
  1273. t.editMessageTgBot(chatId, messageID[0], output, inlineKeyboard)
  1274. } else {
  1275. t.SendMsgToTgbot(chatId, output, inlineKeyboard)
  1276. }
  1277. }
  1278. func (t *Tgbot) clientTelegramUserInfo(chatId int64, email string, messageID ...int) {
  1279. traffic, client, err := t.inboundService.GetClientByEmail(email)
  1280. if err != nil {
  1281. logger.Warning(err)
  1282. msg := t.I18nBot("tgbot.wentWrong")
  1283. t.SendMsgToTgbot(chatId, msg)
  1284. return
  1285. }
  1286. if client == nil {
  1287. msg := t.I18nBot("tgbot.noResult")
  1288. t.SendMsgToTgbot(chatId, msg)
  1289. return
  1290. }
  1291. tgId := "None"
  1292. if client.TgID != 0 {
  1293. tgId = strconv.FormatInt(client.TgID, 10)
  1294. }
  1295. output := ""
  1296. output += t.I18nBot("tgbot.messages.email", "Email=="+email)
  1297. output += t.I18nBot("tgbot.messages.TGUser", "TelegramID=="+tgId)
  1298. output += t.I18nBot("tgbot.messages.refreshedOn", "Time=="+time.Now().Format("2006-01-02 15:04:05"))
  1299. inlineKeyboard := tu.InlineKeyboard(
  1300. tu.InlineKeyboardRow(
  1301. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.refresh")).WithCallbackData(t.encodeQuery("tgid_refresh "+email)),
  1302. ),
  1303. tu.InlineKeyboardRow(
  1304. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.removeTGUser")).WithCallbackData(t.encodeQuery("tgid_remove "+email)),
  1305. ),
  1306. )
  1307. if len(messageID) > 0 {
  1308. t.editMessageTgBot(chatId, messageID[0], output, inlineKeyboard)
  1309. } else {
  1310. t.SendMsgToTgbot(chatId, output, inlineKeyboard)
  1311. requestUser := telego.KeyboardButtonRequestUsers{
  1312. RequestID: int32(traffic.Id),
  1313. UserIsBot: new(bool),
  1314. }
  1315. keyboard := tu.Keyboard(
  1316. tu.KeyboardRow(
  1317. tu.KeyboardButton(t.I18nBot("tgbot.buttons.selectTGUser")).WithRequestUsers(&requestUser),
  1318. ),
  1319. tu.KeyboardRow(
  1320. tu.KeyboardButton(t.I18nBot("tgbot.buttons.closeKeyboard")),
  1321. ),
  1322. ).WithIsPersistent().WithResizeKeyboard()
  1323. t.SendMsgToTgbot(chatId, t.I18nBot("tgbot.buttons.selectOneTGUser"), keyboard)
  1324. }
  1325. }
  1326. func (t *Tgbot) searchClient(chatId int64, email string, messageID ...int) {
  1327. traffic, err := t.inboundService.GetClientTrafficByEmail(email)
  1328. if err != nil {
  1329. logger.Warning(err)
  1330. msg := t.I18nBot("tgbot.wentWrong")
  1331. t.SendMsgToTgbot(chatId, msg)
  1332. return
  1333. }
  1334. if traffic == nil {
  1335. msg := t.I18nBot("tgbot.noResult")
  1336. t.SendMsgToTgbot(chatId, msg)
  1337. return
  1338. }
  1339. output := t.clientInfoMsg(traffic, true, true, true, true, true, true)
  1340. inlineKeyboard := tu.InlineKeyboard(
  1341. tu.InlineKeyboardRow(
  1342. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.refresh")).WithCallbackData(t.encodeQuery("client_refresh "+email)),
  1343. ),
  1344. tu.InlineKeyboardRow(
  1345. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.resetTraffic")).WithCallbackData(t.encodeQuery("reset_traffic "+email)),
  1346. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.limitTraffic")).WithCallbackData(t.encodeQuery("limit_traffic "+email)),
  1347. ),
  1348. tu.InlineKeyboardRow(
  1349. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.resetExpire")).WithCallbackData(t.encodeQuery("reset_exp "+email)),
  1350. ),
  1351. tu.InlineKeyboardRow(
  1352. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.ipLog")).WithCallbackData(t.encodeQuery("ip_log "+email)),
  1353. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.ipLimit")).WithCallbackData(t.encodeQuery("ip_limit "+email)),
  1354. ),
  1355. tu.InlineKeyboardRow(
  1356. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.setTGUser")).WithCallbackData(t.encodeQuery("tg_user "+email)),
  1357. ),
  1358. tu.InlineKeyboardRow(
  1359. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.toggle")).WithCallbackData(t.encodeQuery("toggle_enable "+email)),
  1360. ),
  1361. )
  1362. if len(messageID) > 0 {
  1363. t.editMessageTgBot(chatId, messageID[0], output, inlineKeyboard)
  1364. } else {
  1365. t.SendMsgToTgbot(chatId, output, inlineKeyboard)
  1366. }
  1367. }
  1368. func (t *Tgbot) searchInbound(chatId int64, remark string) {
  1369. inbounds, err := t.inboundService.SearchInbounds(remark)
  1370. if err != nil {
  1371. logger.Warning(err)
  1372. msg := t.I18nBot("tgbot.wentWrong")
  1373. t.SendMsgToTgbot(chatId, msg)
  1374. return
  1375. }
  1376. if len(inbounds) == 0 {
  1377. msg := t.I18nBot("tgbot.noInbounds")
  1378. t.SendMsgToTgbot(chatId, msg)
  1379. return
  1380. }
  1381. for _, inbound := range inbounds {
  1382. info := ""
  1383. info += t.I18nBot("tgbot.messages.inbound", "Remark=="+inbound.Remark)
  1384. info += t.I18nBot("tgbot.messages.port", "Port=="+strconv.Itoa(inbound.Port))
  1385. info += t.I18nBot("tgbot.messages.traffic", "Total=="+common.FormatTraffic((inbound.Up+inbound.Down)), "Upload=="+common.FormatTraffic(inbound.Up), "Download=="+common.FormatTraffic(inbound.Down))
  1386. if inbound.ExpiryTime == 0 {
  1387. info += t.I18nBot("tgbot.messages.expire", "Time=="+t.I18nBot("tgbot.unlimited"))
  1388. } else {
  1389. info += t.I18nBot("tgbot.messages.expire", "Time=="+time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05"))
  1390. }
  1391. t.SendMsgToTgbot(chatId, info)
  1392. if len(inbound.ClientStats) > 0 {
  1393. output := ""
  1394. for _, traffic := range inbound.ClientStats {
  1395. output += t.clientInfoMsg(&traffic, true, true, true, true, true, true)
  1396. }
  1397. t.SendMsgToTgbot(chatId, output)
  1398. }
  1399. }
  1400. }
  1401. func (t *Tgbot) getExhausted(chatId int64) {
  1402. trDiff := int64(0)
  1403. exDiff := int64(0)
  1404. now := time.Now().Unix() * 1000
  1405. var exhaustedInbounds []model.Inbound
  1406. var exhaustedClients []xray.ClientTraffic
  1407. var disabledInbounds []model.Inbound
  1408. var disabledClients []xray.ClientTraffic
  1409. TrafficThreshold, err := t.settingService.GetTrafficDiff()
  1410. if err == nil && TrafficThreshold > 0 {
  1411. trDiff = int64(TrafficThreshold) * 1073741824
  1412. }
  1413. ExpireThreshold, err := t.settingService.GetExpireDiff()
  1414. if err == nil && ExpireThreshold > 0 {
  1415. exDiff = int64(ExpireThreshold) * 86400000
  1416. }
  1417. inbounds, err := t.inboundService.GetAllInbounds()
  1418. if err != nil {
  1419. logger.Warning("Unable to load Inbounds", err)
  1420. }
  1421. for _, inbound := range inbounds {
  1422. if inbound.Enable {
  1423. if (inbound.ExpiryTime > 0 && (inbound.ExpiryTime-now < exDiff)) ||
  1424. (inbound.Total > 0 && (inbound.Total-(inbound.Up+inbound.Down) < trDiff)) {
  1425. exhaustedInbounds = append(exhaustedInbounds, *inbound)
  1426. }
  1427. if len(inbound.ClientStats) > 0 {
  1428. for _, client := range inbound.ClientStats {
  1429. if client.Enable {
  1430. if (client.ExpiryTime > 0 && (client.ExpiryTime-now < exDiff)) ||
  1431. (client.Total > 0 && (client.Total-(client.Up+client.Down) < trDiff)) {
  1432. exhaustedClients = append(exhaustedClients, client)
  1433. }
  1434. } else {
  1435. disabledClients = append(disabledClients, client)
  1436. }
  1437. }
  1438. }
  1439. } else {
  1440. disabledInbounds = append(disabledInbounds, *inbound)
  1441. }
  1442. }
  1443. // Inbounds
  1444. output := ""
  1445. output += t.I18nBot("tgbot.messages.exhaustedCount", "Type=="+t.I18nBot("tgbot.inbounds"))
  1446. output += t.I18nBot("tgbot.messages.disabled", "Disabled=="+strconv.Itoa(len(disabledInbounds)))
  1447. output += t.I18nBot("tgbot.messages.depleteSoon", "Deplete=="+strconv.Itoa(len(exhaustedInbounds)))
  1448. if len(exhaustedInbounds) > 0 {
  1449. output += t.I18nBot("tgbot.messages.depleteSoon", "Deplete=="+t.I18nBot("tgbot.inbounds"))
  1450. for _, inbound := range exhaustedInbounds {
  1451. output += t.I18nBot("tgbot.messages.inbound", "Remark=="+inbound.Remark)
  1452. output += t.I18nBot("tgbot.messages.port", "Port=="+strconv.Itoa(inbound.Port))
  1453. output += t.I18nBot("tgbot.messages.traffic", "Total=="+common.FormatTraffic((inbound.Up+inbound.Down)), "Upload=="+common.FormatTraffic(inbound.Up), "Download=="+common.FormatTraffic(inbound.Down))
  1454. if inbound.ExpiryTime == 0 {
  1455. output += t.I18nBot("tgbot.messages.expire", "Time=="+t.I18nBot("tgbot.unlimited"))
  1456. } else {
  1457. output += t.I18nBot("tgbot.messages.expire", "Time=="+time.Unix((inbound.ExpiryTime/1000), 0).Format("2006-01-02 15:04:05"))
  1458. }
  1459. output += "\r\n"
  1460. }
  1461. }
  1462. // Clients
  1463. exhaustedCC := len(exhaustedClients)
  1464. output += t.I18nBot("tgbot.messages.exhaustedCount", "Type=="+t.I18nBot("tgbot.clients"))
  1465. output += t.I18nBot("tgbot.messages.disabled", "Disabled=="+strconv.Itoa(len(disabledClients)))
  1466. output += t.I18nBot("tgbot.messages.depleteSoon", "Deplete=="+strconv.Itoa(exhaustedCC))
  1467. if exhaustedCC > 0 {
  1468. output += t.I18nBot("tgbot.messages.depleteSoon", "Deplete=="+t.I18nBot("tgbot.clients"))
  1469. var buttons []telego.InlineKeyboardButton
  1470. for _, traffic := range exhaustedClients {
  1471. output += t.clientInfoMsg(&traffic, true, false, false, true, true, false)
  1472. output += "\r\n"
  1473. buttons = append(buttons, tu.InlineKeyboardButton(traffic.Email).WithCallbackData(t.encodeQuery("client_get_usage "+traffic.Email)))
  1474. }
  1475. cols := 0
  1476. if exhaustedCC < 11 {
  1477. cols = 1
  1478. } else {
  1479. cols = 2
  1480. }
  1481. output += t.I18nBot("tgbot.messages.refreshedOn", "Time=="+time.Now().Format("2006-01-02 15:04:05"))
  1482. keyboard := tu.InlineKeyboardGrid(tu.InlineKeyboardCols(cols, buttons...))
  1483. t.SendMsgToTgbot(chatId, output, keyboard)
  1484. } else {
  1485. output += t.I18nBot("tgbot.messages.refreshedOn", "Time=="+time.Now().Format("2006-01-02 15:04:05"))
  1486. t.SendMsgToTgbot(chatId, output)
  1487. }
  1488. }
  1489. func (t *Tgbot) notifyExhausted() {
  1490. trDiff := int64(0)
  1491. exDiff := int64(0)
  1492. now := time.Now().Unix() * 1000
  1493. TrafficThreshold, err := t.settingService.GetTrafficDiff()
  1494. if err == nil && TrafficThreshold > 0 {
  1495. trDiff = int64(TrafficThreshold) * 1073741824
  1496. }
  1497. ExpireThreshold, err := t.settingService.GetExpireDiff()
  1498. if err == nil && ExpireThreshold > 0 {
  1499. exDiff = int64(ExpireThreshold) * 86400000
  1500. }
  1501. inbounds, err := t.inboundService.GetAllInbounds()
  1502. if err != nil {
  1503. logger.Warning("Unable to load Inbounds", err)
  1504. }
  1505. var chatIDsDone []int64
  1506. for _, inbound := range inbounds {
  1507. if inbound.Enable {
  1508. if len(inbound.ClientStats) > 0 {
  1509. clients, err := t.inboundService.GetClients(inbound)
  1510. if err == nil {
  1511. for _, client := range clients {
  1512. if client.TgID != 0 {
  1513. chatID := client.TgID
  1514. if !int64Contains(chatIDsDone, chatID) && !checkAdmin(chatID) {
  1515. var disabledClients []xray.ClientTraffic
  1516. var exhaustedClients []xray.ClientTraffic
  1517. traffics, err := t.inboundService.GetClientTrafficTgBot(client.TgID)
  1518. if err == nil && len(traffics) > 0 {
  1519. output := t.I18nBot("tgbot.messages.exhaustedCount", "Type=="+t.I18nBot("tgbot.clients"))
  1520. for _, traffic := range traffics {
  1521. if traffic.Enable {
  1522. if (traffic.ExpiryTime > 0 && (traffic.ExpiryTime-now < exDiff)) ||
  1523. (traffic.Total > 0 && (traffic.Total-(traffic.Up+traffic.Down) < trDiff)) {
  1524. exhaustedClients = append(exhaustedClients, *traffic)
  1525. }
  1526. } else {
  1527. disabledClients = append(disabledClients, *traffic)
  1528. }
  1529. }
  1530. if len(exhaustedClients) > 0 {
  1531. output += t.I18nBot("tgbot.messages.disabled", "Disabled=="+strconv.Itoa(len(disabledClients)))
  1532. if len(disabledClients) > 0 {
  1533. output += t.I18nBot("tgbot.clients") + ":\r\n"
  1534. for _, traffic := range disabledClients {
  1535. output += " " + traffic.Email
  1536. }
  1537. output += "\r\n"
  1538. }
  1539. output += "\r\n"
  1540. output += t.I18nBot("tgbot.messages.depleteSoon", "Deplete=="+strconv.Itoa(len(exhaustedClients)))
  1541. for _, traffic := range exhaustedClients {
  1542. output += t.clientInfoMsg(&traffic, true, false, false, true, true, false)
  1543. output += "\r\n"
  1544. }
  1545. t.SendMsgToTgbot(chatID, output)
  1546. }
  1547. chatIDsDone = append(chatIDsDone, chatID)
  1548. }
  1549. }
  1550. }
  1551. }
  1552. }
  1553. }
  1554. }
  1555. }
  1556. }
  1557. func int64Contains(slice []int64, item int64) bool {
  1558. for _, s := range slice {
  1559. if s == item {
  1560. return true
  1561. }
  1562. }
  1563. return false
  1564. }
  1565. func (t *Tgbot) onlineClients(chatId int64, messageID ...int) {
  1566. if !p.IsRunning() {
  1567. return
  1568. }
  1569. onlines := p.GetOnlineClients()
  1570. onlinesCount := len(onlines)
  1571. output := t.I18nBot("tgbot.messages.onlinesCount", "Count=="+fmt.Sprint(onlinesCount))
  1572. keyboard := tu.InlineKeyboard(tu.InlineKeyboardRow(
  1573. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.refresh")).WithCallbackData(t.encodeQuery("onlines_refresh"))))
  1574. if onlinesCount > 0 {
  1575. var buttons []telego.InlineKeyboardButton
  1576. for _, online := range onlines {
  1577. buttons = append(buttons, tu.InlineKeyboardButton(online).WithCallbackData(t.encodeQuery("client_get_usage "+online)))
  1578. }
  1579. cols := 0
  1580. if onlinesCount < 21 {
  1581. cols = 2
  1582. } else if onlinesCount < 61 {
  1583. cols = 3
  1584. } else {
  1585. cols = 4
  1586. }
  1587. keyboard.InlineKeyboard = append(keyboard.InlineKeyboard, tu.InlineKeyboardCols(cols, buttons...)...)
  1588. }
  1589. if len(messageID) > 0 {
  1590. t.editMessageTgBot(chatId, messageID[0], output, keyboard)
  1591. } else {
  1592. t.SendMsgToTgbot(chatId, output, keyboard)
  1593. }
  1594. }
  1595. func (t *Tgbot) sendBackup(chatId int64) {
  1596. output := t.I18nBot("tgbot.messages.backupTime", "Time=="+time.Now().Format("2006-01-02 15:04:05"))
  1597. t.SendMsgToTgbot(chatId, output)
  1598. // Update by manually trigger a checkpoint operation
  1599. err := database.Checkpoint()
  1600. if err != nil {
  1601. logger.Error("Error in trigger a checkpoint operation: ", err)
  1602. }
  1603. file, err := os.Open(config.GetDBPath())
  1604. if err == nil {
  1605. document := tu.Document(
  1606. tu.ID(chatId),
  1607. tu.File(file),
  1608. )
  1609. _, err = bot.SendDocument(document)
  1610. if err != nil {
  1611. logger.Error("Error in uploading backup: ", err)
  1612. }
  1613. } else {
  1614. logger.Error("Error in opening db file for backup: ", err)
  1615. }
  1616. file, err = os.Open(xray.GetConfigPath())
  1617. if err == nil {
  1618. document := tu.Document(
  1619. tu.ID(chatId),
  1620. tu.File(file),
  1621. )
  1622. _, err = bot.SendDocument(document)
  1623. if err != nil {
  1624. logger.Error("Error in uploading config.json: ", err)
  1625. }
  1626. } else {
  1627. logger.Error("Error in opening config.json file for backup: ", err)
  1628. }
  1629. }
  1630. func (t *Tgbot) sendBanLogs(chatId int64, dt bool) {
  1631. if dt {
  1632. output := t.I18nBot("tgbot.messages.datetime", "DateTime=="+time.Now().Format("2006-01-02 15:04:05"))
  1633. t.SendMsgToTgbot(chatId, output)
  1634. }
  1635. file, err := os.Open(xray.GetIPLimitBannedPrevLogPath())
  1636. if err == nil {
  1637. // Check if the file is non-empty before attempting to upload
  1638. fileInfo, _ := file.Stat()
  1639. if fileInfo.Size() > 0 {
  1640. document := tu.Document(
  1641. tu.ID(chatId),
  1642. tu.File(file),
  1643. )
  1644. _, err = bot.SendDocument(document)
  1645. if err != nil {
  1646. logger.Error("Error in uploading IPLimitBannedPrevLog: ", err)
  1647. }
  1648. } else {
  1649. logger.Warning("IPLimitBannedPrevLog file is empty, not uploading.")
  1650. }
  1651. file.Close()
  1652. } else {
  1653. logger.Error("Error in opening IPLimitBannedPrevLog file for backup: ", err)
  1654. }
  1655. file, err = os.Open(xray.GetIPLimitBannedLogPath())
  1656. if err == nil {
  1657. // Check if the file is non-empty before attempting to upload
  1658. fileInfo, _ := file.Stat()
  1659. if fileInfo.Size() > 0 {
  1660. document := tu.Document(
  1661. tu.ID(chatId),
  1662. tu.File(file),
  1663. )
  1664. _, err = bot.SendDocument(document)
  1665. if err != nil {
  1666. logger.Error("Error in uploading IPLimitBannedLog: ", err)
  1667. }
  1668. } else {
  1669. logger.Warning("IPLimitBannedLog file is empty, not uploading.")
  1670. }
  1671. file.Close()
  1672. } else {
  1673. logger.Error("Error in opening IPLimitBannedLog file for backup: ", err)
  1674. }
  1675. }
  1676. func (t *Tgbot) sendCallbackAnswerTgBot(id string, message string) {
  1677. params := telego.AnswerCallbackQueryParams{
  1678. CallbackQueryID: id,
  1679. Text: message,
  1680. }
  1681. if err := bot.AnswerCallbackQuery(&params); err != nil {
  1682. logger.Warning(err)
  1683. }
  1684. }
  1685. func (t *Tgbot) editMessageCallbackTgBot(chatId int64, messageID int, inlineKeyboard *telego.InlineKeyboardMarkup) {
  1686. params := telego.EditMessageReplyMarkupParams{
  1687. ChatID: tu.ID(chatId),
  1688. MessageID: messageID,
  1689. ReplyMarkup: inlineKeyboard,
  1690. }
  1691. if _, err := bot.EditMessageReplyMarkup(&params); err != nil {
  1692. logger.Warning(err)
  1693. }
  1694. }
  1695. func (t *Tgbot) editMessageTgBot(chatId int64, messageID int, text string, inlineKeyboard ...*telego.InlineKeyboardMarkup) {
  1696. params := telego.EditMessageTextParams{
  1697. ChatID: tu.ID(chatId),
  1698. MessageID: messageID,
  1699. Text: text,
  1700. ParseMode: "HTML",
  1701. }
  1702. if len(inlineKeyboard) > 0 {
  1703. params.ReplyMarkup = inlineKeyboard[0]
  1704. }
  1705. if _, err := bot.EditMessageText(&params); err != nil {
  1706. logger.Warning(err)
  1707. }
  1708. }