-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
131 lines (101 loc) · 5.33 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
"""
<summary>
This solution file creates a simple and usable telegram bot
</summary>
The code is strictly for view only, it is copyright infringement to remove this line. Anyone who conducts a copyright will be liable to be sued, unless there has been approval from either one of the 3 names mentioned above.
"""
from telegram import InlineKeyboardMarkup, InlineKeyboardButton, Update
from telegram.ext import CallbackQueryHandler, Updater, CallbackContext, MessageHandler, CommandHandler, Filters, commandhandler
import pandas as pd
import telepot
BOT = telepot.Bot("<insert_bot_father_secret_token>")
UPDATER = Updater("<insert_bot_father_secret_token>", use_context=True)
DP = UPDATER.dispatcher
FIRST_COLUMN_OF_EXCEL_FILE = 0
SECOND_COLUMN_OF_EXCEL_FILE = 1
THIRD_COLUMN_OF_EXCEL_FILE = 2
FOURTH_COLUMN_OF_EXCEL_FILE = 3
FILE_NAME= 'finlit.xlsx'
HELP_URL = 'https://ifl.org.sg/'
SORRY_MSG = "Sorry, I do not understand you. Please check if you have spelled the word correctly. If it is correct, please press the link /feedback to update us on the new term you want us to include in our update"
AVAILABLE_BUTTONS = ["yes_no", "understood_chat"]
YES_NO_BUTTONS = ["ABUDEN", "I BLUR SOTONG"]
UNDERSTOOD_CHAT_BUTTONS = ["HOSEH LIAO", "CATCH NO BALL"]
FIRST_BUTTON_INDEX = 0
SECOND_BUTTON_INDEX = 1
def get_inline_keyboard(type_of_question):
button_one = ""
button_two = ""
if type_of_question == AVAILABLE_BUTTONS[FIRST_BUTTON_INDEX]:
button_one = YES_NO_BUTTONS[FIRST_BUTTON_INDEX]
button_two = YES_NO_BUTTONS[SECOND_BUTTON_INDEX]
elif type_of_question == AVAILABLE_BUTTONS[SECOND_BUTTON_INDEX]:
button_one = UNDERSTOOD_CHAT_BUTTONS[FIRST_BUTTON_INDEX]
button_two = UNDERSTOOD_CHAT_BUTTONS[SECOND_BUTTON_INDEX]
keyboard = [
[
InlineKeyboardButton(button_one, callback_data=button_one),
InlineKeyboardButton(button_two, callback_data=button_two),
]
]
return keyboard
def get_keyboard(button_type, update):
keyboard = get_inline_keyboard(button_type)
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text("Do you understand what " + term + " means?", reply_markup=reply_markup)
def get_excel_data():
cols = 'A:D' # This is based on the column you want to use: A is 0, B is 1, C is 2, D is 3
terms = pd.read_excel(FILE_NAME, sheet_name=0,usecols=cols,header=0)
termsArr = terms.to_numpy()
return termsArr
def echo(update: Update, context: CallbackContext):
global youtube_url, term, type_of_button
command = update.message.text.lower()
termsArr = get_excel_data()
for word in termsArr:
if command in word[FIRST_COLUMN_OF_EXCEL_FILE].lower():
youtube_url = word[FOURTH_COLUMN_OF_EXCEL_FILE]
update.message.reply_text(word[SECOND_COLUMN_OF_EXCEL_FILE])
update.message.reply_text(word[THIRD_COLUMN_OF_EXCEL_FILE])
term = word[FIRST_COLUMN_OF_EXCEL_FILE]
type_of_button = AVAILABLE_BUTTONS[FIRST_BUTTON_INDEX]
get_keyboard(button_type = type_of_button, update = update)
return
update.message.reply_text(SORRY_MSG)
def button(update: Update, context: CallbackContext):
global type_of_button
query = update.callback_query
query.answer()
message = query.data
if type_of_button == AVAILABLE_BUTTONS[FIRST_BUTTON_INDEX]:
if message == YES_NO_BUTTONS[FIRST_BUTTON_INDEX]:
query.message.reply_text("Thanks for dropping by today!")
return
elif message == YES_NO_BUTTONS[SECOND_BUTTON_INDEX]:
query.message.reply_text("Hi, here is your link for " + term + " : " + youtube_url)
type_of_button = AVAILABLE_BUTTONS[SECOND_BUTTON_INDEX]
get_keyboard(button_type = type_of_button, update = query)
elif type_of_button == AVAILABLE_BUTTONS[SECOND_BUTTON_INDEX]:
if message == UNDERSTOOD_CHAT_BUTTONS[SECOND_BUTTON_INDEX]:
query.message.reply_text("Hi, it seems that you need help! We will be redirecting you to this url so that you can speak with one of our trainers : " + HELP_URL)
query.message.reply_text("If you are unable to access the link, it could be because that the server is down or our trainers are currently busy. Do let our trainers know what you want to enquire about")
return
elif message == UNDERSTOOD_CHAT_BUTTONS[FIRST_BUTTON_INDEX]:
query.message.reply_text("Hope the video was useful!")
return
def start(update, context):
update.message.reply_text("Welcome to ATLAS Dictionary! Enter any terms that you are unfamiliar with! Enter /help to ...")
def help(update, context):
update.message.reply_text("What do you need help with?")
def about(update, context):
update.message.reply_text("This bot helps youths to gain some financial knowledge of terms that are commonly used so as to make sound financial decisions and hence distinguish prejudiced opinions of financial products.")
def main():
# main code to run the function
DP.add_handler(CommandHandler("start", start))
DP.add_handler(CommandHandler("help", help))
DP.add_handler(CommandHandler("about", about))
DP.add_handler(MessageHandler(Filters.text, echo))
DP.add_handler(CallbackQueryHandler(button))
UPDATER.start_polling()
UPDATER.idle()
main()