Amazing-Python-Scripts
48 строк · 1.4 Кб
1import requests
2import telebot
3
4bot_token = 'YOUR_BOT_TOKEN'
5bot = telebot.TeleBot(bot_token)
6
7
8@bot.message_handler(commands=['start', 'help'])
9def send_welcome(message):
10bot.reply_to(message, "Send me a city name")
11
12
13@bot.message_handler(func=lambda m: True)
14def echo_all(message):
15response = requests.get("http://api.weatherapi.com/v1/current.json?key={}&q={}".format(
16"2d3f4a2bd175414aa45175205221408", message.text)).json()
17bot.send_message(
18message.chat.id, format_response_to_human_readable(response))
19
20
21def format_response_to_human_readable(response):
22location = response["location"]
23current = response["current"]
24astronomy = response["forecast"]["forecastday"][0]["astro"]
25
26return "Weather Information for {}\n"\
27"Temperature: {}°C\n"\
28"Wind: {} kph, {}\n"\
29"Humidity: {}%\n"\
30"Pressure: {} mb\n"\
31"Sunrise: {}\n"\
32"Sunset: {}\n"\
33"Day Length: {} hours {} minutes".format(
34location["name"],
35current["temp_c"],
36current["wind_kph"],
37current["wind_dir"],
38current["humidity"],
39current["pressure_mb"],
40astronomy["sunrise"],
41astronomy["sunset"],
42astronomy["sunrise"],
43astronomy["sunset"],
44astronomy["moon_phase"]
45)
46
47
48bot.infinity_polling()
49