create channel, adding bot to channel, get invite link and leave owner tg channel success

This commit is contained in:
Pavel 2024-07-03 14:04:29 +03:00
parent 2d1456d40b
commit f3414541c6
2 changed files with 16 additions and 21 deletions

@ -172,6 +172,13 @@ func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string
return "", errors.New("no active Telegram clients")
}
_, err := activeClient.GetUser(&client.GetUserRequest{
UserId: botID,
})
if err != nil {
return "", errors.New("not found this bot, make privacy off")
}
channel, err := activeClient.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{
Title: channelName,
IsChannel: true,
@ -181,21 +188,11 @@ func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string
return "", fmt.Errorf("failed to create channel: %s", err.Error())
}
//resp, err := activeClient.AddChatMember(&client.AddChatMemberRequest{
// ChatId: channel.Id,
// UserId: botID,
//})
//if err != nil {
// return "", fmt.Errorf("failed to add bot to channel: %s", err.Error())
//}
//
//fmt.Println("resp", resp)
_, err = activeClient.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
ChatId: channel.Id,
MemberId: &client.MessageSenderUser{UserId: botID},
Status: &client.ChatMemberStatusAdministrator{
CanBeEdited: true,
CustomTitle: "bot",
Rights: &client.ChatAdministratorRights{
CanManageChat: true,
CanChangeInfo: true,
@ -210,15 +207,6 @@ func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string
return "", fmt.Errorf("failed to make bot admin: %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())
//}
fmt.Println("channel.Id", channel.Id)
inviteLink, err := activeClient.CreateChatInviteLink(&client.CreateChatInviteLinkRequest{
ChatId: channel.Id,
Name: channelName,
@ -230,5 +218,12 @@ func (tg *TelegramClient) CreateChannel(channelName string, botID int64) (string
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
}

@ -171,7 +171,7 @@ func (s *Service) DeleteTgAccountByID(ctx *fiber.Ctx) error {
}
//func (s *Service) TEST(ctx *fiber.Ctx) error {
// botID := 542073142
// botID := 6712573453
// chanalName := "testAAAAAA"
// link, err := s.telegramClient.CreateChannel(chanalName, int64(botID))
// if err != nil {