@@ -135,3 +135,114 @@ func (j *StatsNotifyJob) UserLoginNotify(username string, ip string, time string
135135 msg += fmt .Sprintf ("IP:%s\r \n " , ip )
136136 j .SendMsgToTgbot (msg )
137137}
138+
139+
140+ var numericKeyboard = tgbotapi .NewInlineKeyboardMarkup (
141+ tgbotapi .NewInlineKeyboardRow (
142+ tgbotapi .NewInlineKeyboardButtonData ("Get Usage" , "get_usage" ),
143+ ),
144+ )
145+
146+ func (j * StatsNotifyJob ) OnReceive () * StatsNotifyJob {
147+ tgBottoken , err := j .settingService .GetTgBotToken ()
148+ if err != nil || tgBottoken == "" {
149+ logger .Warning ("sendMsgToTgbot failed,GetTgBotToken fail:" , err )
150+ return j
151+ }
152+ bot , err := tgbotapi .NewBotAPI (tgBottoken )
153+ if err != nil {
154+ fmt .Println ("get tgbot error:" , err )
155+ return j
156+ }
157+ bot .Debug = false
158+ u := tgbotapi .NewUpdate (0 )
159+ u .Timeout = 10
160+
161+ updates := bot .GetUpdatesChan (u )
162+
163+ for update := range updates {
164+ if update .Message == nil {
165+
166+ if update .CallbackQuery != nil {
167+ // Respond to the callback query, telling Telegram to show the user
168+ // a message with the data received.
169+ callback := tgbotapi .NewCallback (update .CallbackQuery .ID , update .CallbackQuery .Data )
170+ if _ , err := bot .Request (callback ); err != nil {
171+ logger .Warning (err )
172+ }
173+
174+ // And finally, send a message containing the data received.
175+ msg := tgbotapi .NewMessage (update .CallbackQuery .Message .Chat .ID , "" )
176+
177+ switch update .CallbackQuery .Data {
178+ case "get_usage" :
179+ msg .Text = "for get your usage send command like this : \n <code>/usage uuid | id</code> \n example : <code>/usage fc3239ed-8f3b-4151-ff51-b183d5182142</code>"
180+ msg .ParseMode = "HTML"
181+ }
182+ if _ , err := bot .Send (msg ); err != nil {
183+ logger .Warning (err )
184+ }
185+ }
186+
187+ continue
188+ }
189+
190+ if ! update .Message .IsCommand () { // ignore any non-command Messages
191+ continue
192+ }
193+
194+ // Create a new MessageConfig. We don't have text yet,
195+ // so we leave it empty.
196+ msg := tgbotapi .NewMessage (update .Message .Chat .ID , "" )
197+
198+ // Extract the command from the Message.
199+ switch update .Message .Command () {
200+ case "help" :
201+ msg .Text = "What you need?"
202+ msg .ReplyMarkup = numericKeyboard
203+ case "start" :
204+ msg .Text = "Hi :) \n What you need?"
205+ msg .ReplyMarkup = numericKeyboard
206+
207+ case "status" :
208+ msg .Text = "bot is ok."
209+
210+ case "usage" :
211+ msg .Text = j .getClientUsage (update .Message .CommandArguments ())
212+ default :
213+ msg .Text = "I don't know that command, /help"
214+ msg .ReplyMarkup = numericKeyboard
215+
216+ }
217+
218+ if _ , err := bot .Send (msg ); err != nil {
219+ logger .Warning (err )
220+ }
221+ }
222+ return j
223+
224+ }
225+ func (j * StatsNotifyJob ) getClientUsage (id string ) string {
226+ traffic , err := j .inboundService .GetClientTrafficById (id )
227+ if err != nil {
228+ logger .Warning (err )
229+ return "something wrong!"
230+ }
231+ expiryTime := ""
232+ if traffic .ExpiryTime == 0 {
233+ expiryTime = fmt .Sprintf ("unlimited" )
234+ } else {
235+ expiryTime = fmt .Sprintf ("%s" , time .Unix ((traffic .ExpiryTime / 1000 ), 0 ).Format ("2006-01-02 15:04:05" ))
236+ }
237+ total := ""
238+ if traffic .Total == 0 {
239+ total = fmt .Sprintf ("unlimited" )
240+ } else {
241+ total = fmt .Sprintf ("%s" , common .FormatTraffic ((traffic .Total )))
242+ }
243+ output := fmt .Sprintf ("💡 Active: %t\r \n 📧 Email: %s\r \n 🔼 Upload↑: %s\r \n 🔽 Download↓: %s\r \n 🔄 Total: %s / %s\r \n 📅 Expire in: %s\r \n " ,
244+ traffic .Enable , traffic .Email , common .FormatTraffic (traffic .Up ), common .FormatTraffic (traffic .Down ), common .FormatTraffic ((traffic .Up + traffic .Down )),
245+ total , expiryTime )
246+
247+ return output
248+ }
0 commit comments