package main import ( "encoding/json" "fmt" "io/ioutil" "log" "net/http"…
- User
package main
import ( "encoding/json" "fmt" "io/ioutil" "log" "net/http"
"github.com/go-telegram-bot-api/telegram-bot-api" "github.com/kelseyhightower/envconfig")
type Config struct { WundergroundAPIKey string
envconfig:"WUNDERGROUND_API_KEY"WundergroundStationID stringenvconfig:"WUNDERGROUND_STATION_ID"TelegramBotToken stringenvconfig:"TELEGRAM_BOT_TOKEN"TelegramChatID int64envconfig:"TELEGRAM_CHAT_ID"}// Define a struct to hold the weather data type WeatherData struct { Observations []struct { Metric struct { Temp float64
json:"temp"WindSpeed float64json:"windSpeed"WindGust float64json:"windGust"Humidity intjson:"humidity"}json:"metric"}json:"observations"}func main() { var config Config err := envconfig.Process("", &config) if err != nil { log.Fatal(err) }
// Create a new Telegram bot bot, err := tgbotapi.NewBotAPI(config.TelegramBotToken) if err != nil { log.Fatal(err) } bot.Debug = true // Set up the update configuration u := tgbotapi.NewUpdate(0) u.Timeout = 60 // Get updates from the bot updates, err := bot.GetUpdatesChan(u) // Handle updates for update := range updates { if update.CallbackQuery != nil { // Handle button click handleCallback(bot, update.CallbackQuery, config) } if update.Message != nil && update.Message.IsCommand() { // Handle commands handleCommand(bot, update.Message, config) } }}
func handleCommand(bot *tgbotapi.BotAPI, message *tgbotapi.Message, config Config) { switch message.Command() { case "weather": sendWeatherData(bot, config) default: msg := tgbotapi.NewMessage(message.Chat.ID, "Invalid command. Try /weather.") bot.Send(msg) } }
func sendWeatherData(bot *tgbotapi.BotAPI, config Config) { // Get weather data from Wunderground by station ID wundergroundURL := fmt.Sprintf("https://api.weather.com/v2/pws/observations/current?stationId=%s&format=json&units=m&apiKey=%s", config.WundergroundStationID, config.WundergroundAPIKey) resp, err := http.Get(wundergroundURL) if err != nil { log.Fatal(err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal(err) }
// Parse weather data from JSON to struct var weatherData WeatherData err = json.Unmarshal(body, &weatherData) if err != nil { log.Fatal(err) } // Extract temperature, wind, and humidity data from struct temp := weatherData.Observations[0].Metric.Temp windSpeed := weatherData.Observations[0].Metric.WindSpeed windGust := weatherData.Observations[0].Metric.WindGust humidity := weatherData.Observations[0].Metric.Humidity // Send weather data to Telegram chat msg := tgbotapi.NewMessage(config.TelegramChatID, "") msg.ParseMode = "markdown" msg.Text = fmt.Sprintf("*Weather Data for %s*\n\nTemperature: %.1f°C\nWind Speed: %.1f km/h\nWind Gust: %.1f km/h\nHumidity: %d%%", config.WundergroundStationID, temp, windSpeed, windGust, humidity) // Create inline keyboard with a button keyboard := tgbotapi.NewInlineKeyboardMarkup( tgbotapi.NewInlineKeyboardRow( tgbotapi.NewInlineKeyboardButtonData("Request Update", "update_request"), ), ) msg.ReplyMarkup = keyboard bot.Send(msg) - Assistant
This is a Go program that creates a Telegram bot to retrieve live weather data from the Weather Underground API and send it to a user on Telegram. The program first reads the necessary credentials from environment variables using the
envconfigpackage. It then sets up the Telegram bot using thegithub.com/go-telegram-bot-api/telegram-bot-apipackage and starts listening for updates from the bot.When a user sends a "/weather" command, the program retrieves the weather data from the Weather Underground API and sends it to the user on Telegram. The weather data includes temperature, wind speed, wind gust, and humidity. The message also includes an inline keyboard with a "Request Update" button. When the button is clicked, the program performs the same action to send updated weather data.
The program also includes functions to handle button clicks and perform any desired action, such as sending an updated weather report.
- User
Convert it for update last sended message
- Assistant
- User
i got cannot use keyboard (variable of type tgbotapi.InlineKeyboardMarkup) as *tgbotapi.InlineKeyboardMarkup value in assignment
- Assistant