在 telegram-bot-ruby 中无法获取经度和纬度
Can't get longitude and latitude in telegram-bot-ruby
我有问题。在收到带有位置的消息后,我必须在其中添加 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
我会在你的 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
我有问题。在收到带有位置的消息后,我必须在其中添加 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
我会在你的 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