簡體   English   中英

在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之前添加此內容,並將所有處理天氣的內容移至某個單獨的功能。 但是我跌倒了,我沒有回答您的問題,也沒有在這里寫一些明顯的東西。 您可以看一下我的機器人的前端。 這是我很久以前做的相當老舊且雜亂的實現,但是它可以工作。 如果您希望用戶保存位置並在他們第一次設置位置后才發送命令,則也可以使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM