update logic in create tg channel
This commit is contained in:
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()
|
tg.mu.Lock()
|
||||||
defer tg.mu.Unlock()
|
defer tg.mu.Unlock()
|
||||||
|
if len(tg.TgClients) == 0 {
|
||||||
var activeClient *client.Client
|
return "", 0, errors.New("no active Telegram clients")
|
||||||
for _, c := range tg.TgClients {
|
|
||||||
activeClient = c
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
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 {
|
// todo нужно поймать ошибку, при которой либо бан либо медленный редим включается для того чтобы прервать
|
||||||
return "", errors.New("no active Telegram clients")
|
// исполнение клиента текущего аккаунта и дать задачу следующему пока поймал 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{
|
_, err = activeClient.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
|
||||||
UserId: botID,
|
ChatId: channel.Id,
|
||||||
})
|
MemberId: &client.MessageSenderUser{UserId: botID},
|
||||||
if err != nil {
|
Status: &client.ChatMemberStatusAdministrator{
|
||||||
return "", errors.New("not found this bot, make privacy off")
|
CustomTitle: "bot",
|
||||||
}
|
Rights: &client.ChatAdministratorRights{
|
||||||
|
CanManageChat: true,
|
||||||
// todo нужно поймать ошибку, при которой либо бан либо медленный редим включается для того чтобы прервать
|
CanChangeInfo: true,
|
||||||
// исполнение клиента текущего аккаунта и дать задачу следующему пока поймал 1 раз и не запомнил больше не получается
|
CanPostMessages: true,
|
||||||
channel, err := activeClient.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{
|
CanInviteUsers: true,
|
||||||
Title: channelName,
|
CanRestrictMembers: true,
|
||||||
IsChannel: true,
|
CanPromoteMembers: 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,
|
|
||||||
},
|
},
|
||||||
},
|
})
|
||||||
})
|
if err != nil {
|
||||||
if err != nil {
|
lastError = fmt.Errorf("failed to make bot admin: %s", err.Error())
|
||||||
return "", 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{
|
return "", 0, lastError
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user