【问题标题】:How to works with Golang echo framework and Telegram bot?如何使用 Golang echo 框架和 Telegram bot?
【发布时间】:2023-03-08 22:33:01
【问题描述】:

我想将“telegram bot”与“echo framework”一起使用(当服务器启动时,echo 和 telegram bot 一起工作)。我使用了下面的代码,但是当我运行它时,电报机器人没有启动。

我的 main.go:

package main

import (
    "database/sql"
    "log"
    "net/http"
    "strings"

    tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
    "github.com/labstack/echo"
    _ "github.com/mattn/go-sqlite3"
)

func main() {
    e := echo.New()
    e.GET("/", func(c echo.Context) error {
        return c.String(http.StatusOK, "Hello, World!")
    })
    _ = e.Start(":1323")

    db, err := sql.Open("sqlite3", "./criticism.db")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()

    bot, err := tgbotapi.NewBotAPI("")
    if err != nil {
        log.Fatal(err)
    }

    bot.Debug = true

    log.Printf("Authorized on account %s", bot.Self.UserName)

    u := tgbotapi.NewUpdate(0)
    u.Timeout = 60

    updates, err := bot.GetUpdatesChan(u)

    for update := range updates {
        if update.Message == nil {
            continue
        }

        gp_msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Hi")
        bot.Send(gp_msg)
    }
}

【问题讨论】:

  • 这可能无法解决您的问题,但我注意到您使用的是 echo lib v1.x,而最新的是 v4。您可能希望将要使用的版本号添加到 echo 导入路径。
  • NewBotAPI 的参数必须是根据go-telegram-bot-api.github.io/getting-started 的有效电报 API 令牌。你给一个空字符串。根据此文档,如果令牌无效,该函数将崩溃。
  • @chmike 他删除了令牌,以便可以在此处发布代码。他的问题是它永远不会到达那部分。
  • @NavidZarepak 因此问题不在电报机器人 api 中。 OP 没有说明程序在哪里以及如何失败。错误信息 ?没有这些信息就帮不上什么忙了。
  • @chmike 你可以。您可以清楚地看到该进程在_ = e.Start(":1323") 之后没有到达任何代码。问题是:“电报机器人没有启动。”。即使它可以到达代码(它不会),你关于令牌不存在的论点也不会解决任何问题,因为你知道它为什么不存在,即使你不存在,那么你应该问。如果您在没有任何错误消息的情况下无法提供帮助,请询问它们,以便他可以告诉您没有可以帮助您查看_ = e.Start(":1323") 部分并了解实际问题的方法。

标签: go telegram-bot go-echo


【解决方案1】:

问题是,当你启动回显服务器时,代码并没有继续下去。

为了同时使用它们,您需要将它们中的每一个分离到不同的线程中,并停止您的程序以完成并停止一切。

最简单的方法是将web server和telegram bot分开,分别启动:

func StartEcho() { 
    e := echo.New()
    e.GET("/", func(c echo.Context) error {
        return c.String(http.StatusOK, "Hello, World!")
    })
    _ = e.Start(":1323")
}

func StartBot() {
    bot, err := tgbotapi.NewBotAPI("")
    if err != nil {
        log.Fatal(err)
    }

    bot.Debug = true

    log.Printf("Authorized on account %s", bot.Self.UserName)

    u := tgbotapi.NewUpdate(0)
    u.Timeout = 60

    updates, err := bot.GetUpdatesChan(u)

    for update := range updates {
        if update.Message == nil {
            continue
        }

        gp_msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Hi")
        bot.Send(gp_msg)
    }
}

然后打电话给他们:

func main() {
    # Start the echo server in a separate thread
    go StartEcho()

    db, err := sql.Open("sqlite3", "./criticism.db")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()
    
    # Start the bot in a separate thread
    go StartBot()

    # To stop the program to finish and close
    select{}
    
    # You can also use https://golang.org/pkg/sync/#WaitGroup instead.
}

或者你可以在主线程中运行最后一个:

func main() {
    # Start the echo server in a separate thread
    go StartEcho()

    db, err := sql.Open("sqlite3", "./criticism.db")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()
    
    # Start the bot
    StartBot()
}

但是如果最后一个停止了,你的整个程序和 echo 服务器也会停止。所以你必须恢复任何恐慌,不要让它停止。

【讨论】:

    猜你喜欢
    • 2022-10-20
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 2019-03-04
    相关资源
    最近更新 更多