在Telegram-bot-Ruby中无法获得经度和纬度

时间:2018-08-25 11:53:38

标签: ruby geolocation telegram-bot

我有问题。在获取带有位置的消息后,必须在哪里添加message.location.latitude命令以获取用户的位置。我必须将地理编码替换为之前请求的用户位置。谢谢你的建议。

require 'open_weather'
require 'telegram/bot'

options_OpenWeather = { units: "metric", APPID: "some text" }
options_Bot = 'some keys'

Telegram::Bot::Client.run(options_Bot) do |bot|

bot.listen do |message|
  kb = [
        Telegram::Bot::Types::KeyboardButton.new(text: 'Show me your location', request_location: true)           
       ]
       markup = Telegram::Bot::Types::ReplyKeyboardMarkup.new(keyboard: kb)

       pp weather = OpenWeather::Current.geocode(53.11, 23.36, options_OpenWeather)
  case message.text
    when '/start'
      bot.api.send_message(chat_id: message.chat.id, text: "Hello, #{message.from.first_name}!")
    when '/end'
      bot.api.send_message(chat_id: message.chat.id, text: "Bye, #{message.from.first_name}!")     
    when '/help'
      bot.api.send_message(chat_id: message.chat.id, text: "Available commands:
      /start
      /end
      /help
      /hi
      /weather
      /test")
    when '/hi' 
      bot.api.send_message(chat_id: message.chat.id, text: 'Hey!', reply_markup: markup)         
     when '/weather'
      bot.api.send_message(chat_id: message.chat.id, text: "Weather in your location: #{weather["main"]["temp"]} celcius, #{weather["main"]["pressure"]} hPa. Humidity is #{weather["main"]["humidity"]}% ")                    
    when '/test'
      bot.api.send_message(chat_id: message.chat.id, text:"I'm testing command")
    else
      bot.api.send_message(chat_id: message.chat.id, text: "I don't understand you ")
    end
  end
end

1 个答案:

答案 0 :(得分:0)

我将其添加到您的case之前,并将所有处理天气的内容移至某个单独的功能。但是我跌倒了,我没有回答您的问题,也没有在这里写一些明显的东西。您可以查看my bot's前端。这是我很久以前做的相当老旧且杂乱的实现,但是它可以工作。如果您希望用户保存位置并在他们第一次设置位置后才发送命令,则也可以使用Redis进行缓存。

def(lat, lng)
  return OpenWeather::Current.geocode(lat, lng, options_OpenWeather)
  ...
end

if message.location != nil
  weather = handle_weather_from_location(
    message.location.lattitude
    message.location.longitude
  )
  bot.api.send_message(chat_id: message.chat.id, text: "Weather in your location: #{weather["main"]["temp"]} celcius, #{weather["main"]["pressure"]} hPa. Humidity is #{weather["main"]["humidity"]}% ")                    
end