added logic for make user admin who sent messages need rework with event
This commit is contained in:
parent
7e2b4b2656
commit
fa24ff4de6
@ -155,17 +155,18 @@ func New(ctx context.Context, opts interface{}, ver appInit.Version) (appInit.Co
|
||||
privilegeController := privilege.NewPrivilege(clientData, fiberClient)
|
||||
go tools.PublishPrivilege(privilegeController, 10, 5*time.Minute)
|
||||
|
||||
chatIDsChan := make(chan int64, 100)
|
||||
tgClient, err := telegram.NewTelegramClient(ctx, pgdal, chatIDsChan)
|
||||
tgClient, err := telegram.NewTelegramClient(ctx, pgdal)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("failed init tg clietns: %v", err))
|
||||
}
|
||||
|
||||
_, err = worker.NewTelegramBotWorker(options.TgBotToken, chatIDsChan)
|
||||
tgWC, err := worker.NewTelegramBotWorker(options.TgBotToken)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("failed init tg listener WC: %v", err))
|
||||
}
|
||||
|
||||
go tgWC.Run()
|
||||
|
||||
// todo подумать над реализацией всего а то пока мне кажется что немного каша получается такой предикт что через некоторое время
|
||||
// сложно будет разобраться что есть где
|
||||
grpcControllers := initialize.InitRpcControllers(pgdal)
|
||||
|
||||
@ -18,7 +18,6 @@ type TelegramClient struct {
|
||||
TgClients map[int64]*client.Client
|
||||
WaitingClients map[string]WaitingClient
|
||||
mu sync.Mutex
|
||||
chatIDsChan chan int64
|
||||
}
|
||||
|
||||
type WaitingClient struct {
|
||||
@ -26,12 +25,11 @@ type WaitingClient struct {
|
||||
Authorizer *client.ClientAuthorizer
|
||||
}
|
||||
|
||||
func NewTelegramClient(ctx context.Context, repo *dal.DAL, chatIDsChan chan int64) (*TelegramClient, error) {
|
||||
func NewTelegramClient(ctx context.Context, repo *dal.DAL) (*TelegramClient, error) {
|
||||
tgClient := &TelegramClient{
|
||||
repo: repo,
|
||||
TgClients: make(map[int64]*client.Client),
|
||||
WaitingClients: make(map[string]WaitingClient),
|
||||
chatIDsChan: chatIDsChan,
|
||||
}
|
||||
|
||||
allTgAccounts, err := repo.TgRepo.GetAllTgAccounts(ctx)
|
||||
@ -196,12 +194,20 @@ func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string
|
||||
Status: &client.ChatMemberStatusAdministrator{
|
||||
CustomTitle: "bot",
|
||||
Rights: &client.ChatAdministratorRights{
|
||||
CanManageChat: true,
|
||||
CanChangeInfo: true,
|
||||
CanPostMessages: true,
|
||||
CanInviteUsers: true,
|
||||
CanRestrictMembers: true,
|
||||
CanPromoteMembers: true,
|
||||
CanManageChat: true,
|
||||
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,
|
||||
},
|
||||
},
|
||||
})
|
||||
@ -232,7 +238,6 @@ func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string
|
||||
|
||||
inviteLink = inviteLinkResp.InviteLink
|
||||
channelId = channel.Id
|
||||
tg.chatIDsChan <- channelId
|
||||
return inviteLink, channelId, nil
|
||||
}
|
||||
|
||||
|
||||
4
go.mod
4
go.mod
@ -23,6 +23,7 @@ require (
|
||||
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 v0.1.2-0.20240709135426-e17a9ce9ba2c
|
||||
)
|
||||
|
||||
require (
|
||||
@ -34,6 +35,7 @@ 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/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/mscfb v1.0.4 // indirect
|
||||
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/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
|
||||
@ -68,5 +71,4 @@ require (
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
penahub.gitlab.yandexcloud.net/external/trashlog v0.1.2-0.20240704185641-304b297f19d8 // indirect
|
||||
)
|
||||
|
||||
13
go.sum
13
go.sum
@ -26,6 +26,8 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF
|
||||
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
|
||||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
|
||||
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
|
||||
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
|
||||
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
|
||||
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
|
||||
@ -34,6 +36,8 @@ 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/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=
|
||||
@ -139,6 +143,10 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
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/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=
|
||||
github.com/themakers/hlog v0.0.0-20191205140925-235e0e4baddf/go.mod h1:1FsorU3vnXO9xS9SrhUp8fRb/6H/Zfll0rPt1i4GWaA=
|
||||
github.com/twmb/franz-go v1.16.1 h1:rpWc7fB9jd7TgmCyfxzenBI+QbgS8ZfJOUQE+tzPtbE=
|
||||
@ -257,6 +265,7 @@ google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6h
|
||||
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||
@ -285,5 +294,5 @@ penahub.gitlab.yandexcloud.net/backend/quiz/worker.git v0.0.0-20240421230341-0e0
|
||||
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=
|
||||
penahub.gitlab.yandexcloud.net/backend/tdlib v0.0.0-20240701075856-1731684c936f/go.mod h1:AkE19hcbDwB7hoEASwImm7rUI+cK/8jMVJaTvMK4F+c=
|
||||
penahub.gitlab.yandexcloud.net/external/trashlog v0.1.2-0.20240704185641-304b297f19d8 h1:9RiVgJk+/0vNvzObrpM3Cjga0zz6PtNsJXj1babTk4Q=
|
||||
penahub.gitlab.yandexcloud.net/external/trashlog v0.1.2-0.20240704185641-304b297f19d8/go.mod h1:6NMjnrYqjRc2YVzQxNvs5Q2BfQR6x40bb3oUVdV61XI=
|
||||
penahub.gitlab.yandexcloud.net/external/trashlog v0.1.2-0.20240709135426-e17a9ce9ba2c h1:ZMOaDmy+OZkShN1u2x+qigtpSUi2b9V6I4n/10qBqqM=
|
||||
penahub.gitlab.yandexcloud.net/external/trashlog v0.1.2-0.20240709135426-e17a9ce9ba2c/go.mod h1:6NMjnrYqjRc2YVzQxNvs5Q2BfQR6x40bb3oUVdV61XI=
|
||||
|
||||
@ -7,61 +7,32 @@ import (
|
||||
)
|
||||
|
||||
type TelegramBotWorker struct {
|
||||
bot *telebot.Bot
|
||||
channelIDs chan int64
|
||||
bot *telebot.Bot
|
||||
}
|
||||
|
||||
func NewTelegramBotWorker(botToken string, channelIDs chan int64) (*TelegramBotWorker, error) {
|
||||
botSettings := telebot.Settings{
|
||||
func NewTelegramBotWorker(botToken string) (*TelegramBotWorker, error) {
|
||||
bot, err := telebot.NewBot(telebot.Settings{
|
||||
Token: botToken,
|
||||
Poller: &telebot.LongPoller{Timeout: 10 * time.Second},
|
||||
}
|
||||
|
||||
bot, err := telebot.NewBot(botSettings)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
worker := &TelegramBotWorker{
|
||||
bot: bot,
|
||||
channelIDs: channelIDs,
|
||||
bot: bot,
|
||||
}
|
||||
|
||||
return worker, nil
|
||||
}
|
||||
|
||||
// todo в канал писать нельзя надо что то придумать
|
||||
func (w *TelegramBotWorker) Run() {
|
||||
w.bot.Handle(telebot.OnUserJoined, func(message telebot.Message) {
|
||||
w.handleNewChatMembers(&message)
|
||||
})
|
||||
|
||||
w.bot.Start()
|
||||
}
|
||||
|
||||
func (w *TelegramBotWorker) handleNewChatMembers(message *telebot.Message) {
|
||||
chatID := message.Chat.ID
|
||||
var found bool
|
||||
|
||||
for {
|
||||
select {
|
||||
case channelID := <-w.channelIDs:
|
||||
if chatID == channelID {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
if found {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if found {
|
||||
for _, newMember := range message.UsersJoined {
|
||||
w.bot.Handle(telebot.OnChannelPost, func(m *telebot.Message) {
|
||||
if m.Text == "/admin" {
|
||||
fmt.Println(m.Sender.ID)
|
||||
adminRights := telebot.ChatMember{
|
||||
User: &telebot.User{ID: newMember.ID},
|
||||
User: &telebot.User{ID: m.Sender.ID},
|
||||
Role: telebot.Administrator,
|
||||
Rights: telebot.Rights{
|
||||
CanManageChat: true,
|
||||
@ -73,13 +44,13 @@ func (w *TelegramBotWorker) handleNewChatMembers(message *telebot.Message) {
|
||||
},
|
||||
}
|
||||
|
||||
err := w.bot.Promote(message.Chat, &adminRights)
|
||||
err := w.bot.Promote(m.Chat, &adminRights)
|
||||
if err != nil {
|
||||
fmt.Printf("error to promote user %d in chat %d: %v", newMember.ID, chatID, err)
|
||||
fmt.Printf("error to promote user %d in chat %d: %v", m.Sender.ID, m.Chat.ID, err)
|
||||
} else {
|
||||
fmt.Printf("user %d has been promoted to admin in chat %d", newMember.ID, chatID)
|
||||
fmt.Printf("user %d has been promoted to admin in chat %d", m.Sender.ID, m.Chat.ID)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
w.bot.Start()
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user