dtja73027 2017-11-29 11:17
浏览 117
已采纳

GoLang Telegram-bot-api如何询问位置?

I am using github.com/go-telegram-bot-api to create my bot. I want to ask location of the client. How do I do it? So far I did this:

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
    }
}
  • 写回答

1条回答 默认 最新

  • donglou1866 2017-11-29 14:10
    关注

    Unfortunately there is no examples for this in their repository. I read a bit of their code to figure out how to do it.

    This is a sample bot that requests location for every message it got. I created a one button virtual keyboard that shares location when clicked.

    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)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 netty整合springboot之后自动重连失效
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击