-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
278 lines (249 loc) · 12.4 KB
/
Copy pathbot.py
File metadata and controls
278 lines (249 loc) · 12.4 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
from telegram import (ReplyKeyboardMarkup, ReplyKeyboardRemove, InlineKeyboardButton, InlineKeyboardMarkup)
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, RegexHandler,ConversationHandler,CallbackQueryHandler)
import logging
import telegramcalendar
import telegram
from pytz import timezone
import os
import time
import datetime
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
SET_STAT, BOOK, MENU, VIEW, CAT, DATE, DONE = range(7)
LST_CHAT_ID = []
TOKEN = "1047958577:AAFpUNLTsBvr9fi3-DQmZIElPVZcC895Ia0"
t = "1115904035:AAG5x0dpPwmYNKRNBIHfjN7iADHTU4VN6UE"
PORT = int(os.environ.get('PORT', 5000))
def start(bot, update):
try:
chat_id=update["message"]["chat"]["id"]
if chat_id not in LST_CHAT_ID:
LST_CHAT_ID.append(chat_id)
print(LST_CHAT_ID)
print("Total users till date: "+ str(len(LST_CHAT_ID)))
print(datetime.datetime.now(timezone('UTC')).astimezone(timezone('Asia/Kolkata')))
day = datetime.datetime.now(timezone('UTC')).astimezone(timezone('Asia/Kolkata')).day
print(day)
if day == 1:
date_str = str(day) + "st"
new_file = date_str + " July 2020.pdf"
elif day == 2:
date_str = str(day) + "nd"
new_file = date_str + " July 2020.pdf"
elif day == 3:
date_str = str(day) + "rd"
new_file = date_str + " July 2020.pdf"
else:
if day == 22:
date_str = str(day) + "nd"
new_file = date_str + " June 2020.pdf"
print(new_file)
elif day == 23:
date_str = str(day) + "rd"
new_file = date_str + " June 2020.pdf"
else:
if day > 23:
date_str = str(day) + "th"
new_file = date_str + " June 2020.pdf"
else:
date_str = str(day) + "th"
new_file = date_str + " July 2020.pdf"
if os.path.exists('posters/'+new_file):
bot.send_chat_action(chat_id=update["message"]["chat"]["id"], action=telegram.ChatAction.TYPING)
update.message.reply_text("Webinar poster scheduled for today.")
bot.send_document(chat_id=update.message.chat.id, document=open('posters/'+new_file,"rb"))
else:
if int(day) < 22 and int(datetime.datetime.now().month)==6:
dl = 22 - int(day)
bot.send_chat_action(chat_id=update["message"]["chat"]["id"], action=telegram.ChatAction.TYPING)
update.message.reply_text(str(dl)+" days left for the webinar series to start!")
else:
bot.send_chat_action(chat_id=update["message"]["chat"]["id"], action=telegram.ChatAction.TYPING)
update.message.reply_text("The Webinar series are over.")
return ConversationHandler.END
reply_keyboard = [['View webinar posters date wise'],['View webinar posters category wise'],['View webinar posters by viewing permissions'],['Thanks']]
update.message.reply_text("Select an option",reply_markup=ReplyKeyboardMarkup(reply_keyboard, resize_keyboard=True))
return SET_STAT
except Exception as e:
print(e)
update.message.reply_text("Service Timed Out. Please press /start to continue.")
def menu(bot,update):
try:
bot.send_chat_action(chat_id=update["message"]["chat"]["id"], action=telegram.ChatAction.TYPING)
reply_keyboard = [['View webinar posters date wise'],['View webinar posters category wise'],['View webinar posters by viewing permissions']]
update.message.reply_text("Select an option",reply_markup=ReplyKeyboardMarkup(reply_keyboard, resize_keyboard=True))
return SET_STAT
except Exception as e:
print(e)
update.message.reply_text("Service Timed Out. Please press /start to continue.")
def select_service(bot,update):
try:
serviceType = update.message.text
print(serviceType)
if serviceType == 'View webinar posters date wise':
print(serviceType)
date_wise(bot, update)
return SET_STAT
elif serviceType == 'View webinar posters category wise':
print(serviceType)
menu_cat(bot,update)
return CAT
elif serviceType == 'View webinar posters by viewing permissions':
print(serviceType)
menu_view(bot,update)
return VIEW
elif serviceType == 'Thanks':
print("Finally")
thanks(bot,update)
return SET_STAT
elif serviceType == 'Menu':
print("m")
STATE = MENU
menu(bot,update)
return SET_STAT
else:
STATE = MENU
return MENU
except Exception as e:
print(e)
def date_wise(bot, update):
try:
print("Hi")
user = update.message.from_user
update.message.reply_text('I see! Please select a date to proceed',reply_markup=telegramcalendar.create_calendar())
except Exception as e:
print(e)
update.message.reply_text("Service Timed Out. Please press /start to continue.")
def select_slot(bot, update):
try:
print("Hi")
selected,date = telegramcalendar.process_calendar_selection(bot, update)
if selected:
print(date.day)
day = date.day
if (21< int(day) < 31 and int(date.month) == 6) or (int(day)<=12 and int(date.month) == 7):
if day == 1:
date_str = str(day) + "st"
new_file = date_str + " July 2020.pdf"
elif day == 2:
date_str = str(day) + "nd"
new_file = date_str + " July 2020.pdf"
elif day == 3:
date_str = str(day) + "rd"
new_file = date_str + " July 2020.pdf"
else:
if day == 22:
date_str = str(day) + "nd"
new_file = date_str + " June 2020.pdf"
elif day == 23:
date_str = str(day) + "rd"
new_file = date_str + " June 2020.pdf"
else:
if day > 23:
date_str = str(day) + "th"
new_file = date_str + " June 2020.pdf"
else:
date_str = str(day) + "th"
new_file = date_str + " July 2020.pdf"
bot.send_document(chat_id=update.callback_query.from_user.id, document=open('posters/'+new_file,"rb"))
bot.send_message(chat_id=update.callback_query.from_user.id, text = "Select an option to continue.",reply_markup = ReplyKeyboardMarkup(keyboard= [['Thanks', 'Menu']],resize_keyboard=True))
return
else:
bot.send_message(chat_id=update.callback_query.from_user.id, text ="No Webinar is scheduled for the selected date.")
bot.send_message(chat_id=update.callback_query.from_user.id, text = "Select an option to continue.",reply_markup = ReplyKeyboardMarkup(keyboard= [['Thanks', 'Menu']],resize_keyboard=True))
return
except Exception as e:
print(e)
update.message.reply_text("Service Timed Out. Please press /start to continue.")
def cat_wise(bot,update):
try:
#bot.send_chat_action(chat_id=update["message"]["chat"]["id"], action=telegram.ChatAction.TYPING)
#update.message.reply_text('I see! Please select a category to proceed',reply_markup=ReplyKeyboardMarkup(keyboard= [['Thanks', 'Menu']],resize_keyboard=True))
print("Category Wise")
cat = update.message.text
files = []
bot.send_chat_action(chat_id=update["message"]["chat"]["id"], action=telegram.ChatAction.TYPING)
for r, d, f in os.walk(cat+'/'):
for file in f:
print(file)
bot.send_document(chat_id=update.message.chat.id, document=open(cat+'/'+file,"rb"))
update.message.reply_text("Select an option to continue.",reply_markup = ReplyKeyboardMarkup(keyboard= [['Thanks', 'Menu']],resize_keyboard=True))
return SET_STAT
except Exception as e:
print(e)
update.message.reply_text("Service Timed Out. Please press /start to continue.")
def view_wise(bot,update):
try:
print("View Wise"+update.message.text)
bot.send_chat_action(chat_id=update["message"]["chat"]["id"], action=telegram.ChatAction.TYPING)
cat = update.message.text
for r, d, f in os.walk(cat+'/'):
for file in f:
bot.send_document(chat_id=update.message.chat.id, document=open(cat+'/'+file,"rb"))
update.message.reply_text("Select an option to continue.",reply_markup = ReplyKeyboardMarkup(keyboard= [['Thanks', 'Menu']],resize_keyboard=True))
return SET_STAT
except Exception as e:
print(e)
update.message.reply_text("Service Timed Out. Please press /start to continue.")
def menu_cat(bot, update):
try:
bot.send_chat_action(chat_id=update["message"]["chat"]["id"], action=telegram.ChatAction.TYPING)
time.sleep(2)
keyboard = [['Guidance for better placements'], ['Higher studies and Career Guidance'],['Research and Innovations'],['Entrepreneurship Development'],['Trends in Technology']]
reply_markup = ReplyKeyboardMarkup(keyboard,resize_keyboard=True)
update.message.reply_text("Select an option to continue.", reply_markup=reply_markup)
except Exception as e:
print(e)
def menu_view(bot, update):
try:
bot.send_chat_action(chat_id=update["message"]["chat"]["id"], action=telegram.ChatAction.TYPING)
time.sleep(2)
keyboard = [['Private Viewing'], ['Public Viewing']]
reply_markup = ReplyKeyboardMarkup(keyboard,resize_keyboard=True)
update.message.reply_text("Select an option to continue.", reply_markup=reply_markup)
except Exception as e:
print(e)
def thanks(bot, update):
user = update.message.from_user
logger.info("Bio of %s: %s", user.first_name, update.message.text)
update.message.reply_text('Thank you! I hope we can talk again some day. To again have a conversation type Menu or /start.',reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
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. To again have a conversation type Menu or /start.',
reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
def main():
# Create the EventHandler and pass it your bot's token.
updater = Updater(TOKEN)
# Get the dispatcher to register handlers
dp = updater.dispatcher
conv_handler = ConversationHandler(
entry_points=[CommandHandler('start', start)],
states={
SET_STAT: [RegexHandler('^(View webinar posters date wise|View webinar posters category wise|View webinar posters by viewing permissions|Thanks|Menu)$',select_service)],
DATE: [MessageHandler(Filters.text,date_wise),CommandHandler('cancel', cancel)],
VIEW: [MessageHandler(Filters.text, view_wise),CommandHandler('cancel', cancel)],
CAT: [MessageHandler(Filters.text, cat_wise),CommandHandler('cancel', cancel)],
MENU: [MessageHandler(Filters.text, menu)]
},
fallbacks=[CommandHandler('start', start),CommandHandler('cancel', cancel)],
allow_reentry = True
)
dp.add_handler(conv_handler)
dp.add_handler(CallbackQueryHandler(select_slot))
# Add conversation handler with the states GENDER, PHOTO, LOCATION and BIO
# log all errors)
# Start the Bot
updater.start_webhook(listen="0.0.0.0",port=int(PORT),url_path=TOKEN)
updater.bot.setWebhook('https://cryptic-cliffs-47840.herokuapp.com/' + TOKEN)
#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()