forked from ccxx72/flyobj-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflyobj-bot.py
More file actions
151 lines (124 loc) · 5.5 KB
/
flyobj-bot.py
File metadata and controls
151 lines (124 loc) · 5.5 KB
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
import logging
import time
import telepot
from telepot.namedtuple import ReplyKeyboardMarkup, KeyboardButton
from config import TELEGRAM_TOKEN
from flight_manager import elenco_aerei, get_flight_info
from utils import translate, get_user_info, flight_message
logging.basicConfig(filename='myapp.log', format='%(asctime)s %(message)s', level=logging.INFO)
logging.info('Started')
def on_chat_message(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
user_dict = get_user_info(msg)
logging.info(f"USER: {user_dict['name']} {user_dict['last_name']}")
if content_type == 'text':
txt = msg['text']
if txt[2] + txt[-1:] == '><':
flight_dict = get_flight_info(user_dict['language'], txt)
if not flight_dict:
bot.sendMessage(
user_dict['chat_id'],
translate(
'Alcuni voli non hanno tutte le informazioni che vorrei mostrarti. Prova con un altro.',
user_dict['language']
)
)
flight_msg = flight_message(user_dict['language'])
# Messaggio rotta
msg_to_send = f"{flight_msg['volo']} {flight_dict['call']} {flight_msg['compagnia']} {flight_dict['op']}"
if flight_dict['from']:
msg_to_send += f" {flight_msg['da']} {flight_dict['from']} {flight_msg['per']} {flight_dict['to']}"
msg_to_send += f"{flight_msg['altezza']} {flight_dict['hight']} {flight_msg['velocita']} " \
f"{flight_dict['speed']} {flight_msg['rotta']} {flight_dict['trak']}."
bot.sendMessage(user_dict['chat_id'], msg_to_send)
# Messaggio velocità
if flight_dict['velocita_cambio'] > 1:
bot.sendMessage(
user_dict['chat_id'], f"{flight_msg['velocita_cambio']} {flight_dict['velocita_cambio']} Kmh."
)
# elif flight_dict['velocita_cambio'] < 0:
# bot.sendMessage(
# chat_id,
# 'In questo momento sta effettuando una discesa ad una velocità di ' +
# str(velocita_cambio) + ' Kmh' + str(dict_from_file[infovolo]['Vsi'])
# )
elif txt == '/start':
bot.sendMessage(
user_dict['chat_id'],
'Ciao ' + user_dict['name'] +
translate(
', if you give me your position I give you a list of flights in your area',
user_dict['language']
),
reply_markup=ReplyKeyboardMarkup(
keyboard=[
[
KeyboardButton(
text=translate('Share your coordinates', user_dict['language']),
request_location=True
)
],
[
KeyboardButton(
text=translate('Share your address', user_dict['language']),
request_location=False
)
]
]
)
)
elif txt == '/help':
bot.sendMessage(
user_dict['chat_id'],
translate(
'Questo bot fornisce informazioni sugli aerei che sorvolano la tua zona in un raggio di '
'circa 50 Km. Digita "/start" per attivare il bot',
user_dict['language']
)
)
else:
bot.sendMessage(
user_dict['chat_id'],
translate(
'Questo bot fornisce informazioni sugli aerei che sorvolano la tua zona in un raggio di '
'circa 50 Km. Digita "/start" per attivare il bot', user_dict['language']
)
)
elif content_type == "location":
tastiera = [[
KeyboardButton(
text=translate('Renew coordinates', user_dict['language']),
request_contact=None,
request_location=True)
]]
try:
latitudine = msg["location"]["latitude"]
longitudine = msg["location"]["longitude"]
except KeyError as e:
logging.info('I got a KeyError - reason "%s"' % str(e))
bot.sendMessage(
user_dict['chat_id'],
translate("C'è qualche problema con le tue coordinate", user_dict['language'])
)
logging.info(f"{latitudine}, {longitudine}")
aerei = elenco_aerei(user_dict, latitudine, longitudine)
if aerei['success']:
tastiera.extend(aerei['keyboard'])
bot.sendPhoto(user_dict['chat_id'], open(aerei['image'], "rb"))
bot.sendMessage(
user_dict['chat_id'],
translate('Clicca sul nome del volo per avere altre info', user_dict['language']),
reply_markup=ReplyKeyboardMarkup(keyboard=tastiera)
)
else:
bot.sendMessage(
user_dict['chat_id'],
translate(aerei["message"], user_dict['language']),
)
elif content_type == "sticker":
bot.sendMessage(user_dict['chat_id'], translate('The answer is 42', user_dict['language']))
bot = telepot.Bot(TELEGRAM_TOKEN)
bot.message_loop(on_chat_message)
logging.info('Listening ...')
while 1:
time.sleep(10)