如何运行无限循环并在电报机器人中不断获取更新

我正在使用 Go ( go-telegram-bot-api ) 编写电报机器人。它不断解析网站并通知更改(每分钟)。今天我添加了简单的键盘,现在不能让它们一起工作。


问题是网页解析处于无限循环中,当它进入循环时,程序会忽略来自“客户端”的更新。


我已经尝试将所有内容放在一个循环中,更改订单等。也许有另一种或正确的方法可以做到这一点?


示例代码:


    u := tgbotapi.NewUpdate(0)

    u.Timeout = 60


    updates, err := bot.GetUpdatesChan(u)


    for update := range updates {

            if update.Message != nil {


                  switch update.Message.Text {

                        case "Show Keyboard":

                              Keyboard is sent

                        case "OptionsForParsing":

                              options.applied

                        case "StartParsing":

                              search bool = true

                        case "StopParsing":

                              search bool = false

                   }

            }


            if search == true{  

            for{

                 time.Sleep(1 * time.Minute)

                 AreThereChanges?()

                 if yes{

                         msg := tgbotapi.NewMessage(update.Message.Chat.ID, "help please")

                         bot.Send(msg)

                 }

            }}

     }


白猪掌柜的
浏览 153回答 2
2回答

梵蒂冈之花

如果我理解一切正确,您应该在分离的 goroutine 内开始解析循环,以便更新循环继续工作。尝试做这样的事情:// you can use cancel to stop running parsing goroutinectx, cancel := context.WithCancel(context.Background())go func() {&nbsp; ticker := time.NewTicker(5 * time.Second)&nbsp; defer ticker.Stop()&nbsp; for {&nbsp; &nbsp; select {&nbsp; &nbsp; &nbsp; case <-ctx.Done():&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; case <-ticker.C:&nbsp; &nbsp; &nbsp; &nbsp; AreThereChanges?()&nbsp; &nbsp; &nbsp; &nbsp; if yes {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg := tgbotapi.NewMessage(update.Message.Chat.ID, "help please")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bot.Send(msg)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; }}()从我的脑海中写作,所以不要指望它从第一次尝试就编译,而是演示这个想法。请查看以下链接以获取有关 Ticker 和取消上下文的更多信息:单击、单击。

米琪卡哇伊

我对 goroutine 和通道进行了一些研究。无限循环在一个单独的 goroutine 中,转义是通过无缓冲通道完成的(检查示例代码中的“游戏改变者”注释)。现在我可以在需要时进入和退出无限循环。这是我解决问题的方法:u := tgbotapi.NewUpdate(0)u.Timeout = 60updates, err := bot.GetUpdatesChan(u)done := make(chan bool)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//game changerfor update := range updates {&nbsp; &nbsp; &nbsp; &nbsp; if update.Message != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch update.Message.Text {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "Show Keyboard":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Keyboard is sent&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "OptionsForParsing":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; options.applied&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "StartParsing":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; search bool = true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "StopParsing":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; search bool = false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; done<-true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//game changer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ticker := time.NewTicker(time.Minute)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; defer ticker.Stop()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if search{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case <-done:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //game changer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case <-ticker.C:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AnyChanges?()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go