-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot.py
171 lines (130 loc) · 6.28 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
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Simple Bot to reply to Telegram messages
# This program is dedicated to the public domain under the CC0 license.
"""
This Bot uses the Updater class to handle the bot.
First, a few callback functions are defined. Then, those functions are passed to
the Dispatcher and registered at their respective places.
Then, the bot is started and runs until we press Ctrl-C on the command line.
Usage:
Example of a bot-user conversation using ConversationHandler.
Send /start to initiate the conversation.
Press Ctrl-C on the command line or send a signal to the process to stop the
bot.
"""
import re as regex
from datetime import datetime
all_user_data = dict()
from data import data
from telegram import (ReplyKeyboardMarkup, ReplyKeyboardRemove)
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, RegexHandler,
ConversationHandler)
# 1 su 2 fu 3 fd 4 sd
import logging
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
GENDER, PHOTO, LOCATION, BIO, STATION, PLATFORM, PTT, ACTIONSELECT, TRACKTRAIN= range(9)
stations = ['churchgate', 'marinelines', 'charniroad', 'grantroad', 'mumbaicentral', 'mahalaxmi', 'lowerparel', 'elphinstoneroad', 'dadar', 'matungaroad', 'mahim','bandra', 'kharroad', 'santacruz', 'vileparle', 'andheri', 'jogeshwari', 'goregaon', 'malad', 'kandivali', 'borivali', 'dahisar', 'miraroad', 'bhayandar', 'naigaon', 'vasai', 'nalasopara', 'virar']
dplat = {
'VIRAR <- (S)': 1 , '-> CHURCHGATE (S)': 4, 'VIRAR <- (F)': 2,'-> CHURCHGATE (F)': 3
}
def start(bot, update):
update.message.reply_text(
'''Hi! My name is Rail It. I am here to help you. Send /cancel to stop talking to me.\nPlease may I know what station are you at?''')
all_user_data[update.message.from_user.id] = {}
return STATION
def station(bot, update):
pfr = [['VIRAR <- (S)'] , ['-> CHURCHGATE (S)'], ['VIRAR <- (F)'], ['-> CHURCHGATE (F)']]
user = update.message.from_user
logger.info("%s said he is at %s", user.first_name, update.message.text)
stn = update.message.text.lower().replace(' ', '')
# print(stn)
# print(stations)
if stn not in stations:
update.message.reply_text("I am sorry. I don't think that's a station")
else:
all_user_data[user.id]['station']=stn
update.message.reply_text("""Cool! So which direction are you gonna travel>""",
reply_markup=ReplyKeyboardMarkup(pfr, one_time_keyboard=True))
return PLATFORM
def platform(bot, update):
user = update.message.from_user
logger.info("%s said wanna travel towards %s", user.first_name, update.message.text)
# update.message.reply_text("Platform number %s serves %s trains going %s side.\n Please select one of the following actions.", update.message.text, pfsupd[int(update.message.text)][0], )
rep = update.message.text
msgstr = ''
if '(F)' in rep:
msgstr += 'In a hurry I see! '
msgstr += 'You should head to Platform Number '+str(dplat[rep])+'.'
all_user_data[user.id]['platform'] = str(dplat[rep])
msgstr+='\nPlease select one of the following actions'
acl = [['1. Track Train'], ['2. Get info about Delays'], ['3. List all upcoming trains']]
update.message.reply_text(msgstr, reply_markup=ReplyKeyboardMarkup(acl, one_time_keyboard=True))
return ACTIONSELECT
def actionselect(bot, update):
user = update.message.from_user
ch = update.message.text
m = regex.search('[1-3]', ch)
ch = m.group(0)
logger.info("%s selected %s", user.first_name, ch)
if ch == "1":
update.message.reply_text("Please enter the scheduled time of the train")
return TRACKTRAIN
elif ch == "2":
delaymsg = delayinfo(all_user_data[user.id]['station'], all_user_data[user.id]['platform'])
update.message.reply_text(delaymsg)
elif ch == "3":
update.message.reply_text("All upcoming trains are: ")
upcominglist = get_upcoming_trains(station, platform)
def tracktrain(bot, update):
user = update.message.from_user
txt = update.message.text.replace('.', ':')
txt.lower()
get_train_info(all_user_data[user.id]['station'], all_user_data[user.id]['platform'], txt)
def get_train_info(station, platform, time):
logger.info("getting train for %s station platform %s time %s", station, platform, time)
def get_upcoming_trains(station, platform):
time = datetime.now().strftime('%Y-%m-%d %-I:%M%p').split()[1].lower()
def delayinfo(station, platform):
logger.info("getting delay info for %s station %s platform", station, platform)
return 'No delays! ;)'
def cancel(bot, update):
user = update.message.from_user
logger.info("User %s canceled the conversation.", user.first_name)
update.message.reply_text('Bye! I hope we can talk again some day.',
reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
def error(bot, update, error):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, error)
def main():
# Create the EventHandler and pass it your bot's token.
updater = Updater("761246133:AAG471ei8DYATbh3WLJK2wUoITDa5-cGq6U")
# Get the dispatcher to register handlers
dp = updater.dispatcher
# Add conversation handler with the states GENDER, PHOTO, LOCATION and BIO
conv_handler = ConversationHandler(
entry_points=[CommandHandler('start', start)],
states={
STATION: [MessageHandler(Filters.text, station )],
PLATFORM: [MessageHandler(Filters.text, platform)],
ACTIONSELECT: [MessageHandler(Filters.text, actionselect)],
TRACKTRAIN: [MessageHandler(Filters.text, tracktrain)]
},
fallbacks=[CommandHandler('cancel', cancel)]
)
dp.add_handler(conv_handler)
# log all errors
dp.add_error_handler(error)
# Start the Bot
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()