Skip to content

Commit cbd74b2

Browse files
feat(userbot): auto add bots to channel
1 parent 9c48430 commit cbd74b2

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ An example of `fsb.env` file:
158158
API_ID=452525
159159
API_HASH=esx576f8738x883f3sfzx83
160160
BOT_TOKEN=55838383:yourbottokenhere
161-
BIN_CHANNEL=-10045145224562
161+
LOG_CHANNEL=-10045145224562
162162
PORT=8080
163163
HOST=http://yourserverip
164164
# (if you want to set up multiple bots)
@@ -175,7 +175,7 @@ Before running the bot, you will need to set up the following mandatory variable
175175

176176
- `BOT_TOKEN` : This is the bot token for the Telegram Media Streamer Bot, which can be obtained from [@BotFather](https://telegram.dog/BotFather).
177177

178-
- `BIN_CHANNEL` : This is the channel ID for the log channel where the bot will forward media messages and store these files to make the generated direct links work. To obtain a channel ID, create a new telegram channel (public or private), post something in the channel, forward the message to [@missrose_bot](https://telegram.dog/MissRose_bot) and **reply the forwarded message** with the /id command. Copy the forwarded channel ID and paste it into the this field.
178+
- `LOG_CHANNEL` : This is the channel ID for the log channel where the bot will forward media messages and store these files to make the generated direct links work. To obtain a channel ID, create a new telegram channel (public or private), post something in the channel, forward the message to [@missrose_bot](https://telegram.dog/MissRose_bot) and **reply the forwarded message** with the /id command. Copy the forwarded channel ID and paste it into the this field.
179179

180180
### Optional Vars
181181
In addition to the mandatory variables, you can also set the following optional variables:
@@ -188,7 +188,7 @@ In addition to the mandatory variables, you can also set the following optional
188188

189189
- `USE_SESSION_FILE` : Use session files for worker client(s). This speeds up the worker bot startups. (default: `false`)
190190

191-
- `USER_SESSION` : A pyrogram session string for the user bot. (default: `null`)
191+
- `USER_SESSION` : A pyrogram session string for a user bot. Used for auto adding the bots to `LOG_CHANNEL`.(default: `null`)
192192

193193
<hr>
194194

@@ -211,7 +211,7 @@ you may also add as many as bots you want. (max limit is 50)
211211
`MULTI_TOKEN3`, `MULTI_TOKEN4`, etc.
212212

213213
> **Warning**
214-
> Don't forget to add all these worker bots to the `BIN_CHANNEL` for the proper functioning
214+
> Don't forget to add all these worker bots to the `LOG_CHANNEL` for the proper functioning
215215
216216
## Contributing
217217

internal/bot/userbot.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package bot
22

33
import (
44
"EverythingSuckz/fsb/config"
5+
"errors"
56

67
"github.com/celestix/gotgproto"
78
"github.com/celestix/gotgproto/sessionMaker"
9+
"github.com/gotd/td/tg"
810
"go.uber.org/zap"
911
)
1012

@@ -40,5 +42,75 @@ func StartUserBot(l *zap.Logger) {
4042
UserBot.log = log
4143
UserBot.client = client
4244
log.Info("Userbot started", zap.String("username", client.Self.Username), zap.String("FirstName", client.Self.FirstName), zap.String("LastName", client.Self.LastName))
45+
if err := UserBot.AddBotsAsAdmins(); err != nil {
46+
log.Error("Failed to add bots as admins", zap.Error(err))
47+
return
48+
}
49+
}
4350

51+
func (u *UserBotStruct) AddBotsAsAdmins() error {
52+
u.log.Info("Preparing to add bots as admins")
53+
ctx := u.client.CreateContext()
54+
channel := config.ValueOf.LogChannelID
55+
channelInfos, err := u.client.API().ChannelsGetChannels(
56+
ctx,
57+
[]tg.InputChannelClass{
58+
&tg.InputChannel{
59+
ChannelID: channel,
60+
},
61+
},
62+
)
63+
if err != nil {
64+
u.log.Error("Failed to get channel info", zap.Error(err))
65+
return errors.New("failed to get channel info")
66+
}
67+
if len(channelInfos.GetChats()) == 0 {
68+
return errors.New("no channels found")
69+
}
70+
inputChannel := channelInfos.GetChats()[0].(*tg.Channel).AsInput()
71+
currentAdmins := []int64{}
72+
admins, err := u.client.API().ChannelsGetParticipants(ctx, &tg.ChannelsGetParticipantsRequest{
73+
Channel: inputChannel,
74+
Filter: &tg.ChannelParticipantsAdmins{},
75+
Offset: 0,
76+
Limit: 100,
77+
})
78+
if err != nil {
79+
u.log.Error("Failed to get admins", zap.Error(err))
80+
return err
81+
}
82+
for _, admin := range admins.(*tg.ChannelsChannelParticipants).Participants {
83+
if user, ok := admin.(*tg.ChannelParticipantAdmin); ok {
84+
currentAdmins = append(currentAdmins, user.UserID)
85+
}
86+
}
87+
for _, bot := range Workers.Bots {
88+
for _, admin := range currentAdmins {
89+
if admin == bot.Self.ID {
90+
u.log.Sugar().Infof("Bot @%s is already an admin", bot.Self.Username)
91+
continue
92+
}
93+
}
94+
botInfo, err := ctx.ResolveUsername(bot.Self.Username)
95+
if err != nil {
96+
u.log.Warn(err.Error())
97+
}
98+
_, err = u.client.API().ChannelsEditAdmin(
99+
u.client.CreateContext().Context,
100+
&tg.ChannelsEditAdminRequest{
101+
Channel: inputChannel,
102+
UserID: botInfo.GetInputUser(),
103+
AdminRights: tg.ChatAdminRights{
104+
PostMessages: true,
105+
},
106+
Rank: "admin",
107+
},
108+
)
109+
if err != nil {
110+
u.log.Sugar().Warnf("Failed to add @%s as admin", bot.Self.Username)
111+
u.log.Warn(err.Error())
112+
}
113+
u.log.Sugar().Infof("Added @%s as admin", bot.Self.Username)
114+
}
115+
return nil
44116
}

0 commit comments

Comments
 (0)