tgbot_send.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package tgbot
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  8. "github.com/mymmrac/telego"
  9. tu "github.com/mymmrac/telego/telegoutil"
  10. )
  11. // sendResponse sends the response message based on the onlyMessage flag.
  12. func (t *Tgbot) sendResponse(chatId int64, msg string, onlyMessage, isAdmin bool) {
  13. if onlyMessage {
  14. t.SendMsgToTgbot(chatId, msg)
  15. } else {
  16. t.SendAnswer(chatId, msg, isAdmin)
  17. }
  18. }
  19. // SendAnswer sends a response message with an inline keyboard to the specified chat.
  20. func (t *Tgbot) SendAnswer(chatId int64, msg string, isAdmin bool) {
  21. numericKeyboard := tu.InlineKeyboard(
  22. tu.InlineKeyboardRow(
  23. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.SortedTrafficUsageReport")).WithCallbackData(t.encodeQuery("get_sorted_traffic_usage_report")),
  24. ),
  25. tu.InlineKeyboardRow(
  26. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.serverUsage")).WithCallbackData(t.encodeQuery("get_usage")),
  27. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.ResetAllTraffics")).WithCallbackData(t.encodeQuery("reset_all_traffics")),
  28. ),
  29. tu.InlineKeyboardRow(
  30. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.dbBackup")).WithCallbackData(t.encodeQuery("get_backup")),
  31. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.getBanLogs")).WithCallbackData(t.encodeQuery("get_banlogs")),
  32. ),
  33. tu.InlineKeyboardRow(
  34. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.getInbounds")).WithCallbackData(t.encodeQuery("inbounds")),
  35. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.depleteSoon")).WithCallbackData(t.encodeQuery("deplete_soon")),
  36. ),
  37. tu.InlineKeyboardRow(
  38. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.commands")).WithCallbackData(t.encodeQuery("commands")),
  39. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.onlines")).WithCallbackData(t.encodeQuery("onlines")),
  40. ),
  41. tu.InlineKeyboardRow(
  42. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.allClients")).WithCallbackData(t.encodeQuery("get_inbounds")),
  43. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.addClient")).WithCallbackData(t.encodeQuery("add_client")),
  44. ),
  45. tu.InlineKeyboardRow(
  46. tu.InlineKeyboardButton(t.I18nBot("pages.settings.subSettings")).WithCallbackData(t.encodeQuery("admin_client_sub_links")),
  47. tu.InlineKeyboardButton(t.I18nBot("subscription.individualLinks")).WithCallbackData(t.encodeQuery("admin_client_individual_links")),
  48. tu.InlineKeyboardButton(t.I18nBot("qrCode")).WithCallbackData(t.encodeQuery("admin_client_qr_links")),
  49. ),
  50. // TODOOOOOOOOOOOOOO: Add restart button here.
  51. )
  52. numericKeyboardClient := tu.InlineKeyboard(
  53. tu.InlineKeyboardRow(
  54. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.clientUsage")).WithCallbackData(t.encodeQuery("client_traffic")),
  55. tu.InlineKeyboardButton(t.I18nBot("tgbot.buttons.commands")).WithCallbackData(t.encodeQuery("client_commands")),
  56. ),
  57. tu.InlineKeyboardRow(
  58. tu.InlineKeyboardButton(t.I18nBot("pages.settings.subSettings")).WithCallbackData(t.encodeQuery("client_sub_links")),
  59. tu.InlineKeyboardButton(t.I18nBot("subscription.individualLinks")).WithCallbackData(t.encodeQuery("client_individual_links")),
  60. ),
  61. tu.InlineKeyboardRow(
  62. tu.InlineKeyboardButton(t.I18nBot("qrCode")).WithCallbackData(t.encodeQuery("client_qr_links")),
  63. ),
  64. )
  65. var ReplyMarkup telego.ReplyMarkup
  66. if isAdmin {
  67. ReplyMarkup = numericKeyboard
  68. } else {
  69. ReplyMarkup = numericKeyboardClient
  70. }
  71. t.SendMsgToTgbot(chatId, msg, ReplyMarkup)
  72. }
  73. // SendMsgToTgbot sends a message to the Telegram bot with optional reply markup.
  74. func (t *Tgbot) SendMsgToTgbot(chatId int64, msg string, replyMarkup ...telego.ReplyMarkup) {
  75. if !isRunning {
  76. return
  77. }
  78. if msg == "" {
  79. logger.Info("[tgbot] message is empty!")
  80. return
  81. }
  82. var allMessages []string
  83. limit := 2000
  84. // paging message if it is big
  85. if len(msg) > limit {
  86. messages := strings.Split(msg, "\r\n\r\n")
  87. lastIndex := -1
  88. for _, message := range messages {
  89. if (len(allMessages) == 0) || (len(allMessages[lastIndex])+len(message) > limit) {
  90. allMessages = append(allMessages, message)
  91. lastIndex++
  92. } else {
  93. allMessages[lastIndex] += "\r\n\r\n" + message
  94. }
  95. }
  96. if strings.TrimSpace(allMessages[len(allMessages)-1]) == "" {
  97. allMessages = allMessages[:len(allMessages)-1]
  98. }
  99. } else {
  100. allMessages = append(allMessages, msg)
  101. }
  102. for n, message := range allMessages {
  103. params := telego.SendMessageParams{
  104. ChatID: tu.ID(chatId),
  105. Text: message,
  106. ParseMode: "HTML",
  107. }
  108. // only add replyMarkup to last message
  109. if len(replyMarkup) > 0 && n == (len(allMessages)-1) {
  110. params.ReplyMarkup = replyMarkup[0]
  111. }
  112. // Retry logic with exponential backoff for connection errors
  113. maxRetries := 3
  114. for attempt := range maxRetries {
  115. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  116. _, err := bot.SendMessage(ctx, &params)
  117. cancel()
  118. if err == nil {
  119. break // Success
  120. }
  121. // Check if error is a connection error
  122. errStr := err.Error()
  123. isConnectionError := strings.Contains(errStr, "connection") ||
  124. strings.Contains(errStr, "timeout") ||
  125. strings.Contains(errStr, "closed")
  126. if isConnectionError && attempt < maxRetries-1 {
  127. // Exponential backoff: 1s, 2s, 4s
  128. backoff := time.Duration(1<<uint(attempt)) * time.Second
  129. logger.Warningf("Connection error sending telegram message (attempt %d/%d), retrying in %v: %v",
  130. attempt+1, maxRetries, backoff, err)
  131. time.Sleep(backoff)
  132. } else {
  133. logger.Warning("Error sending telegram message:", err)
  134. break
  135. }
  136. }
  137. // Reduced delay to improve performance (only needed for rate limiting)
  138. if n < len(allMessages)-1 { // Only delay between messages, not after the last one
  139. time.Sleep(100 * time.Millisecond)
  140. }
  141. }
  142. }
  143. // SendMsgToTgbotAdmins sends a message to all admin Telegram chats.
  144. func (t *Tgbot) SendMsgToTgbotAdmins(msg string, replyMarkup ...telego.ReplyMarkup) {
  145. if len(replyMarkup) > 0 {
  146. for _, adminId := range adminIds {
  147. t.SendMsgToTgbot(adminId, msg, replyMarkup[0])
  148. }
  149. } else {
  150. for _, adminId := range adminIds {
  151. t.SendMsgToTgbot(adminId, msg)
  152. }
  153. }
  154. }
  155. // sendCallbackAnswerTgBot answers a callback query with a message.
  156. func (t *Tgbot) sendCallbackAnswerTgBot(id string, message string) {
  157. params := telego.AnswerCallbackQueryParams{
  158. CallbackQueryID: id,
  159. Text: message,
  160. }
  161. if err := bot.AnswerCallbackQuery(context.Background(), &params); err != nil {
  162. logger.Warning(err)
  163. }
  164. }
  165. // editMessageCallbackTgBot edits the reply markup of a message.
  166. func (t *Tgbot) editMessageCallbackTgBot(chatId int64, messageID int, inlineKeyboard *telego.InlineKeyboardMarkup) {
  167. params := telego.EditMessageReplyMarkupParams{
  168. ChatID: tu.ID(chatId),
  169. MessageID: messageID,
  170. ReplyMarkup: inlineKeyboard,
  171. }
  172. if _, err := bot.EditMessageReplyMarkup(context.Background(), &params); err != nil {
  173. logger.Warning(err)
  174. }
  175. }
  176. // editMessageTgBot edits the text and reply markup of a message.
  177. func (t *Tgbot) editMessageTgBot(chatId int64, messageID int, text string, inlineKeyboard ...*telego.InlineKeyboardMarkup) {
  178. params := telego.EditMessageTextParams{
  179. ChatID: tu.ID(chatId),
  180. MessageID: messageID,
  181. Text: text,
  182. ParseMode: "HTML",
  183. }
  184. if len(inlineKeyboard) > 0 {
  185. params.ReplyMarkup = inlineKeyboard[0]
  186. }
  187. if _, err := bot.EditMessageText(context.Background(), &params); err != nil {
  188. logger.Warning(err)
  189. }
  190. }
  191. // SendMsgToTgbotDeleteAfter sends a message and deletes it after a specified delay.
  192. func (t *Tgbot) SendMsgToTgbotDeleteAfter(chatId int64, msg string, delayInSeconds int, replyMarkup ...telego.ReplyMarkup) {
  193. // Determine if replyMarkup was passed; otherwise, set it to nil
  194. var replyMarkupParam telego.ReplyMarkup
  195. if len(replyMarkup) > 0 {
  196. replyMarkupParam = replyMarkup[0] // Use the first element
  197. }
  198. // Send the message
  199. sentMsg, err := bot.SendMessage(context.Background(), &telego.SendMessageParams{
  200. ChatID: tu.ID(chatId),
  201. Text: msg,
  202. ReplyMarkup: replyMarkupParam, // Use the correct replyMarkup value
  203. })
  204. if err != nil {
  205. logger.Warning("Failed to send message:", err)
  206. return
  207. }
  208. // Delete the sent message after the specified number of seconds
  209. go func() {
  210. time.Sleep(time.Duration(delayInSeconds) * time.Second) // Wait for the specified delay
  211. t.deleteMessageTgBot(chatId, sentMsg.MessageID) // Delete the message
  212. delete(userStates, chatId)
  213. }()
  214. }
  215. // deleteMessageTgBot deletes a message from the chat.
  216. func (t *Tgbot) deleteMessageTgBot(chatId int64, messageID int) {
  217. params := telego.DeleteMessageParams{
  218. ChatID: tu.ID(chatId),
  219. MessageID: messageID,
  220. }
  221. if err := bot.DeleteMessage(context.Background(), &params); err != nil {
  222. logger.Warning("Failed to delete message:", err)
  223. } else {
  224. logger.Info("Message deleted successfully")
  225. }
  226. }
  227. // TestConnection verifies the bot token is valid and the API is reachable.
  228. func (t *Tgbot) TestConnection() error {
  229. tgBotMutex.Lock()
  230. b := bot
  231. tgBotMutex.Unlock()
  232. if b == nil {
  233. return fmt.Errorf("bot not initialized")
  234. }
  235. me, err := b.GetMe(context.Background())
  236. if err != nil {
  237. return fmt.Errorf("API unreachable: %w", err)
  238. }
  239. _ = me
  240. return nil
  241. }