Amazing-Python-Scripts

Форк
0
48 строк · 1.4 Кб
1
import requests
2
import telebot
3

4
bot_token = 'YOUR_BOT_TOKEN'
5
bot = telebot.TeleBot(bot_token)
6

7

8
@bot.message_handler(commands=['start', 'help'])
9
def send_welcome(message):
10
    bot.reply_to(message, "Send me a city name")
11

12

13
@bot.message_handler(func=lambda m: True)
14
def echo_all(message):
15
    response = requests.get("http://api.weatherapi.com/v1/current.json?key={}&q={}".format(
16
        "2d3f4a2bd175414aa45175205221408", message.text)).json()
17
    bot.send_message(
18
        message.chat.id, format_response_to_human_readable(response))
19

20

21
def format_response_to_human_readable(response):
22
    location = response["location"]
23
    current = response["current"]
24
    astronomy = response["forecast"]["forecastday"][0]["astro"]
25

26
    return "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(
34
               location["name"],
35
               current["temp_c"],
36
               current["wind_kph"],
37
               current["wind_dir"],
38
               current["humidity"],
39
               current["pressure_mb"],
40
               astronomy["sunrise"],
41
               astronomy["sunset"],
42
               astronomy["sunrise"],
43
               astronomy["sunset"],
44
               astronomy["moon_phase"]
45
           )
46

47

48
bot.infinity_polling()
49

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.