使用go-tdlib停留在“ authorizationsStateWaitPhoneNumber”状态

时间:2019-12-30 15:56:22

标签: go telegram tdlib

我正在与go-tdlib一起使用Telegram收集邮件。我尝试修改example。当我第三次重新启动服务时。它停止工作。所以我打印出消息。它一直赋予我authorizationsStateWaitPhoneNumber的身份。我什至无法在控制台中输入我的电话号码。下面是代码:

package main

import (
    "fmt"
    "log"
    "math"
    "os"
    "os/signal"
    "syscall"
    "time"
    "github.com/Arman92/go-tdlib"
    "github.com/joho/godotenv"
)

const (
    MaxAttempt = 10
)

var (
    apiID string
    apiHash string
)

var allChats []*tdlib.Chat
var haveFullChatList bool

func main() {
    log.Println("Start Service")
    log.Printf("API ID: %+v\n", apiID)
    log.Printf("API HASH: %+v\n", apiHash)
    tdlib.SetLogVerbosityLevel(1)
    tdlib.SetFilePath("./errors.txt")

    // Create new instance of client
    client := tdlib.NewClient(tdlib.Config{
        APIID:               apiID,
        APIHash:             apiHash,
        SystemLanguageCode:  "en",
        DeviceModel:         "Server",
        SystemVersion:       "1.0.0",
        ApplicationVersion:  "1.0.0",
        UseMessageDatabase:  true,
        UseFileDatabase:     true,
        UseChatInfoDatabase: true,
        UseTestDataCenter:   false,
        DatabaseDirectory:   "./tdlib-db",
        FileDirectory:       "./tdlib-files",
        IgnoreFileNames:     false,
    })

    log.Println("Starting auth")

    // Wait while we get AuthorizationReady!
    // Note: See authorization example for complete authorization sequence example
    currentState, err := client.Authorize()
    if err != nil {
        log.Fatalf("Authorize error: %+v\n", err)
    }

    attempt := 0
    for ; currentState.GetAuthorizationStateEnum() != tdlib.AuthorizationStateReadyType; currentState, err = client.Authorize() {
        if err != nil {
            log.Fatalf("Authorize error: %+v\n", err)
        }
        fmt.Printf("Current auth state: %+v\n", currentState)
        attempt += 1
        time.Sleep(5000 * time.Millisecond)
    }

    // get at most 1000 chats list
    if err = getChatList(client, 1000); err != nil {
        log.Fatalf("Error occurred in getChatList, msg: %+v\n", err)
    }
    log.Printf("got %d chats\n", len(allChats))

    for _, chat := range allChats {
        log.Printf("Chat title: %s \n", chat.Title)
    }

    signalTermination()
}

// see https://stackoverflow.com/questions/37782348/how-to-use-getchats-in-tdlib
func getChatList(client *tdlib.Client, limit int) error {
    if !haveFullChatList && limit > len(allChats) {
        offsetOrder := int64(math.MaxInt64)
        offsetChatID := int64(0)
        var lastChat *tdlib.Chat

        if len(allChats) > 0 {
            lastChat = allChats[len(allChats)-1]
            offsetOrder = int64(lastChat.Order)
            offsetChatID = lastChat.ID
        }

        // get chats (ids) from tdlib
        chats, err := client.GetChats(tdlib.JSONInt64(offsetOrder),
            offsetChatID, int32(limit-len(allChats)))
        if err != nil {
            return err
        }
        if len(chats.ChatIDs) == 0 {
            haveFullChatList = true
            return nil
        }

        for _, chatID := range chats.ChatIDs {
            // get chat info from tdlib
            chat, err := client.GetChat(chatID)
            if err == nil {
                allChats = append(allChats, chat)
            } else {
                return err
            }
        }

        return getChatList(client, limit)
    }

    return nil
}

func signalTermination() {
    c := make(chan os.Signal, 1)
    signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
    <-c
}

func init() {
    err := godotenv.Load()
    if err != nil {
        log.Fatal("Error loading .env file")
    }
    apiID = os.Getenv("TELEGRAM_API_ID")
    apiHash = os.Getenv("TELEGRAM_API_HASH")
}

有人以前遇到过这个问题吗?我在做什么错了?

1 个答案:

答案 0 :(得分:0)

我使用this example更改了身份验证过程,并且可以正常工作!