Skip to content

Commit

Permalink
[tgbot] Ability to remove telegram user from the client in the bot
Browse files Browse the repository at this point in the history
  • Loading branch information
masoud-hidden committed May 14, 2023
1 parent bcdac5a commit 4cf7f75
Showing 1 changed file with 60 additions and 14 deletions.
74 changes: 60 additions & 14 deletions web/service/tgbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,23 @@ func (t *Tgbot) asnwerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool
email := dataArray[1]
switch dataArray[0] {
case "client_refresh":
t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("✅ %s : Client Refreshed successfully.", email))
t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("✅ %s : Client refreshed successfully.", email))
t.searchClient(chatId, email, callbackQuery.Message.MessageID)
case "client_cancel":
t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("❌ %s : Operation canceled.", email))
t.searchClient(chatId, email, callbackQuery.Message.MessageID)
case "ips_refresh":
t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("✅ %s : IPs Refreshed successfully.", email))
t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("✅ %s : IPs refreshed successfully.", email))
t.searchClientIps(chatId, email, callbackQuery.Message.MessageID)
case "ips_cancel":
t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("❌ %s : Operation canceled.", email))
t.searchClientIps(chatId, email, callbackQuery.Message.MessageID)
case "tgid_refresh":
t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("✅ %s : Client's Telegram User refreshed successfully.", email))
t.clientTelegramUserInfo(chatId, email, callbackQuery.Message.MessageID)
case "tgid_cancel":
t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("❌ %s : Operation canceled.", email))
t.clientTelegramUserInfo(chatId, email, callbackQuery.Message.MessageID)
case "reset_traffic":
inlineKeyboard := tu.InlineKeyboard(
tu.InlineKeyboardRow(
Expand Down Expand Up @@ -325,6 +331,29 @@ func (t *Tgbot) asnwerCallback(callbackQuery *telego.CallbackQuery, isAdmin bool
case "tg_user":
t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("✅ %s : Get Telegram User Info.", email))
t.clientTelegramUserInfo(chatId, email)
case "tgid_remove":
inlineKeyboard := tu.InlineKeyboard(
tu.InlineKeyboardRow(
tu.InlineKeyboardButton("❌ Cancel").WithCallbackData("tgid_cancel "+email),
),
tu.InlineKeyboardRow(
tu.InlineKeyboardButton("✅ Confirm Remove Telegram User?").WithCallbackData("tgid_remove_c "+email),
),
)
t.editMessageCallbackTgBot(chatId, callbackQuery.Message.MessageID, inlineKeyboard)
case "tgid_remove_c":
traffic, err := t.inboundService.GetClientTrafficByEmail(email)
if err != nil || traffic == nil {
t.sendCallbackAnswerTgBot(callbackQuery.ID, "❗ Error in Operation.")
return
}
err = t.inboundService.SetClientTelegramUserID(traffic.Id, "")
if err == nil {
t.sendCallbackAnswerTgBot(callbackQuery.ID, fmt.Sprintf("✅ %s : Telegram User removed successfully.", email))
t.clientTelegramUserInfo(chatId, email, callbackQuery.Message.MessageID)
} else {
t.sendCallbackAnswerTgBot(callbackQuery.ID, "❗ Error in Operation.")
}
case "toggle_enable":
enabled, err := t.inboundService.ToggleClientEnableByEmail(email)
if err == nil {
Expand Down Expand Up @@ -637,7 +666,7 @@ func (t *Tgbot) searchClientIps(chatId int64, email string, messageID ...int) {
}
}

func (t *Tgbot) clientTelegramUserInfo(chatId int64, email string) {
func (t *Tgbot) clientTelegramUserInfo(chatId int64, email string, messageID ...int) {
traffic, client, err := t.inboundService.GetClientByEmail(email)
if err != nil {
logger.Warning(err)
Expand All @@ -650,20 +679,37 @@ func (t *Tgbot) clientTelegramUserInfo(chatId int64, email string) {
t.SendMsgToTgbot(chatId, msg)
return
}
output := fmt.Sprintf("📧 Email: %s\r\n👤 Telegram User: %s\r\n", email, client.TgID)
requestUser := telego.KeyboardButtonRequestUser{
RequestID: int32(traffic.Id),
UserIsBot: false,
tdId := "None"
if len(client.TgID) > 0 {
tdId = client.TgID
}
keyboard := tu.Keyboard(
tu.KeyboardRow(
tu.KeyboardButton("👤 Select Telegram User").WithRequestUser(&requestUser),
output := fmt.Sprintf("📧 Email: %s\r\n👤 Telegram User: %s\r\n", email, tdId)
inlineKeyboard := tu.InlineKeyboard(
tu.InlineKeyboardRow(
tu.InlineKeyboardButton("🔄 Refresh").WithCallbackData("tgid_refresh "+email),
),
tu.KeyboardRow(
tu.KeyboardButton("❌ Close Keyboard"),
tu.InlineKeyboardRow(
tu.InlineKeyboardButton("❌ Remove Telegram User").WithCallbackData("tgid_remove "+email),
),
).WithIsPersistent()
t.SendMsgToTgbot(chatId, output, keyboard)
)
if len(messageID) > 0 {
t.editMessageTgBot(chatId, messageID[0], output, inlineKeyboard)
} else {
t.SendMsgToTgbot(chatId, output, inlineKeyboard)
requestUser := telego.KeyboardButtonRequestUser{
RequestID: int32(traffic.Id),
UserIsBot: false,
}
keyboard := tu.Keyboard(
tu.KeyboardRow(
tu.KeyboardButton("👤 Select Telegram User").WithRequestUser(&requestUser),
),
tu.KeyboardRow(
tu.KeyboardButton("❌ Close Keyboard"),
),
).WithIsPersistent()
t.SendMsgToTgbot(chatId, "👤 Select a telegram user:", keyboard)
}
}

func (t *Tgbot) searchClient(chatId int64, email string, messageID ...int) {
Expand Down

0 comments on commit 4cf7f75

Please sign in to comment.