tgbot.go 66 KB

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