ruspet_portal_back/service/telegram_service/common/update/update.go

251 lines
7.4 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package tgupdatehandler
import (
"context"
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/sirupsen/logrus"
"strings"
)
func (s *UpdateHandler) GetUpdateTelegram() {
bot := s.ipc.TGBotClient.Bot()
u := tgbotapi.NewUpdate(0)
u.Timeout = 10
updates := bot.GetUpdatesChan(u)
logrus.Info("🟢 Telegram Bot запущен. Ожидание обновлений...")
for update := range updates {
if update.Message != nil {
go s.handleMessage(bot, update.Message)
continue
}
if update.CallbackQuery != nil {
go s.handleCallback(bot, update.CallbackQuery)
continue
}
}
}
func (s *UpdateHandler) handleMessage(bot *tgbotapi.BotAPI, msg *tgbotapi.Message) {
ctx := context.Background()
if msg.IsCommand() {
switch msg.Command() {
case "start":
s.startHandler.Handle(bot, msg)
case "approvals":
s.withDepartmentCheck(bot, msg, func() {
s.approvalHandler.HandleMenu(bot, msg.Chat.ID, msg.From.ID)
})
}
return
}
// ── Постоянные кнопки меню ───────────────────────────────────────────────
switch msg.Text {
case "👤 Инициатор":
s.withDepartmentCheck(bot, msg, func() {
s.approvalHandler.HandleInitiatorSubmenu(bot, msg.Chat.ID)
})
return
case "✅ Согласующий":
s.withDepartmentCheck(bot, msg, func() {
ctx := context.Background()
user, err := s.ipc.Database.User.GetUserByTelegramIDDB(ctx, msg.From.ID)
if err != nil || user == nil || user.ID == nil {
return
}
isApprover, _ := s.ipc.Database.User.IsApproverDB(ctx, *user.ID)
isDirector, _ := s.ipc.Database.User.IsDirectorDB(ctx, *user.ID)
if !isApprover && !isDirector {
return
}
s.approvalHandler.HandleApproverSubmenu(bot, msg.Chat.ID)
})
return
case "👁 Наблюдатель":
s.withDepartmentCheck(bot, msg, func() {
ctx := context.Background()
user, err := s.ipc.Database.User.GetUserByTelegramIDDB(ctx, msg.From.ID)
if err != nil || user == nil || user.ID == nil {
return
}
isObserver, _ := s.ipc.Database.User.IsObserverDB(ctx, *user.ID)
if !isObserver {
return
}
s.approvalHandler.HandleObserverSubmenu(bot, msg.Chat.ID)
})
return
case "➕ Создать согласование":
s.withDepartmentCheck(bot, msg, func() {
s.approvalHandler.HandleMenu(bot, msg.Chat.ID, msg.From.ID)
})
return
case "Ранее созданные согласования":
s.withDepartmentCheck(bot, msg, func() {
s.approvalHandler.HandleMyApprovals(bot, msg.Chat.ID, msg.From.ID)
})
return
case "Принять согласование в работу":
s.withDepartmentCheck(bot, msg, func() {
s.approvalHandler.HandleIncomingApprovals(bot, msg.Chat.ID, msg.From.ID)
})
return
case "Отложенные согласования":
s.withDepartmentCheck(bot, msg, func() {
s.approvalHandler.HandlePendingCommentsList(bot, msg.Chat.ID, msg.From.ID)
})
return
case "Список согласований":
s.withDepartmentCheck(bot, msg, func() {
s.approvalHandler.HandleApproverApprovals(bot, msg.Chat.ID, msg.From.ID)
})
return
case "📊 Сформировать отчёт":
s.withDepartmentCheck(bot, msg, func() {
s.approvalHandler.HandleReportMenu(bot, msg.Chat.ID, msg.From.ID)
})
return
case "Делегировать права":
s.withDepartmentCheck(bot, msg, func() {
s.approvalHandler.HandleDelegationMainMenu(bot, msg.Chat.ID, msg.From.ID)
})
return
case "Наблюдать за согласованием":
s.withDepartmentCheck(bot, msg, func() {
s.approvalHandler.HandleObservedApprovals(bot, msg.Chat.ID, msg.From.ID)
})
return
case "Задачи к исполнению":
s.withDepartmentCheck(bot, msg, func() {
s.approvalHandler.HandleMyTasks(bot, msg.Chat.ID, msg.From.ID)
})
return
case "Все", "Согласованные", "Не согласованные", "Ожидают согласования":
statusMap := map[string]string{
"Все": "all", "Согласованные": "approved",
"Не согласованные": "rejected", "Ожидают согласования": "pending",
}
s.approvalHandler.HandleMyApprovalsByStatus(bot, msg.Chat.ID, msg.From.ID, statusMap[msg.Text])
return
case "❓ Как создать согласование?":
s.withDepartmentCheck(bot, msg, func() {
s.faqHandler.Handle(bot, msg)
})
return
case "💬 Предложения и пожелания":
s.withDepartmentCheck(bot, msg, func() {
s.suggestionHandler.HandleCreateSuggestion(bot, msg.Chat.ID, msg.From.ID)
})
return
case "← Назад":
s.withDepartmentCheck(bot, msg, func() {
s.approvalHandler.HandleBackNavigation(bot, msg.Chat.ID, msg.From.ID)
})
return
}
revisionKey := fmt.Sprintf("appr_revision_pending:%d", msg.From.ID)
if val, err := s.ipc.RedisClient.Get(ctx, revisionKey); err == nil && val != "" {
var approvalID uint64
fmt.Sscanf(val, "%d", &approvalID)
s.ipc.RedisClient.Del(ctx, revisionKey) //nolint:errcheck
s.approvalHandler.HandleRevisionWithComment(bot, msg.Chat.ID, msg.From.ID, approvalID, strings.TrimSpace(msg.Text))
return
}
approveCommentKey := fmt.Sprintf("appr_approve_comment_pending:%d", msg.From.ID)
if val, err := s.ipc.RedisClient.Get(ctx, approveCommentKey); err == nil && val != "" {
var approvalID uint64
fmt.Sscanf(val, "%d", &approvalID)
s.ipc.RedisClient.Del(ctx, approveCommentKey) //nolint:errcheck
s.approvalHandler.HandleDecisionWithChat(bot, msg.Chat.ID, msg.From.ID, approvalID, true, strings.TrimSpace(msg.Text))
return
}
rejectKey := fmt.Sprintf("appr_reject_pending:%d", msg.From.ID)
if val, err := s.ipc.RedisClient.Get(ctx, rejectKey); err == nil && val != "" {
var approvalID uint64
fmt.Sscanf(val, "%d", &approvalID)
s.ipc.RedisClient.Del(ctx, rejectKey) //nolint:errcheck
s.approvalHandler.HandleDecisionWithChat(bot, msg.Chat.ID, msg.From.ID, approvalID, false, strings.TrimSpace(msg.Text))
return
}
if s.approvalHandler.HandleRevisionTextInput(bot, msg) {
return
}
if s.approvalHandler.HandleAttachFileInput(bot, msg) {
return
}
if s.approvalHandler.HandleRevisionFileInput(bot, msg) {
return
}
if s.approvalHandler.HandlePendingCommentInput(bot, msg) {
return
}
if s.approvalHandler.HandleReportTextInput(bot, msg) {
return
}
if s.startHandler.HandleTextInput(bot, msg) {
return
}
if s.approvalHandler.HandleCommentReplyInput(bot, msg) {
return
}
if s.approvalHandler.HandleCommentTextInput(bot, msg) {
return
}
if s.suggestionHandler.HandleSuggestionTextInput(bot, msg) {
return
}
if msg.Photo != nil || msg.Document != nil {
s.withDepartmentCheck(bot, msg, func() {
s.approvalHandler.HandleFileInput(bot, msg)
})
return
}
if s.approvalHandler.HandleDelegationDateInput(bot, msg) {
return
}
if s.approvalHandler.HandleDelegationExtendInput(bot, msg) {
return
}
s.withDepartmentCheck(bot, msg, func() {
s.approvalHandler.HandleTextInput(bot, msg)
})
}
// withDepartmentCheck блокирует действие если отдел не назначен.
func (s *UpdateHandler) withDepartmentCheck(bot *tgbotapi.BotAPI, msg *tgbotapi.Message, action func()) {
ctx := context.Background()
user, err := s.ipc.Database.User.GetUserByTelegramIDDB(ctx, msg.From.ID)
if err != nil || user == nil || user.DepartmentID == nil {
return // молчим — бот пуст
}
action()
}