-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
121 lines (97 loc) · 4.92 KB
/
Copy pathbot.py
File metadata and controls
121 lines (97 loc) · 4.92 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
import logging
import telegram
from telegram import Update
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters, PicklePersistence, \
CallbackQueryHandler
from utils.credentials import BOT_TOKEN
from utils.database import database
from utils.save_data import save_message_response, save_bot_data
from utils.taxi import send_driver_requests, send_offer_to_rider, notify_driver_rider_accepts_offer, \
driver_accepts_rider_request
from utils.utils import get_do_nothing_button, bot_data_file_path, request_requirements
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
bot_welcome = f"""
Book a driver in Montenegro.
Please include: {request_requirements}.
Example:\n
pickup from Chedi hotel lustica bay to UDG podgorica, 5pm today, 3 people
"""
await context.bot.send_message(chat_id=update.message.chat_id,
text=bot_welcome,
reply_to_message_id=update.message.message_id)
async def chat(update: Update, context: ContextTypes.DEFAULT_TYPE):
response = ''
message_text = update.message.text
user = {
'first_name': update.message.from_user.first_name,
'telegram_username': update.message.from_user.username,
'telegram_id': update.message.from_user.id,
'source': 'telegram'
}
if message_text.isdigit():
ride_request_id = database['active_request_ids'].find_one({
'chat_id': str(update.effective_chat.id)})
await send_offer_to_rider(message_text, user, ride_request_id['request_id'])
else:
if len(message_text.split(' ')) < 4:
message_text = f"Please send the following information: {request_requirements}"
await context.bot.send_message(chat_id=str(update.effective_chat.id), text=message_text)
else:
await context.bot.send_message(
chat_id=update.message.from_user.id,
text=f"We are looking for drivers for your request: {message_text}\n\n"
f"We'll let you know as soon as we receive an order."
)
await send_driver_requests(str(update.effective_chat.id), user, message_text)
save_message_response(response, update.message, context)
async def accept_ride(update: Update, context: ContextTypes.DEFAULT_TYPE):
cqd = update.callback_query.data
if cqd.startswith('accept__'):
# driver accepts rider's request
ride_request_id = cqd.split('__')[1]
ride_request = database['ride_requests'].find_one({'id': ride_request_id})
print(ride_request)
database['active_request_ids'].update_one(
{'chat_id': str(update.effective_chat.id)},
{'$set': {'chat_id': str(update.effective_chat.id), 'request_id': ride_request['id']}},
upsert=True
)
driver = {
'first_name': update.callback_query.from_user.first_name,
'telegram_username': update.callback_query.from_user.username,
'telegram_id': update.callback_query.from_user.id
}
await driver_accepts_rider_request(ride_request, driver)
if cqd.startswith('accept_offer'):
offer_id = cqd.split('__')[1]
await notify_driver_rider_accepts_offer(str(update.effective_chat.id), offer_id)
if cqd.startswith('decline_offer'):
text = "Offer was declined"
await context.bot.send_message(chat_id=str(update.effective_chat.id), text=text)
reply_markup = None
if cqd.startswith('accept'):
# driver accepts rider's request
reply_markup = telegram.InlineKeyboardMarkup([[get_do_nothing_button('request was accepted')]])
elif cqd.startswith('decline'):
reply_markup = telegram.InlineKeyboardMarkup([[get_do_nothing_button('request was declined')]])
await update.callback_query.edit_message_reply_markup(reply_markup)
async def chat_shared(update: Update, context: ContextTypes.DEFAULT_TYPE):
chat_id = str(update.message.chat_id)
print('chat_shared')
print('update.message', update.message)
print('chat_id', chat_id)
if __name__ == '__main__':
persistence = PicklePersistence(filepath=bot_data_file_path)
application = ApplicationBuilder().token(BOT_TOKEN).persistence(persistence).build()
application.add_handler(CommandHandler('start', start))
application.add_handler(CommandHandler('save', save_bot_data))
application.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), chat))
application.add_handler(CallbackQueryHandler(accept_ride))
application.add_handler(MessageHandler(filters.StatusUpdate.CHAT_SHARED |
filters.StatusUpdate.CHAT_CREATED,
chat_shared))
application.run_polling()