-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
225 lines (184 loc) Β· 6.79 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
import logging
import pprint
import requests
from typing import Dict
from telegram import __version__ as TG_VER
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
if __version_info__ < (20, 0, 0, "alpha", 1):
raise RuntimeError(
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
f"{TG_VER} version of this example, "
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
)
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, Update
from telegram.ext import (
Application,
CommandHandler,
ContextTypes,
ConversationHandler,
MessageHandler,
filters,
)
import os
import keyBoards
import helperFunctions
from dotenv import load_dotenv
load_dotenv()
# Enable logging
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
logger = logging.getLogger(__name__)
# Starting the program of telegram bot ____________________________________________________________________________
WEBHOOK_LINK_2 = os.getenv("WEBHOOK_PAPERS")
WEBHOOK_LINK_CMP = os.getenv("WEBHOOK_COMPS")
# States
TYPING_BARCODE, CHOOSING, TYPING_PAPER, TYPING_ISSUE = range(4)
markup_1 = ReplyKeyboardMarkup(keyBoards.reply_keyboard_1, one_time_keyboard=True)
# Functions for the bot
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
await update.message.reply_text(
"Enter your barcode : "
)
return TYPING_BARCODE
async def enter_barcode(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
text = update.message.text
context.user_data["barcode"] = text
await update.message.reply_text(
"This bot will help you to get your papers and marks\n"
"Choose an option : "
, reply_markup=markup_1
)
return CHOOSING
async def enter_paper_no(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
text = update.message.text
context.user_data["paper"] = text
# pprint.pprint(context.user_data)
if context.user_data["choice"] == "π Get Marks":
params = {"barcode": context.user_data["barcode"], "paper_no": context.user_data["paper"]}
paper_no = params["paper_no"]
sheet_id = "Paper " + params["paper_no"]
data = {"message": params['barcode'], "chat_id": update.message.chat_id, "paper_no": paper_no, "sheet_id": sheet_id}
response = requests.post(WEBHOOK_LINK_2, data=data)
response_data = response.json()
if response_data["status"] == "success":
data_record = response_data["data"][0]
await update.message.reply_html(helperFunctions.generate_beautiful_message(
name=data_record[1],
marks=data_record[4],
Drank=data_record[0],
Arank="-",
link="-",
year="2023",
paper_no=paper_no,
ptype="ONLINE"
))
elif response_data["status"] == "failed":
await update.message.reply_text(response_data["message"])
else:
await update.message.reply_text("Something went wrong")
elif context.user_data["choice"] == "π§Ύ Get Paper":
await update.message.reply_html("This Feature is not available yet.")
else:
await update.message.reply_text("Invalid Choice")
await update.message.reply_text(
"Choose an option : "
, reply_markup=markup_1
)
return CHOOSING
async def get_marks(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
text = update.message.text
context.user_data["choice"] = text
await update.message.reply_text(
"Enter your paper no : "
)
return TYPING_PAPER
async def get_papers(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
text = update.message.text
context.user_data["choice"] = text
await update.message.reply_text(
"Enter your paper no : "
)
return TYPING_PAPER
async def paper_issue(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
text = update.message.text
context.user_data["choice"] = text
await update.message.reply_text(
"Enter Your Complaint : "
)
return TYPING_ISSUE
async def enter_paper_issue(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
text = update.message.text
context.user_data["issue"] = text
data = {
"barcode": context.user_data['barcode'],
"chat_id": update.message.chat_id,
"complaint": context.user_data["issue"]
}
response = requests.post(WEBHOOK_LINK_CMP, data=data)
response_data = response.json()
if response_data["status"] == "success":
await update.message.reply_text(
"\n π¨πΌβπ» Your Complaint has been recorded ! π₯ \n"
"\n π² We will check it soon π°π° \n"
)
else:
await update.message.reply_text(response_data["message"])
await update.message.reply_text(
"Choose an option : "
, reply_markup=markup_1
)
return CHOOSING
async def done(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
user_data = context.user_data
user_data.clear()
await update.message.reply_html(
"\n<b>ππΌ Bye! </b>\n"
"\nGood Luck for your exams. ππ\n"
"\n<u>To start again</u> /start π\n",
reply_markup=ReplyKeyboardRemove(),
)
return ConversationHandler.END
# Main Function
def main() -> None:
application = Application.builder().token(os.getenv("API_TOKEN")).build()
# Add conversation handler with the states that defined above
conv_handler = ConversationHandler(
entry_points=[CommandHandler("start", start)],
states={
CHOOSING: [
MessageHandler(
filters.Regex("^π Get Marks$"), get_marks
),
MessageHandler(
filters.Regex("^π§Ύ Get Paper$"), get_papers
),
MessageHandler(
filters.Regex("^π Paper Issues$"), paper_issue
),
],
TYPING_BARCODE: [
MessageHandler(
filters.Regex("^[0-9]{8,10}$"), enter_barcode
)
],
TYPING_PAPER: [
MessageHandler(
filters.Regex("^[0-9]{2}$"), enter_paper_no
)
],
TYPING_ISSUE: [
MessageHandler(
filters.TEXT, enter_paper_issue
)
]
},
fallbacks=[MessageHandler(filters.Regex("^β Close"), done)],
)
application.add_handler(conv_handler)
application.run_polling()
if __name__ == "__main__":
main()