-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtele.py
More file actions
143 lines (122 loc) · 4.74 KB
/
tele.py
File metadata and controls
143 lines (122 loc) · 4.74 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
import logging
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
import NUSBot as nus
from Group import Group
from Student import Student
import Error as e
import LoadData
import pickle
from datetime import datetime
API_token = '5738711983:AAE5qf6nLnmzwwoNBpNiulG0q-WkGpnHvYQ'
allGroups = LoadData.loadFile()
admin_ID = "964015471"
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
#Send a message when the command /start is issued + Creates Group object
def start(update: Updater, context: CallbackContext):
message = """Hi! Welcome to ModFriend, helping you to find a common free time with your friends!
\nPlease copy and paste your NUSMods timetable link!
\nFor help, use /help"""
update.message.reply_text(message)
group_id = getGroupID(update)
if group_id not in allGroups:
createGroup(group_id)
#Send a message when the command /help is issued
def getHelp(update: Updater, context: CallbackContext):
message = """Step 1: Go to www.nusmods.com/timetable
\nStep 2: After entering all of your modules, click share/sync and copy and paste the given link onto the chat
\nStep 3: Enter /show to view all of the available timings with your friends!
\nFor any other queries, please PM @jajabonks"""
update.message.reply_text(message)
#Updates user modlink when a valid NUSMod link is sent
def updateLink(update, context):
logger.info(update)
link = getMessage(update)
group_id = getGroupID(update)
studentID = getUserID(update)
currentGroup = allGroups[group_id]
if studentID in currentGroup.users:
affectedStudent = currentGroup.users[studentID]
if affectedStudent.getNextAction() == "edit":
affectedStudent.changeLink(link)
update.message.reply_text("Link changed!")
affectedStudent.completeAction()
currentGroup.recalculateFreeTime()
else:
update.message.reply_text("Link uploaded!")
newStudent = Student(link, currentGroup, getUsername(update), studentID)
currentGroup.add_user(newStudent)
saveBackUpData()
#Send a message when the command /edit is issued
def editLink(update, context):
update.message.reply_text("Please send your new NUSMod Link!")
studentID = getUserID(update)
group_id = getGroupID(update)
if group_id in allGroups:
currentGroup = allGroups[group_id]
if studentID in currentGroup.users:
affectedStudent = currentGroup.users[studentID]
affectedStudent.addAction("edit")
#Handles errors
def errorHandler(update, context):
logger.error(msg="Something went wrong", exc_info = context.error)
message = str(context.error)
#Sends error message to group/user
context.bot.send_message(
chat_id = getGroupID(update),
text = message
)
context.bot.send_message(
chat_id = admin_ID,
text = message
)
def getMessage(update):
return update.message.text
def getUsername(update):
return update.message.from_user.username
def getUserID(update):
try:
return update.message.from_user.id
except:
raise e.UserError
def getFreeTime(update, context):
group_id = getGroupID(update)
if group_id not in allGroups:
raise e.GroupError
currentGroup = allGroups[group_id]
context.bot.send_message(chat_id = update.effective_chat.id, text= currentGroup.showFreeTime())
def createGroup(group_id):
newGroup = Group(group_id)
allGroups[group_id] = newGroup
return newGroup
def getGroupID(update):
try:
return update.message.chat.id
except:
raise e.GroupError
def saveBackUpData():
currentTime = datetime.now()
currentTimeString = currentTime.strftime("%m%d%Y")
with open(f"modFriendsData{currentTimeString}.pkl", 'wb') as f:
pickle.dump(allGroups, f)
#Start the bot
def main():
# Create the Updater and pass it your bot's token.
updater = Updater(API_token, use_context=True)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", getHelp))
dp.add_handler(CommandHandler("show", getFreeTime))
dp.add_handler(CommandHandler("edit", editLink))
# on noncommand i.e message - echo the message on Telegram
dp.add_handler(MessageHandler(Filters.regex("^https:\/\/nusmods.com/timetable/sem-(1|2)/share?"), updateLink))
# log all errors
dp.add_error_handler(errorHandler)
# Start the Bot
updater.start_polling(drop_pending_updates=True)
updater.idle()
if __name__ == '__main__':
main()