go - 处理用户回答
问题描述
任务是用户输入一笔存款,我可以在这种情况下处理它,但不像一个简单的命令。例子:
我的代码:
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)
解决方案
您已尝试使用路由器两次注册相同的 url '/mytokenbot'。您可以在 net/http 中找到错误:
https://golang.org/src/net/http/server.go#L2433
在 mux Handle 函数中。
因此,只需使用servemux查看您的注册函数代码,并检查您可能如何调用它两次。
推荐阅读
- node.js - 是否可以在不使用承诺的情况下将许多插入 MongoDB?
- debugging - 如何在 Pycharm Scientific View 中调试代码块
- opencv - 是否有 OpenCV 函数可以将掩码下的所有像素复制到数组中?
- javascript - On Press 监听器阻塞父组件 PanResponder
- oracle - PL/SQL 异常以什么顺序引发?
- codeigniter - Codeigniter - 访问非公共属性的自定义结果对象方法......没有设置器?
- python-3.5 - 使用分隔符合并两个列表中的项目
- c# - xbox live上的“应用程序没有云通知功能”在unity上登录
- javascript - 使用异步 Firestore 数据库调用后无法发送数组作为响应
- python - 更新神经网络中的权重