【问题标题】:GoLang Telegram-bot-api how to ask for location?GoLang Telegram-bot-api 如何询问位置?
【发布时间】:2018-05-20 03:19:30
【问题描述】:

我正在使用github.com/go-telegram-bot-api 创建我的机器人。我想问客户的位置。我该怎么做?到目前为止,我这样做了:

updates, err := bot.GetUpdatesChan(u)

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

    switch update.Message.Text {
    case "/shop":   
        msg := tgbotapi.NewMessage(update.Message.Chat.ID,"Send me your location")
        //I need to make this message ask for the location
        msg.Text = tgbotapi.ChatFindLocation
        bot.Send(msg)
        continue
    }
}

【问题讨论】:

    标签: go telegram telegram-bot


    【解决方案1】:

    不幸的是,他们的存储库中没有这方面的示例。我读了一些他们的代码来弄清楚如何去做。

    这是一个示例机器人,它会为收到的每条消息请求位置。我创建了一个单键虚拟键盘,单击时共享位置。

    package main
    
    import (
        "gopkg.in/telegram-bot-api.v4"
        "log"
    )
    
    const botToken = "Put your bot token here!"
    
    func main() {
        bot, err := tgbotapi.NewBotAPI(botToken)
        if err != nil {
            log.Panic(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
            }
    
            log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
    
            msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Hi")
            //msg.ReplyToMessageID = update.Message.MessageID
            btn := tgbotapi.KeyboardButton{
                RequestLocation: true,
                Text: "Gimme where u live!!",
            }
            msg.ReplyMarkup = tgbotapi.NewReplyKeyboard([]tgbotapi.KeyboardButton{btn})
            bot.Send(msg)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-10-20
      • 2018-04-24
      • 2023-03-08
      • 2018-11-12
      • 2018-04-07
      • 2017-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多