-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
200 lines (138 loc) · 6.52 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import os
import telebot
from models import Carpool, session
from functions import check_available_seats, check_time, format_time, schedule_ride, get_all_rides, get_rides_by_dir
from background import keep_alive
BOT_TOKEN = os.environ['BOT_TOKEN']
tb = telebot.TeleBot(token=BOT_TOKEN)
@tb.message_handler(commands=['ida', 'volta'])
def ride(message):
chat_id = message.chat.id
username = message.from_user.username
parsed_message = message.text.split()
command = parsed_message[0]
direction = command[1:].split('@')[0]
# Get Rides
if len(parsed_message) == 1:
rides = get_rides_by_dir(chat_id, direction)
if len(rides) == 0:
return tb.send_message(
chat_id, text=f"Não há ofertas de carona de {direction} :(")
return tb.send_message(chat_id, str(rides), parse_mode='HTML')
if not 3 < len(parsed_message) < 6:
return tb.send_message(chat_id,
text=f"Uso: {command} [horario] [vagas] [local]\n\
Ex: {command} 8:00 3 Barra Shopping")
time = parsed_message[1]
available_seats = parsed_message[2]
place = ' '.join(parsed_message[3:])
if not check_available_seats(available_seats):
return tb.send_message(chat_id, text='Número de vagas inválido')
if not check_time(time):
return tb.send_message(chat_id, text='Horário Invalido.')
time = format_time(time)
return_message = schedule_ride(chat_id, username, direction, time,
available_seats, place)
return tb.send_message(message.chat.id, return_message)
@tb.message_handler(commands=['caronas'])
def list_rides(message):
chat_id = message.chat.id
message = get_all_rides(chat_id)
return tb.send_message(chat_id, text=message, parse_mode='HTML')
@tb.message_handler(commands=['remover'])
def remove_ride(message):
chat_id = message.chat.id
username = message.from_user.username
parsed_message = message.text.split()
if len(parsed_message) != 2:
return tb.send_message(chat_id,
text='Uso: /remover [ida|volta]\nEx: /remover ida')
direction = parsed_message[1]
if direction not in ['ida', 'volta']:
return tb.send_message(chat_id,
text='Uso: /remover [ida|volta]\nEx: /remover ida')
carpool_type = 'going' if direction == 'ida' else 'returning'
session.query(Carpool).filter_by(chat_id=str(chat_id),
telegram_username=username,
carpool_type=carpool_type).delete()
return tb.send_message(chat_id,
text=f'@{username} removeu a carona de {direction}')
@tb.message_handler(commands=['vagas'])
def update_available_seats(message):
chat_id = message.chat.id
username = message.from_user.username
warning_message_use = f'Uso: /vagas [ida|volta] [vagas]\nEx: /vagas ida 2'
parsed_message = message.text.split()
if len(parsed_message) != 3:
return tb.send_message(chat_id, text=warning_message_use)
direction = parsed_message[1]
available_seats = parsed_message[2]
if direction not in ['ida', 'volta']:
return tb.send_message(chat_id, text=warning_message_use)
if not check_available_seats(available_seats):
return tb.send_message(chat_id, text='Número de vagas inválido')
direction = 'going' if direction == 'ida' else 'returning'
ride = session.query(Carpool).filter_by(chat_id=str(chat_id),
telegram_username=username,
carpool_type=direction).first()
if not ride:
return
ride.available_seats = available_seats
ride.save()
return tb.send_message(
chat_id,
text=f'@{username} atualizou o número de vagas para {available_seats}')
@tb.message_handler(commands=['lotou'])
def set_ride_to_full(message):
chat_id = message.chat.id
username = message.from_user.username
warning_message_use = f'Uso: /lotou [ida|volta]\nEx: /lotou ida'
parsed_message = message.text.split()
if len(parsed_message) != 2:
return tb.send_message(chat_id, text=warning_message_use)
direction = parsed_message[1]
if direction not in ['ida', 'volta']:
return tb.send_message(chat_id, text=warning_message_use)
carpool_type = 'going' if direction == 'ida' else 'returning'
ride = session.query(Carpool).filter_by(chat_id=str(chat_id),
telegram_username=username,
carpool_type=carpool_type).first()
if not ride:
return
ride.available_seats = 0
ride.save()
return tb.send_message(
chat_id, f'@{username} atualizou o número de vagas de {direction} para 0')
@tb.message_handler(commands=['help'])
def help(message):
chat_id = message.chat.id
return tb.send_message(
chat_id,
f'''Utilize esse Bot para agendar as caronas. A utilização é super simples e através de comandos:
/caronas --> Esse comando lista as caronas tanto de ida, quanto de volta do Fundão
/ida [horario] [vagas] [local] --> Esse comando serve para definir um horário que você está INDO para o Fundão.
Ex: /ida 11:00 3 Barra Shopping
(Inclui uma carona de ida às 11:00 com 3 vagas saindo do Barra Shopping)
/ida --> Esse comando lista todas as caronas de IDA disponíveis
/volta [horario] [vagas] [local] --> Esse comando serve para definir um horário que você está VOLTANDO para o SEU BAIRRO.
Ex: /volta 15:00 1 Posto 5
(Inclui uma carona de volta às 15:00 com 1 vaga para o Posto 5)
/volta --> Esse comando lista todas as caronas de VOLTA disponíveis
OBS --> O local precisa ser composto por no MÁXIMO duas palavras. Para mais de um local siga o padrão : local01/local02
Ex: gramado/macembu/mananciais/guerenguê/rodrigues caldas
/remover [ida|volta] --> Comando utilizado para remover a carona da lista. SEMPRE REMOVA a carona depois dela ter sido realizada.
Ex: /remover ida
/vagas [ida|volta] [vagas] --> Esse comando serve para atualizar o número de vagas de uma carona
Ex: /vagas ida 2
(Altera o número de vagas da ida para 2)
Ex: /vagas ida 0
(Altera o número de vagas da ida para 0, ou seja, lotado)
/lotou --> Esse comando serve para atualizar o número de vagas para 0. (Lotado)'''
)
@tb.message_handler(commands=['about'])
def about(message):
chat_id = message.chat.id
return tb.send_message(chat_id,
"https://github.com/fseda/carpool-bot-telegram")
keep_alive()
tb.polling(non_stop=True, interval=0)