-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
450 lines (343 loc) · 13.7 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import os
import glob
from replit import db
from flask import Flask, request
import smtplib
from jinja2 import Environment, FileSystemLoader
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText
from email import encoders
from email.mime.base import MIMEBase
from telegram import Update, Bot, InlineKeyboardButton, InlineKeyboardMarkup, Message, user, ReplyKeyboardMarkup, ReplyKeyboardRemove
from telegram.callbackquery import CallbackQuery
from telegram.ext import Dispatcher, CallbackContext, CallbackQueryHandler, CommandHandler, MessageHandler, ConversationHandler, Filters
from telegram.ext.filters import MessageFilter
# APP PARAMS
BOT_TOKEN = os.getenv("BOT_TOKEN")
URL = "https://rh-finance-bot.charleslimjh.repl.co/"
EMAIL_ADDRESS = os.getenv('MAIL_USER')
EMAIL_PASSWORD = os.getenv('MAIL_PW')
# CONSTANTS
INVALID_COMMAND = "Sorry, I didn't understand what you just said. Please enter a valid command"
BUYEE_NAME_PROMPT = "Please enter the buyer's name, or type 'ok' to continue:"
BUYEE_MATRIC_PROMPT = "Please enter the buyer's matric card number:"
BUYEE_CCA_PROMPT = "Please enter the CCA:"
BUYEE_EVENT_PROMPT = "Please enter the event name:"
NUM_RECEIPTS_PROMPT = "Please enter the number of receipts: "
RECEIPT_TYPE_PROMPT = "Please choose your receipt type:"
RECEIPT_DETAILS_PROMPT = "Enter the details of this receipt in the following format: (AMOUNT,VENDOR,PURPOSE,DATE)"
RECEIPT_IMAGE_PROMPT = "Upload a clear image of the receipt:"
SUPPLEMENTARY_DOCS_PROMPT = "Please upload any supporting documents (e.g. Bank statements, Prize lists, Currency exchange etc.) one at a time, then type 'ok' to continue."
BUDGET_CATEGORY_PROMPT = "Please enter the Budget Category:"
CONFIRMATION_PROMPT = "Alright, all received! Please confirm that all the information you entered is true"
CANCEL_SETUP_PROMPT = "Goodbye, see you next time!"
SETUP_NEW_USER = """You have not saved your information yet, Treasurer!
Please input in your full name, phone number and email, separated by commas.
For example, type in 'Charles Lim,98765432,[email protected]'."""
SETUP_EXISTING_USER = """To update your particulars, input in your full name, phone number and email, separated by commas. Else, type /cancel.
For example, type in 'Charles Lim,98765432,[email protected]'."""
UPDATE_USER = range(0)
BUYEE_NAME, BUYEE_MATRIC, BUYEE_CCA, BUYEE_EVENT, NUM_RECEIPTS, RECEIPT_TYPE, RECEIPT_DETAILS, RECEIPT_IMAGE, SUPPLEMENTARY_DOCS, BUDGET_CATEGORY, CONFIRMATION = range(
11)
# utils
num_images = 0
supp_images = 0
def listfiles(path):
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)):
yield file
def clear_folder(folder_name):
for f in listfiles(folder_name):
os.remove("./images/" + f)
def attach_docs(folder_name, msg):
for f in listfiles(folder_name):
part = MIMEBase('application', "octet-stream")
part.set_payload(open("./images/" + f, "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment', filename=f)
msg.attach(part)
return msg
# Initialize flask app and bot
app = Flask(__name__)
bot = Bot(BOT_TOKEN)
def send_email(data):
environment = Environment(loader=FileSystemLoader("templates/"))
template = environment.get_template("template.html")
content = template.render(data)
msg = MIMEMultipart()
msg['Subject'] = data["CCA"] + ' Claims - ' + data["Event"]
msg['From'] = EMAIL_ADDRESS
msg['To'] = data["TreasurerEmail"]
msg.attach(MIMEText(content, 'html'))
print("attaching files")
msg = attach_docs("./images", msg)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
#################################
### Command Handler Functions ###
#################################
def start(update, context):
update.message.reply_text(
"Welcome to the RH Finance Bot, helping RH Treasurers process your claims!"
)
def userSetup(update, context):
# new user
chat_id = str(update.message.chat.id)
print("Existing user:", str(chat_id) in list(db.keys()))
if not str(chat_id) in list(db.keys()):
print("record user info")
update.message.reply_text(SETUP_NEW_USER)
return UPDATE_USER
# existing user
else:
print("display user info")
userInfo = db[str(chat_id)].split(',')
print(db[str(chat_id)], "\n", userInfo)
userName = userInfo[0]
userPhone = userInfo[1]
userEmail = userInfo[2]
update.message.reply_text(
"Treasurer Name: {}\nTreasurer Phone: {}\nEmail:{}".format(
userName, userPhone, userEmail))
update.message.reply_text(SETUP_EXISTING_USER)
return UPDATE_USER
def updateUser(update, context):
print("updating user particulars")
data = update.message.text.strip()
db[str(update.message.chat.id)] = data
data = data.split(',')
context.user_data["TreasurerName"] = data[0]
context.user_data["TreasurerPhone"] = data[1]
context.user_data["TreasurerEmail"] = data[2]
update.message.reply_text("""Okay, particulars updated as follows:
Treasurer name: {}
Phone number: {}
Email: {}""".format(data[0], data[1], data[2]))
return ConversationHandler.END
def cancelSetup(update, context):
print("cancel setup process")
update.message.reply_text("Okay, cancelling setup operation!",
reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
#################################
### Receipt Handler Functions ###
#################################
def receipt(update, context):
clear_folder("./images")
print("get authorisation letter names", "\n")
context.user_data['Students'] = []
global num_images, supp_images
num_images = 0
supp_images = 0
update.message.reply_text(BUYEE_NAME_PROMPT)
return BUYEE_NAME
def buyee_name(update, context):
print("get authorisation letter matric numbers")
print(context.user_data, "\n")
text = str(update.message.text)
if text.lower() == 'ok':
update.message.reply_text(BUYEE_CCA_PROMPT)
return BUYEE_CCA
context.user_data['Students'].append(text)
update.message.reply_text(BUYEE_MATRIC_PROMPT)
return BUYEE_MATRIC
def buyee_matric(update, context):
print("ask for cca/next authorisation letter name")
print(context.user_data, "\n")
text = str(update.message.text)
# add name/matric pair
name = context.user_data['Students'].pop()
context.user_data['Students'].append({"name": name, "matric": text})
update.message.reply_text(BUYEE_NAME_PROMPT)
return BUYEE_NAME
def buyee_cca(update, context):
print("ask for event")
print(context.user_data, "\n")
text = str(update.message.text)
context.user_data['CCA'] = text
update.message.reply_text(BUYEE_EVENT_PROMPT)
return BUYEE_EVENT
def buyee_event(update, context):
print("ask for number of receipts")
print(context.user_data, "\n")
text = str(update.message.text)
context.user_data['Event'] = text
update.message.reply_text(NUM_RECEIPTS_PROMPT)
return NUM_RECEIPTS
def num_receipts(update, context):
print("ask for receipt type")
print(context.user_data, "\n")
text = int(update.message.text)
context.user_data['TotalReceipts'] = text
context.user_data['ReceiptCount'] = text
reply_keyboard = [["Online", "Physical"]]
update.message.reply_text(RECEIPT_TYPE_PROMPT,
reply_markup=ReplyKeyboardMarkup(
reply_keyboard, one_time_keyboard=True))
return RECEIPT_TYPE
def receipt_type(update, context):
print("ask for receipt details")
print(context.user_data, "\n")
text = str(update.message.text)
if (text in context.user_data):
context.user_data[text] += 1
else:
context.user_data[text] = 1
update.message.reply_text(RECEIPT_DETAILS_PROMPT,
reply_markup=ReplyKeyboardRemove())
return RECEIPT_DETAILS
def receipt_details(update, context):
print("ask for receipt photo")
print(context.user_data, "\n")
text = str(update.message.text)
if ('Receipts' in context.user_data):
context.user_data['Receipts'].append(text)
else:
context.user_data['Receipts'] = [text]
update.message.reply_text(RECEIPT_IMAGE_PROMPT)
return RECEIPT_IMAGE
def receipt_image(update, context):
print("ask for more receipts/supplementary docs")
print(context.user_data, "\n")
global num_images
num_images += 1
photo_file = update.message.photo[-1].get_file()
photo_file.download("images/user_photo{}.jpg".format(num_images))
user_data = context.user_data
if ("TotalReceipts" in user_data and user_data["ReceiptCount"] > 1):
user_data["ReceiptCount"] = user_data["ReceiptCount"] - 1
reply_keyboard = [["Online", "Physical"]]
update.message.reply_text(RECEIPT_TYPE_PROMPT,
reply_markup=ReplyKeyboardMarkup(
reply_keyboard, one_time_keyboard=True))
return RECEIPT_TYPE
else:
update.message.reply_text(SUPPLEMENTARY_DOCS_PROMPT)
return SUPPLEMENTARY_DOCS
def supplementary_docs(update, context):
print("ask for more docs/budget category")
print(context.user_data, "\n")
global supp_images
supp_images += 1
try:
photo_file = update.message.photo[-1].get_file()
except:
text = str(update.message.text)
if text.lower() == 'ok':
update.message.reply_text(BUDGET_CATEGORY_PROMPT)
return BUDGET_CATEGORY
update.message.reply_text(SUPPLEMENTARY_DOCS_PROMPT)
return SUPPLEMENTARY_DOCS
else:
photo_file.download("images/supp_docs_photo{}.jpg".format(supp_images))
update.message.reply_text(SUPPLEMENTARY_DOCS_PROMPT)
return SUPPLEMENTARY_DOCS
def budget_category(update, context):
print("ask for confirmation")
print(context.user_data, "\n")
text = str(update.message.text)
context.user_data['BudgetCategory'] = text
reply_keyboard = [["Confirm"]]
update.message.reply_text(CONFIRMATION_PROMPT,
reply_markup=ReplyKeyboardMarkup(
reply_keyboard, one_time_keyboard=True))
return CONFIRMATION
def confirmation(update, context):
print("print summary")
print(context.user_data, "\n")
reply_text = "Okay, generating email now! Check your email for the draft claims template to be sent to the Finance D!"
update.message.reply_text(reply_text, reply_markup=ReplyKeyboardRemove())
# process data
user_data = context.user_data
if not "TreasurerEmail" in context.user_data:
print(db[str(update.message.chat.id)])
tmp = db[str(update.message.chat.id)].split(',')
context.user_data["TreasurerName"] = tmp[0]
context.user_data["TreasurerPhone"] = tmp[1]
context.user_data["TreasurerEmail"] = tmp[2]
if not ("Physical" in user_data):
context.user_data["Physical"] = 0
if not ("Online" in user_data):
context.user_data["Online"] = 0
totalPrice = 0
tmp = []
for receipt in context.user_data["Receipts"]:
receipt = receipt.split(",")
tmp.append({
"count": str(len(tmp) + 1),
"amount": receipt[0],
"vendor": receipt[1],
"purpose": receipt[2],
"date": receipt[3]
})
totalPrice += float(receipt[0])
context.user_data["Receipts"] = tmp
context.user_data["TotalAmount"] = totalPrice
send_email(context.user_data)
clear_folder("./images")
return ConversationHandler.END
def cancel(update, context):
update.message.reply_text(CANCEL_SETUP_PROMPT,
reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
def invalid(update, context):
update.message.reply_text("Invalid input!")
################
### Handlers ###
################
receipt_handler = ConversationHandler(
entry_points=[CommandHandler("receipt", receipt)],
states={
BUYEE_NAME: [MessageHandler(Filters.text & ~Filters.command, buyee_name)],
BUYEE_MATRIC:
[MessageHandler(Filters.text & ~Filters.command, buyee_matric)],
BUYEE_CCA: [MessageHandler(Filters.text & ~Filters.command, buyee_cca)],
BUYEE_EVENT:
[MessageHandler(Filters.text & ~Filters.command, buyee_event)],
NUM_RECEIPTS:
[MessageHandler(Filters.text & ~Filters.command, num_receipts)],
RECEIPT_TYPE:
[MessageHandler(Filters.regex('^(Online|Physical)$'), receipt_type)],
RECEIPT_DETAILS:
[MessageHandler(Filters.text & ~Filters.command, receipt_details)],
RECEIPT_IMAGE: [MessageHandler(Filters.photo, receipt_image)],
SUPPLEMENTARY_DOCS:
[MessageHandler(Filters.photo | Filters.text, supplementary_docs)],
BUDGET_CATEGORY: [MessageHandler(Filters.text, budget_category)],
CONFIRMATION: [MessageHandler(Filters.regex('^(Confirm)$'), confirmation)],
},
fallbacks=[CommandHandler("cancel", cancel)])
userSetupHandler = ConversationHandler(
entry_points=[CommandHandler("setup", userSetup)],
states={
UPDATE_USER: [MessageHandler(Filters.text & ~Filters.command, updateUser)]
},
fallbacks=[CommandHandler("cancel", cancelSetup)])
## Set up dispatcher object ##
dispatcher = Dispatcher(bot, None)
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(receipt_handler)
dispatcher.add_handler(userSetupHandler)
#######################################
@app.route('/{}'.format(BOT_TOKEN), methods=['POST'])
def respond():
# retrieve the message in JSON and then transform it to Telegram object
update = Update.de_json(request.get_json(force=True), bot)
print(update.message.chat.id, update.message.chat.username,
update.message.text)
dispatcher.process_update(update)
return "ok"
@app.route('/set_webhook', methods=['GET', 'POST'])
def set_webhook():
s = bot.setWebhook('{URL}{HOOK}'.format(URL=URL, HOOK=BOT_TOKEN))
if s:
return "webhook setup ok"
else:
return "webhook setup failed"
# homepage
@app.route('/')
def index():
return 'App is live!'
# gunicorn --bind 0.0.0.0:5003 main:app