diff --git a/clients/telegram/tg.go b/clients/telegram/tg.go index 4602ab5..bb40f89 100644 --- a/clients/telegram/tg.go +++ b/clients/telegram/tg.go @@ -21,12 +21,10 @@ type TelegramClient struct { } type WaitingClient struct { - TdLibClient *client.Client PreviousReq AuthTgUserReq Authorizer *client.ClientAuthorizer } -// todo come back saved tg accs to slice for check this status func NewTelegramClient(ctx context.Context, repo *dal.DAL) (*TelegramClient, error) { tgClient := &TelegramClient{ repo: repo, @@ -49,10 +47,10 @@ func NewTelegramClient(ctx context.Context, repo *dal.DAL) (*TelegramClient, err UseTestDc: false, DatabaseDirectory: filepath.Join(".tdlib", "database"), FilesDirectory: filepath.Join(".tdlib", "files"), - UseFileDatabase: false, - UseChatInfoDatabase: false, - UseMessageDatabase: false, - UseSecretChats: false, + UseFileDatabase: true, + UseChatInfoDatabase: true, + UseMessageDatabase: true, + UseSecretChats: true, ApiId: account.ApiID, ApiHash: account.ApiHash, SystemLanguageCode: "en", @@ -147,15 +145,17 @@ func (tg *TelegramClient) GetFromMap(id string) (WaitingClient, bool) { return WaitingClient{}, false } -func (tg *TelegramClient) SaveTgAccount(ctx context.Context, tdLibClient *client.Client, account model.TgAccount) (int64, error) { - tg.mu.Lock() - defer tg.mu.Unlock() - id, err := tg.repo.TgRepo.CreateTgAccount(ctx, account) +func (tg *TelegramClient) SaveTgAccount(appID int32, appHash string, tdLibClient *client.Client) { + account, err := tg.repo.TgRepo.SearchIDByAppIDanAppHash(context.Background(), appID, appHash) if err != nil { - return 0, err + fmt.Println("err SaveTgAccount", err) + return + } + if account.Status == model.ActiveTg { + tg.mu.Lock() + defer tg.mu.Unlock() + tg.TgClients[account.ID] = tdLibClient } - tg.TgClients[id] = tdLibClient - return id, nil } func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string, error) { @@ -190,34 +190,25 @@ func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string //} // //fmt.Println("resp", resp) - // - //_, err = activeClient.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ - // ChatId: channel.Id, - // MemberId: &client.MessageSenderUser{UserId: botID}, - // Status: &client.ChatMemberStatusAdministrator{ - // CanBeEdited: true, - // Rights: &client.ChatAdministratorRights{ - // CanManageChat: false, - // CanChangeInfo: true, - // CanPostMessages: true, - // CanEditMessages: true, - // CanDeleteMessages: true, - // CanInviteUsers: true, - // CanRestrictMembers: true, - // CanPinMessages: true, - // CanManageTopics: true, - // CanPromoteMembers: true, - // CanManageVideoChats: true, - // CanPostStories: true, - // CanEditStories: true, - // CanDeleteStories: true, - // IsAnonymous: true, - // }, - // }, - //}) - //if err != nil { - // return "", fmt.Errorf("failed to make bot admin: %s", err.Error()) - //} + + _, err = activeClient.SetChatMemberStatus(&client.SetChatMemberStatusRequest{ + ChatId: channel.Id, + MemberId: &client.MessageSenderUser{UserId: botID}, + Status: &client.ChatMemberStatusAdministrator{ + CanBeEdited: true, + Rights: &client.ChatAdministratorRights{ + CanManageChat: true, + CanChangeInfo: true, + CanPostMessages: true, + CanInviteUsers: true, + CanRestrictMembers: true, + CanPromoteMembers: true, + }, + }, + }) + if err != nil { + return "", fmt.Errorf("failed to make bot admin: %s", err.Error()) + } //_, err = activeClient.LeaveChat(&client.LeaveChatRequest{ // ChatId: channel.Id, @@ -226,6 +217,8 @@ func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string // return "", fmt.Errorf("failed to leave the channel: %s", err.Error()) //} + fmt.Println("channel.Id", channel.Id) + inviteLink, err := activeClient.CreateChatInviteLink(&client.CreateChatInviteLinkRequest{ ChatId: channel.Id, Name: channelName, diff --git a/go.mod b/go.mod index 37d0420..7cb6321 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ require ( google.golang.org/grpc v1.64.0 google.golang.org/protobuf v1.34.2 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c - penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701131253-42ace400b730 + penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240702152013-c2a0ba2ac307 penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f penahub.gitlab.yandexcloud.net/external/trashlog.git v0.1.2-0.20240615192328-b2f5dffe92ae @@ -34,6 +34,8 @@ require ( github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/go-redis/redis/v8 v8.11.5 // indirect + github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible // indirect + github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect github.com/goccy/go-json v0.10.2 // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/google/uuid v1.6.0 // indirect @@ -52,6 +54,7 @@ require ( github.com/richardlehane/msoleps v1.0.3 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/tealeg/xlsx v1.0.5 // indirect + github.com/technoweenie/multipartstreamer v1.0.1 // indirect github.com/twmb/franz-go/pkg/kmsg v1.8.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.53.0 // indirect diff --git a/go.sum b/go.sum index 5aec6b6..6e2d0ab 100644 --- a/go.sum +++ b/go.sum @@ -36,6 +36,10 @@ github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU= +github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM= +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= +github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/gofiber/fiber/v2 v2.52.4 h1:P+T+4iK7VaqUsq2PALYEfBBo6bJZ4q3FP8cZ84EggTM= @@ -142,6 +146,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE= github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM= +github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM= +github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog= github.com/themakers/bdd v0.0.0-20210316111417-6b1dfe326f33 h1:N9f/Q+2Ssa+yDcbfaoLTYvXmdeyUUxsJKdPUVsjSmiA= github.com/themakers/bdd v0.0.0-20210316111417-6b1dfe326f33/go.mod h1:rpcH99JknBh8seZmlOlUg51gasZH6QH34oXNsIwYT6E= github.com/themakers/hlog v0.0.0-20191205140925-235e0e4baddf h1:TJJm6KcBssmbWzplF5lzixXl1RBAi/ViPs1GaSOkhwo= @@ -287,6 +293,8 @@ penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5 penahub.gitlab.yandexcloud.net/backend/penahub_common v0.0.0-20240607202348-efe5f2bf3e8c/go.mod h1:+bPxq2wfW5S1gd+83vZYmHm33AE7nEBfznWS8AM1TKE= penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701131253-42ace400b730 h1:Macb4cgvNb666E5Ckz3M0QQpJNJ1aWY8lHdTSyt8ZcA= penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240701131253-42ace400b730/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240702152013-c2a0ba2ac307 h1:wCSZWRvg3q1AboSM8TC35429GMYna4U9sKbw9YEJHr0= +penahub.gitlab.yandexcloud.net/backend/quiz/common.git v0.0.0-20240702152013-c2a0ba2ac307/go.mod h1:uOuosXduBzd2WbLH6TDZO7ME7ZextulA662oZ6OsoB0= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990 h1:jiO8GWO+3sCnDAV8/NAV8tQIUwae/I6/xiDilW7zf0o= penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e086fcbb990/go.mod h1:zswBuTwmEsFHBVRu1nkG3/Fwylk5Vcm8OUm9iWxccSE= penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f h1:Qli89wgu0T7nG4VECXZOZ40fjE/hVVfxF3hTaSYS008= diff --git a/service/telegram_svc.go b/service/telegram_svc.go index 0dfad04..ed66035 100644 --- a/service/telegram_svc.go +++ b/service/telegram_svc.go @@ -55,10 +55,10 @@ func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error { UseTestDc: false, DatabaseDirectory: filepath.Join(".tdlib", "database"), FilesDirectory: filepath.Join(".tdlib", "files"), - UseFileDatabase: false, - UseChatInfoDatabase: false, - UseMessageDatabase: false, - UseSecretChats: false, + UseFileDatabase: true, + UseChatInfoDatabase: true, + UseMessageDatabase: true, + UseSecretChats: true, ApiId: req.ApiID, ApiHash: req.ApiHash, SystemLanguageCode: "en", @@ -83,6 +83,7 @@ func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error { fmt.Println("new client failed", err) return } + s.telegramClient.SaveTgAccount(req.ApiID, req.ApiHash, tdlibClient) fmt.Println("i am down") }() if goErr != nil { @@ -101,7 +102,6 @@ func (s *Service) AddingTgAccount(ctx *fiber.Ctx) error { case client.TypeAuthorizationStateWaitCode: signature := xid.New() s.telegramClient.AddedToMap(telegram.WaitingClient{ - TdLibClient: tdlibClient, PreviousReq: req, Authorizer: authorizer, }, signature.String()) @@ -139,7 +139,7 @@ func (s *Service) SettingTgCode(ctx *fiber.Ctx) error { fmt.Println("currnet state:", state) switch state.AuthorizationStateType() { case client.TypeAuthorizationStateReady: - id, err := s.telegramClient.SaveTgAccount(ctx.Context(), data.TdLibClient, model.TgAccount{ + id, err := s.dal.TgRepo.CreateTgAccount(ctx.Context(), model.TgAccount{ ApiID: data.PreviousReq.ApiID, ApiHash: data.PreviousReq.ApiHash, PhoneNumber: data.PreviousReq.PhoneNumber, @@ -171,7 +171,7 @@ func (s *Service) DeleteTgAccountByID(ctx *fiber.Ctx) error { } //func (s *Service) TEST(ctx *fiber.Ctx) error { -// botID := 6712573453 +// botID := 542073142 // chanalName := "testAAAAAA" // link, err := s.telegramClient.CreateChannel(chanalName, int64(botID)) // if err != nil {