-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
159 lines (135 loc) · 5.27 KB
/
bot.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""
Bot provides some information about current state of International Space Station
"""
import logging
import time
from collections import defaultdict
from datetime import datetime
import requests
import telebot
from environs import Env
from telebot import apihelper, types
from telebot.types import Message
URL_ISS_LOCATION = 'http://api.open-notify.org/iss-now.json'
URL_ISS_CREW = 'http://api.open-notify.org/astros.json'
URL_ISS_PASS_TIMES = 'http://api.open-notify.org/iss-pass.json'
logging.basicConfig(filename='iss_bot.log',
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
env = Env()
env.read_env()
TOKEN = env('TOKEN')
PROXY = env('PROXY')
if PROXY:
apihelper.proxy = dict(https=PROXY)
bot = telebot.TeleBot(TOKEN, threaded=False)
def keyboard():
"""
Function generates keyboard
Returns: InlineKeyboardMarkup instance
"""
_keyboard = types.ReplyKeyboardMarkup(row_width=1)
button_position = types.KeyboardButton(text='Position')
button_crew = types.KeyboardButton(text='Crew')
button_pass_times = types.KeyboardButton(text='Pass times', request_location=True)
_keyboard.add(button_position)
_keyboard.add(button_crew)
_keyboard.add(button_pass_times)
return _keyboard
def get_iss_position(message: Message):
"""
Shows current geo position of ISS
Args:
message: telebot.types.Message instance
"""
response = requests.get(URL_ISS_LOCATION)
if response.status_code == 200:
_position = response.json()['iss_position']
bot.send_location(message.chat.id, **_position)
bot.send_message(chat_id=message.chat.id,
text=f'*Current ISS position is: '
f'{_position["latitude"]}°, {_position["longitude"]}°*',
parse_mode='Markdown',
reply_markup=keyboard())
return response
def get_iss_crew(message: Message):
"""
Shows current crew of ISS
Args:
message: telebot.types.Message instance
"""
response = requests.get(URL_ISS_CREW)
if response.status_code == 200:
data = response.json()
names = [human['name'] for human in data['people']]
text = f'*Current crew is {data["number"]} humans:*\n' + ',\n'.join(names)
bot.send_message(chat_id=message.chat.id, text=text, parse_mode='Markdown',
reply_markup=keyboard())
return response
def get_iss_pass_times(message: Message):
"""
Shows times when ISS will have the same position as user
Args:
message: telebot.types.Message instance
"""
parameters = {
'lat': message.location.latitude,
'lon': message.location.longitude,
}
response = requests.get(url=URL_ISS_PASS_TIMES, params=parameters)
if response.status_code == 200:
pass_times = (item['risetime'] for item in response.json()['response'])
pass_times = (datetime.fromtimestamp(item) for item in pass_times)
pass_times = [item.strftime('%d.%m.%Y %H:%M:%S') for item in pass_times]
bot.send_message(chat_id=message.chat.id,
text='\n'.join(pass_times),
reply_markup=keyboard())
return response
HANDLERS = defaultdict(str, **{
'Position': get_iss_position,
'Crew': get_iss_crew,
'Location': get_iss_pass_times,
})
@bot.message_handler(content_types=['text', 'location'])
def text_messages(message: Message):
"""
Buttons handler
"""
try:
if message.content_type == 'text':
handler = HANDLERS[message.text]
elif message.content_type == 'location':
handler = HANDLERS['Location']
else:
handler = None
if not handler:
bot.send_message(chat_id=message.chat.id, text='Push one of buttons',
reply_markup=keyboard())
return
response = handler(message)
if response.status_code == 200:
logging.info('Position request from user {%s} OK', message.from_user)
else:
bot.send_message(chat_id=message.chat.id,
text='Sorry, I lost connection. Try again later please',
reply_markup=keyboard())
logging.error('Message \'{message}\' user {user} FAILED due to response code '
'{status_code} from {url}. Response data: {data}',
message=message.text,
user=message.from_user,
status_code=response.status_code,
url=URL_ISS_LOCATION,
data=response.json())
except Exception as exception:
logging.error('Position request from user %s FAILED with exception %s',
message.from_user, exception)
bot.send_message(chat_id=message.chat.id,
text='Sorry, I don\'t feel good now. Try again later please',
reply_markup=keyboard())
if __name__ == '__main__':
while True:
try:
bot.polling(timeout=60, none_stop=True)
except requests.exceptions.ConnectionError as ex:
logging.exception('Exception: %s', ex)
time.sleep(5)