首页 > 解决方案 > 处理用户回答

问题描述

任务是用户输入一笔存款,我可以在这种情况下处理它,但不像一个简单的命令。例子:

введите сюда описание изображения

我的代码:

func main () {
    testing()
    NewBot, BotError = tgBotApi.NewBotAPI(configuration.BOT_TOKEN)
    if BotError != nil {
        fmt.Println(BotError.Error())
    }
    NewBot.Debug = true
    fmt.Println("OK", time.Now().Unix(), time.Now(), time.Now().Weekday())
    setWebhook(NewBot)
    updates := NewBot.ListenForWebhook("/" + configuration.BOT_TOKEN)
    //go successfulPaymentListen()
    go http.ListenAndServeTLS(fmt.Sprintf("%s:%s", configuration.BOT_HOST, configuration.BOT_PORT), configuration.CERT_FILE, configuration.CERT_KEY, nil)
    for update := range updates {

        if update.Message != nil {
            recognizeCommand(update)
        } else if update.CallbackQuery != nil {
            if update.CallbackQuery.Data == "/addFunds crypto" {
                get_data.AddFundsChooseCurrencyCrypto(update, NewBot)
            } else if update.CallbackQuery.Data == "/addFunds qiwi" {
                get_data.AddFundsChooseCurrencyQiwi(update, NewBot)
            } else if strings.Split(update.CallbackQuery.Data, " ")[2] != "" {
                get_data.AddFundsChooseCurrencyCurrentCrypto(update, NewBot, strings.Split(update.CallbackQuery.Data, " ")[2])
                //This function is below
            }
        }
    }
}

get_data.AddFundsChooseCurrencyCurrentCrypto:

func AddFundsChooseCurrencyCurrentCrypto(update tgBotApi.Update, NewBot *tgBotApi.BotAPI, currency string) {
    chatUser := int64(update.CallbackQuery.From.ID)
    msg := tgBotApi.NewMessage(chatUser, "Input a sum of deposit:")
    NewBot.Send(msg)
    //There is I have to handle user answer, but I can't override ListenWebHook
}

问题是我需要 ListenWebHook localy(在AddFundsChooseCurrencyCurrentCrypto函数中)而不是 main 函数

- - - - - - - - - - - - 更新 - - - - - - - - - - - -

我试过这段代码:

func AddFundsChooseCurrencyCurrentCrypto(update tgBotApi.Update, NewBot *tgBotApi.BotAPI, currency string) {
        chatUser := int64(update.CallbackQuery.From.ID)
        msg := tgBotApi.NewMessage(chatUser, "Input a sum of deposit:")
        NewBot.Send(msg)
         NewBotContext, BotError := tgBotApi.NewBotAPI(configuration.BOT_TOKEN)
         if BotError != nil {
            log.Panic(BotError.Error())
         }
        updates := NewBotContext.ListenForWebhook("/" + configuration.BOT_TOKEN)
        for update := range updates {
            fmt.Println(update)
        }
    }

但是错误:

panic: http: multiple registrations for /mytokenbot

goroutine 1 [running]:
net/http.(*ServeMux).Handle(0xe38620, 0xc25304, 0x2f, 0xc7dbe0, 0xc00018bec0)

标签: gotelegramtelegram-bot

解决方案


您已尝试使用路由器两次注册相同的 url '/mytokenbot'。您可以在 net/http 中找到错误:

https://golang.org/src/net/http/server.go#L2433

在 mux Handle 函数中。

因此,只需使用servemux查看您的注册函数代码,并检查您可能如何调用它两次。


推荐阅读