update logic in create tg channel

This commit is contained in:
Pavel 2024-07-05 10:49:42 +03:00
parent 9b178d2eb9
commit f1cbc8bfa2

@ -158,74 +158,80 @@ func (tg *TelegramClient) SaveTgAccount(appID int32, appHash string, tdLibClient
}
}
func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string, error) {
func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string, int64, error) {
tg.mu.Lock()
defer tg.mu.Unlock()
var activeClient *client.Client
for _, c := range tg.TgClients {
activeClient = c
break
if len(tg.TgClients) == 0 {
return "", 0, errors.New("no active Telegram clients")
}
var lastError error
var inviteLink string
var channelId int64
for _, activeClient := range tg.TgClients {
_, err := activeClient.GetUser(&client.GetUserRequest{
UserId: botID,
})
if err != nil {
lastError = fmt.Errorf("not found this bot, make privacy off: %v", err)
continue
}
if activeClient == nil {
return "", errors.New("no active Telegram clients")
}
// todo нужно поймать ошибку, при которой либо бан либо медленный редим включается для того чтобы прервать
// исполнение клиента текущего аккаунта и дать задачу следующему пока поймал 1 раз и не запомнил больше не получается
channel, err := activeClient.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{
Title: channelName,
IsChannel: true,
Description: "private channel",
})
if err != nil {
lastError = fmt.Errorf("failed to create channel: %s", err.Error())
continue
}
_, err := activeClient.GetUser(&client.GetUserRequest{
UserId: botID,
})
if err != nil {
return "", errors.New("not found this bot, make privacy off")
}
// todo нужно поймать ошибку, при которой либо бан либо медленный редим включается для того чтобы прервать
// исполнение клиента текущего аккаунта и дать задачу следующему пока поймал 1 раз и не запомнил больше не получается
channel, err := activeClient.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{
Title: channelName,
IsChannel: true,
Description: "private channel",
})
if err != nil {
return "", fmt.Errorf("failed to create channel: %s", err.Error())
}
_, err = activeClient.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
ChatId: channel.Id,
MemberId: &client.MessageSenderUser{UserId: botID},
Status: &client.ChatMemberStatusAdministrator{
CustomTitle: "bot",
Rights: &client.ChatAdministratorRights{
CanManageChat: true,
CanChangeInfo: true,
CanPostMessages: true,
CanInviteUsers: true,
CanRestrictMembers: true,
CanPromoteMembers: true,
_, err = activeClient.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
ChatId: channel.Id,
MemberId: &client.MessageSenderUser{UserId: botID},
Status: &client.ChatMemberStatusAdministrator{
CustomTitle: "bot",
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())
})
if err != nil {
lastError = fmt.Errorf("failed to make bot admin: %s", err.Error())
continue
}
inviteLinkResp, err := activeClient.CreateChatInviteLink(&client.CreateChatInviteLinkRequest{
ChatId: channel.Id,
Name: channelName,
ExpirationDate: 0,
MemberLimit: 0,
CreatesJoinRequest: false,
})
if err != nil {
lastError = fmt.Errorf("failed to get invite link: %s", err.Error())
continue
}
_, err = activeClient.LeaveChat(&client.LeaveChatRequest{
ChatId: channel.Id,
})
if err != nil {
lastError = fmt.Errorf("failed to leave the channel: %s", err.Error())
continue
}
inviteLink = inviteLinkResp.InviteLink
channelId = channel.Id
return inviteLink, channelId, nil
}
inviteLink, err := activeClient.CreateChatInviteLink(&client.CreateChatInviteLinkRequest{
ChatId: channel.Id,
Name: channelName,
ExpirationDate: 0,
MemberLimit: 0,
CreatesJoinRequest: false,
})
if err != nil {
return "", fmt.Errorf("failed to get invite link: %s", err.Error())
}
_, err = activeClient.LeaveChat(&client.LeaveChatRequest{
ChatId: channel.Id,
})
if err != nil {
return "", fmt.Errorf("failed to leave the channel: %s", err.Error())
}
return inviteLink.InviteLink, nil
return "", 0, lastError
}