登录
首页 >  Golang >  Go问答

go-tdlib 在“authorizationsStateWaitPhoneNumber”状态下停滞

来源:stackoverflow

时间:2024-03-10 11:27:19 199浏览 收藏

一分耕耘,一分收获!既然都打开这篇《go-tdlib 在“authorizationsStateWaitPhoneNumber”状态下停滞》,就坚持看下去,学下去吧!本文主要会给大家讲到等等知识点,如果大家对本文有好的建议或者看到有不足之处,非常欢迎大家积极提出!在后续文章我会继续更新Golang相关的内容,希望对大家都有所帮助!

问题内容

我正在使用 go-tdlib 收集 telegram 中的消息。我尝试修改该示例。当我第三次重新启动服务时。它停止工作。所以我打印了这些消息。它一直给我 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")
}

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


解决方案


我用 this example 更改了身份验证过程,它起作用了!

理论要掌握,实操不能落!以上关于《go-tdlib 在“authorizationsStateWaitPhoneNumber”状态下停滞》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>