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 高价微信红包金额尾数算法
    • ¥30 矩阵分析与古诗词意境
    • ¥15 使用uniapp把小程序转为app
    • ¥15 在win10 64位的vs打包MSI文件,放到win7 64位上安装成可执行exe文件后,点击程序不运行。
    • ¥50 大众点评用户浏览/消费记录爬虫
    • ¥15 求制作一个个人网页,
    • ¥15 寻涂色内存脚本作者有项目有市场有资源.却技术
    • ¥15 蓝桥杯c51单片机问题
    • ¥15 ajax跨域问题请求修改代码
    • ¥15 python matplotlib