Skip to content

Commit

Permalink
feat(bot): add user sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
EverythingSuckz committed Dec 4, 2023
1 parent fa27487 commit 9c48430
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ In addition to the mandatory variables, you can also set the following optional

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

- `USER_SESSION` : A pyrogram session string for the user bot. (default: `null`)

<hr>

### Use Multiple Bots to speed up
Expand Down
1 change: 1 addition & 0 deletions cmd/fsb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func main() {
}
cache.InitCache(log)
bot.StartWorkers(log)
bot.StartUserBot(log)
mainLogger.Info("Server started", zap.Int("port", config.ValueOf.Port))
mainLogger.Info("File Stream Bot", zap.String("version", versionString))
err = router.Run(fmt.Sprintf(":%d", config.ValueOf.Port))
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type config struct {
Host string `envconfig:"HOST" default:"http://localhost:8080"`
HashLength int `envconfig:"HASH_LENGTH" default:"6"`
UseSessionFile bool `envconfig:"USE_SESSION_FILE" default:"true"`
UserSession string `envconfig:"USER_SESSION"`
MultiTokens []string
}

Expand Down
44 changes: 44 additions & 0 deletions internal/bot/userbot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package bot

import (
"EverythingSuckz/fsb/config"

"github.com/celestix/gotgproto"
"github.com/celestix/gotgproto/sessionMaker"
"go.uber.org/zap"
)

type UserBotStruct struct {
log *zap.Logger
client *gotgproto.Client
}

var UserBot *UserBotStruct = &UserBotStruct{}

func StartUserBot(l *zap.Logger) {
log := l.Named("USERBOT")
if config.ValueOf.UserSession == "" {
log.Warn("User session is empty")
return
}
log.Sugar().Infoln("Starting userbot")
client, err := gotgproto.NewClient(
int(config.ValueOf.ApiID),
config.ValueOf.ApiHash,
gotgproto.ClientType{
Phone: "",
},
&gotgproto.ClientOpts{
Session: sessionMaker.PyrogramSession(config.ValueOf.UserSession),
DisableCopyright: true,
},
)
if err != nil {
log.Error("Failed to start userbot", zap.Error(err))
return
}
UserBot.log = log
UserBot.client = client
log.Info("Userbot started", zap.String("username", client.Self.Username), zap.String("FirstName", client.Self.FirstName), zap.String("LastName", client.Self.LastName))

}

0 comments on commit 9c48430

Please sign in to comment.