-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
94 lines (75 loc) · 3.4 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import datetime
from time import sleep
from BotHandler import BotHandler
from WeatherHandler import WeatherHandler
import logging
import sys
root = logging.getLogger()
root.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
root.addHandler(ch)
weather_api_key = '<open weather map api_key>'
tg_token = '<telegram bot token>'
weather_handler = WeatherHandler(api_key=weather_api_key)
greet_bot = BotHandler(token=tg_token)
now = datetime.datetime.now()
hour = now.hour
def main():
last_update = greet_bot.get_updates(offset=-1)
last_id = last_update['result'][-1]['update_id'] if last_update['result'] != [] else 0
while True:
last = greet_bot.get_updates(offset=-1)
last = last['result'][-1] if last['result'] != [] else 0
last_update_id = last['update_id']
if last_update_id == last_id:
sleep(5)
continue
else:
last_id = last_update_id
last_chat_text = last['message']
last_chat_id = last['message']['chat']['id']
last_keys = last_chat_text.keys()
if 'text' in last_keys:
last_text = last_chat_text['text'].lower()
if last_text == '/start':
greet_bot.get_location(last_chat_id)
sleep(1)
continue
elif last_text == 'cancel':
greet_bot.send_message(last_chat_id, "*Canceled.*\nSend me name of your city")
sleep(1)
continue
else:
res = weather_handler.by_city(city_name=last_text)
if res['cod'] == '404':
res = res['message']
greet_bot.send_message(last_chat_id, "*" + res.capitalize() + "*\nPlease try again...")
sleep(1)
continue
res_weather = res['weather'][-1]
res_main = res['main']
formatted_caption = '*City: {0}*\n\n*{1}*\n_{2}_\n*Temperature:* {3}_°C_\n' \
'*Pressure:* {4}_hPa_\n*Clouds:* {5}_%_\n*Humidity:* {6}_%_\n*Wind Speed:* {7}_m/s_' \
.format(res['name'], res_weather['main'], res_weather['description'], res_main['temp'],
res_main['pressure'], res['clouds']['all'], res_main['humidity'], res['wind']['speed'])
greet_bot.render_info(last_chat_id, res_weather['icon'], formatted_caption)
if 'location' in last_keys:
location = last_chat_text['location']
res = weather_handler.by_location(location)
res_weather = res['weather'][-1]
res_main = res['main']
formatted_caption = '*City: {0}*\n\n*{1}*\n_{2}_\n*Temperature:* {3}_°C_\n' \
'*Pressure:* {4}_hPa_\n*Clouds: {5}_%_*\n*Humidity:* {6}_%_\n*Wind Speed:* {7}_m/s_' \
.format(res['name'], res_weather['main'], res_weather['description'], res_main['temp'],
res_main['pressure'], res['clouds']['all'], res_main['humidity'], res['wind']['speed'])
greet_bot.render_info(last_chat_id, res_weather['icon'], formatted_caption)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
exit()