Skip to content

Commit

Permalink
add an interface for client type
Browse files Browse the repository at this point in the history
  • Loading branch information
celestix committed Apr 30, 2024
1 parent 384a337 commit 30c49e2
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 107 deletions.
45 changes: 15 additions & 30 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ type Client struct {
NoAutoAuth bool

authConversator AuthConversator
clientType ClientType
clientType clientType
ctx context.Context
err error
autoFetchReply bool
Expand All @@ -92,16 +92,6 @@ type Client struct {
apiHash string
}

// Type of client to login to, can be of 2 types:
// 1.) Bot (Fill BotToken in this case)
// 2.) User (Fill Phone in this case)
type ClientType struct {
// BotToken is the unique API Token for the bot you're trying to authorize, get it from @BotFather.
BotToken string
// Mobile number of the authenticating user.
Phone string
}

type ClientOpts struct {
// Logger is instance of zap.Logger. No logs by default.
Logger *zap.Logger
Expand Down Expand Up @@ -185,7 +175,7 @@ type ClientOpts struct {
}

// NewClient creates a new gotgproto client and logs in to telegram.
func NewClient(appId int, apiHash string, cType ClientType, opts *ClientOpts) (*Client, error) {
func NewClient(appId int, apiHash string, cType clientType, opts *ClientOpts) (*Client, error) {
if opts == nil {
opts = &ClientOpts{
SystemLangCode: "en",
Expand Down Expand Up @@ -281,34 +271,29 @@ func (c *Client) initTelegramClient(

func (c *Client) login() error {
authClient := c.Auth()

if c.clientType.BotToken == "" {
status, err := authClient.Status(c.ctx)
if err != nil {
return errors.Wrap(err, "get auth status")
}
if status.Authorized {
return nil
}
if c.NoAutoAuth {
return intErrors.ErrSessionUnauthorized
}
status, err := authClient.Status(c.ctx)
if err != nil {
return errors.Wrap(err, "auth status")
}
if status.Authorized {
return nil
}
if c.NoAutoAuth {
return intErrors.ErrSessionUnauthorized
}
if c.clientType.getType() == clientTypeVPhone {
err = authFlow(
c.ctx, authClient,
c.authConversator,
c.clientType.Phone,
c.clientType.getValue(),
auth.SendCodeOptions{},
)
if err != nil {
return errors.Wrap(err, "auth flow")
}
} else {
status, err := authClient.Status(c.ctx)
if err != nil {
return errors.Wrap(err, "auth status")
}
if !status.Authorized {
if _, err := c.Auth().Bot(c.ctx, c.clientType.BotToken); err != nil {
if _, err := c.Auth().Bot(c.ctx, c.clientType.getValue()); err != nil {
return errors.Wrap(err, "login")
}
}
Expand Down
41 changes: 41 additions & 0 deletions clientType.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package gotgproto

const (
clientTypeVPhone int = iota
clientTypeVBot
)

type clientType interface {
getType() int
getValue() string
}

type clientTypePhone string

func (v *clientTypePhone) getType() int {
return clientTypeVPhone
}

func (v clientTypePhone) getValue() string {
return string(v)
}

func ClientTypePhone(phoneNumber string) clientType {
v := clientTypePhone(phoneNumber)
return &v
}

type clientTypeBot string

func (v *clientTypeBot) getType() int {
return clientTypeVPhone
}

func (v clientTypeBot) getValue() string {
return string(v)
}

func ClientTypeBot(botToken string) clientType {
v := clientTypeBot(botToken)
return &v
}
2 changes: 1 addition & 1 deletion examples/auth-using-api-base/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func main() {
// Get ApiHash from https://my.telegram.org/apps
"API_HASH_HERE",
// ClientType, as we defined above
gotgproto.ClientType{},
gotgproto.ClientTypePhone(""),
// Optional parameters of client
&gotgproto.ClientOpts{

Expand Down
9 changes: 1 addition & 8 deletions examples/auth-using-string-session/gotgproto-string/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,13 @@ import (
)

func main() {
// Type of client to login to, can be of 2 types:
// 1.) Bot (Fill BotToken in this case)
// 2.) User (Fill Phone in this case)
clientType := gotgproto.ClientType{
Phone: "PHONE_NUMBER_HERE",
}

client, err := gotgproto.NewClient(
// Get AppID from https://my.telegram.org/apps
123456,
// Get ApiHash from https://my.telegram.org/apps
"API_HASH_HERE",
// ClientType, as we defined above
clientType,
gotgproto.ClientTypePhone("PHONE_NUMBER_HERE"),
// Optional parameters of client
&gotgproto.ClientOpts{
Session: sessionMaker.StringSession("enter session string here").
Expand Down
9 changes: 1 addition & 8 deletions examples/auth-using-string-session/pyrogram-string/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,13 @@ import (
)

func main() {
// Type of client to login to, can be of 2 types:
// 1.) Bot (Fill BotToken in this case)
// 2.) User (Fill Phone in this case)
clientType := gotgproto.ClientType{
Phone: "PHONE_NUMBER_HERE",
}

client, err := gotgproto.NewClient(
// Get AppID from https://my.telegram.org/apps
123456,
// Get ApiHash from https://my.telegram.org/apps
"API_HASH_HERE",
// ClientType, as we defined above
clientType,
gotgproto.ClientTypePhone("PHONE_NUMBER_HERE"),
// Optional parameters of client
&gotgproto.ClientOpts{
Session: sessionMaker.PyrogramSession("enter session string here").
Expand Down
10 changes: 1 addition & 9 deletions examples/auth-using-string-session/telethon-string/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,12 @@ import (
)

func main() {
// Type of client to login to, can be of 2 types:
// 1.) Bot (Fill BotToken in this case)
// 2.) User (Fill Phone in this case)
clientType := gotgproto.ClientType{
Phone: "PHONE_NUMBER_HERE",
}

client, err := gotgproto.NewClient(
// Get AppID from https://my.telegram.org/apps
123456,
// Get ApiHash from https://my.telegram.org/apps
"API_HASH_HERE",
// ClientType, as we defined above
clientType,
gotgproto.ClientTypePhone("PHONE_NUMBER_HERE"),
// Optional parameters of client
&gotgproto.ClientOpts{
Session: sessionMaker.TelethonSession("enter session string here").
Expand Down
9 changes: 1 addition & 8 deletions examples/auth-using-tdata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,13 @@ func main() {
log.Fatalln(err)
}

// Type of client to login to, can be of 2 types:
// 1.) Bot (Fill BotToken in this case)
// 2.) User (Fill Phone in this case)
clientType := gotgproto.ClientType{
Phone: "PHONE_NUMBER_HERE",
}

client, err := gotgproto.NewClient(
// Get AppID from https://my.telegram.org/apps
123456,
// Get ApiHash from https://my.telegram.org/apps
"API_HASH_HERE",
// ClientType, as we defined above
clientType,
gotgproto.ClientTypePhone("PHONE_NUMBER_HERE"),
// Optional parameters of client
&gotgproto.ClientOpts{
// There can be up to 3 tdesktop.Account, we consider here there is
Expand Down
9 changes: 1 addition & 8 deletions examples/authorizing-as-user/memory_session/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,13 @@ import (
)

func main() {
// Type of client to login to, can be of 2 types:
// 1.) Bot (Fill BotToken in this case)
// 2.) User (Fill Phone in this case)
clientType := gotgproto.ClientType{
Phone: "PHONE_NUMBER_HERE",
}

client, err := gotgproto.NewClient(
// Get AppID from https://my.telegram.org/apps
123456,
// Get ApiHash from https://my.telegram.org/apps
"API_HASH_HERE",
// ClientType, as we defined above
clientType,
gotgproto.ClientTypePhone("PHONE_NUMBER_HERE"),
// Optional parameters of client
&gotgproto.ClientOpts{
Session: sessionMaker.SimpleSession(),
Expand Down
9 changes: 1 addition & 8 deletions examples/authorizing-as-user/sqlite_session/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,13 @@ import (
)

func main() {
// Type of client to login to, can be of 2 types:
// 1.) Bot (Fill BotToken in this case)
// 2.) User (Fill Phone in this case)
clientType := gotgproto.ClientType{
Phone: "PHONE_NUMBER_HERE",
}

client, err := gotgproto.NewClient(
// Get AppID from https://my.telegram.org/apps
123456,
// Get ApiHash from https://my.telegram.org/apps
"API_HASH_HERE",
// ClientType, as we defined above
clientType,
gotgproto.ClientTypePhone("PHONE_NUMBER_HERE"),
// Optional parameters of client
&gotgproto.ClientOpts{
Session: sessionMaker.SqlSession(sqlite.Open("echobot")),
Expand Down
16 changes: 5 additions & 11 deletions examples/downloader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,20 @@ package main

import (
"fmt"
"log"
"os"
"strconv"

"github.com/celestix/gotgproto"
"github.com/celestix/gotgproto/dispatcher/handlers"
"github.com/celestix/gotgproto/dispatcher/handlers/filters"
"github.com/celestix/gotgproto/ext"
"github.com/celestix/gotgproto/functions"
"github.com/celestix/gotgproto/sessionMaker"
"github.com/go-faster/errors"
"log"
"os"
"strconv"
)

func main() {
// Type of client to login to, can be of 2 types:
// 1.) Bot (Fill BotToken in this case)
// 2.) User (Fill Phone in this case)
clientType := gotgproto.ClientType{
BotToken: os.Getenv("TG_BOT_TOKEN"),
}

appIdEnv := os.Getenv("TG_APP_ID")
appId, err := strconv.Atoi(appIdEnv)
if err != nil {
Expand All @@ -34,7 +28,7 @@ func main() {
// Get ApiHash from https://my.telegram.org/apps
os.Getenv("TG_API_HASH"),
// ClientType, as we defined above
clientType,
gotgproto.ClientTypePhone("PHONE_NUMBER_HERE"),
// Optional parameters of client
&gotgproto.ClientOpts{
InMemory: true,
Expand Down
9 changes: 1 addition & 8 deletions examples/echo-bot/memory_session/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,13 @@ import (
)

func main() {
// Type of client to login to, can be of 2 types:
// 1.) Bot (Fill BotToken in this case)
// 2.) User (Fill Phone in this case)
clientType := gotgproto.ClientType{
BotToken: "BOT_TOKEN_HERE",
}

client, err := gotgproto.NewClient(
// Get AppID from https://my.telegram.org/apps
123456,
// Get ApiHash from https://my.telegram.org/apps
"API_HASH_HERE",
// ClientType, as we defined above
clientType,
gotgproto.ClientTypeBot("BOT_TOKEN_HERE"),
// Optional parameters of client
&gotgproto.ClientOpts{
InMemory: true,
Expand Down
9 changes: 1 addition & 8 deletions examples/echo-bot/sqlite_session/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,13 @@ import (
)

func main() {
// Type of client to login to, can be of 2 types:
// 1.) Bot (Fill BotToken in this case)
// 2.) User (Fill Phone in this case)
clientType := gotgproto.ClientType{
BotToken: "BOT_TOKEN_HERE",
}

client, err := gotgproto.NewClient(
// Get AppID from https://my.telegram.org/apps
123456,
// Get ApiHash from https://my.telegram.org/apps
"API_HASH_HERE",
// ClientType, as we defined above
clientType,
gotgproto.ClientTypeBot("BOT_TOKEN_HERE"),
// Optional parameters of client
&gotgproto.ClientOpts{
Session: sessionMaker.SqlSession(sqlite.Open("echobot")),
Expand Down

0 comments on commit 30c49e2

Please sign in to comment.