es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

Cómo empezar un bot de Telegram con diferentes tokens en un servidor en Golang.

Hay un fragmento de código funcional que lanza un bot para recibir webhooks.

paquete principal

importar (
    "registro"
    "net/http"
)

función principal() {
    bot, err := tgbotapi.NewBotAPI("MiAwesomeBotToken")
    if err != nulo {
        registro.Fatal(err)
    }

bot.Debug = verdadero

registro.Printf("Autorizado en la cuenta %s", bot.Self.UserName)

_, err = bot.SetWebhook(tgbotapi.NewWebhookWithCert("https://example.com:8443/"+bot.Token, "cert.pem"))
si err! = nulo {
        registro.Fatal(err)
    }
info, err := bot.GetWebhookInfo()
si err! = nulo {
        registro.Fatal(err)
    }
si info.LastErrorDate! = 0 {
        registro.Printf("Fallo de callback de Telegram: %s", info.LastErrorMessage)
    }
actualizaciones: = bot.ListenForWebhook("/" + bot.Token)
go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nulo)

for update: =range actualizaciones {
        registro.Printf("%+v\n", actualización)
    }


}

Biblioteca para golang – tgbotapi

Cuando intento lanzar otro bot en el mismo servidor, con el mismo código, pero con un token diferente, por supuesto, sucede lo siguiente, el bot se autoriza correctamente pero no responde a los comandos, o simplemente no recibe un webhook, quién sabe la razón?

Apago 1 bot, luego el segundo se inicia sin problemas, al mismo tiempo no quieren …

Tags:  

Answer

  1. Avatar for davy.ai

    paquete principal

    import (
    “log”
    “net/http”
    )

    func main() {
    bot1, err := tgbotapi.NewBotAPI(“MiMarcaBotToken1”)
    if err != nil {
    log.Fatal(err)
    }

    bot1.Debug = true
    
    log.Printf("Autorizado en la cuenta %s", bot1.Self.UserName)
    
    _, err = bot1.SetWebhook(tgbotapi.NewWebhookWithCert("https://ejemplo.com:8443/"+bot1.Token, "cert.pem"))
    if err != nil {
        log.Fatal(err)
    }
    info, err := bot1.GetWebhookInfo()
    if err != nil {
        log.Fatal(err)
    }
    if info.LastErrorDate != 0 {
        log.Printf("Fallo en la llamada de Telegram: %s", info.LastErrorMessage)
    }
    updates := bot1.ListenForWebhook("/" + bot1.Token)
    go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
    
    for update := range updates {
        log.Printf("%+v\n", update)
    }
    
    bot2, err := tgbotapi.NewBotAPI("MiMarcaBotToken2")
    if err != nil {
        log.Fatal(err)
    }
    
    bot2.Debug = true
    
    log.Printf("Autorizado en la cuenta %s", bot2.Self.UserName)
    
    _, err = bot2.SetWebhook(tgbotapi.NewWebhookWithCert("https://ejemplo.com:8443/"+bot2.Token, "cert.pem"))
    if err != nil {
        log.Fatal(err)
    }
    info, err := bot2.GetWebhookInfo()
    if err != nil {
        log.Fatal(err)
    }
    if info.LastErrorDate != 0 {
        log.Printf("Fallo en la llamada de Telegram: %s", info.LastErrorMessage)
    }
    updates := bot2.ListenForWebhook("/" + bot2.Token)
    go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
    
    for update := range updates {
        log.Printf("%+v\n", update)
    }
    

    }

Comments are closed.