-
Notifications
You must be signed in to change notification settings - Fork 0
/
washTestBot.py
218 lines (179 loc) · 8.11 KB
/
washTestBot.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
import schedule
import time, datetime
import telepot
from telepot.loop import MessageLoop
from telepot.namedtuple import ReplyKeyboardMarkup, KeyboardButton
from Storage import Storage
from MachineManager import MachineManager
from UserManager import UserManager
from User import User
led = 26
now = datetime.datetime.now()
nowTime = datetime.datetime.now().time()
actionWord = ""
actionWord2 = ""
useCheckNumber = ""
manager = MachineManager()
userManager = UserManager()
store = Storage("status.txt")
storeUser = Storage("user.txt")
alreadyOpened = False
lastUpdated = now
#t1 is the initial time
#t2 is the current time
def diffInTime(t1, t2):
c = t2 - t1
timeDiff = divmod(c.days * 86400 + c.seconds, 60)
return timeDiff
#Sends reminder for the clothing
def sendReminder(mArray):
print("I am here")
for i in mArray:
machineUser = i.getUser()
if machineUser != 0 and not i.getNotified():
if diffInTime(i.getTime(), datetime.datetime.now())[0] > 120:
telegram_bot.sendMessage(machineUser, "Your laundry has been in the machine for more than 2hrs! It may have already been completed")
i.alrNotified()
#convert String to Boolean
def stringToBool(s):
if s == "True":
return True
else:
return False
def action(msg):
global useCheckNumber
global actionWord2
global actionWord
global alreadyOpened
global lastUpdated
global manager
global userManager
global store
global storeUser
chat_id = msg['chat']['id']
username = msg['chat']['username']
command = msg['text']
#check if the file has already been opened
if not alreadyOpened:
store.readMachineFromStorage(manager)
storeUser.readUserFromStorage(userManager)
alreadyOpened = True
#check if the user is already in the list
if userManager.userInList(username):
print("I am in")
else:
user = User(username, chat_id)
userManager.addEntity(user)
print ('Recieved: ', command)
print (nowTime)
keyboard = ReplyKeyboardMarkup(keyboard=[['/start', '/done'], ['/use', '/status'], ["/notify", "/reset"]])
numberkeyboard = ReplyKeyboardMarkup(keyboard=[['1', '2', '3'], ['4', '5', '6'], ['7', '8'], ['/reset']])
if command != "/reset":
if actionWord == "" and actionWord2 == "":
if "/start" in command:
message = "Hi, I am wAshB, how may I help you today?\n\n"
message = message + "Here are a list of commands I have!\n"
message = message + "/start - Starts the bot and get help\n"
message = message + "/done - when done with washing machine\n"
message = message + "/use - when using washing machine\n"
message = message + "/notify - Notify the machine user\n"
message = message + "/reset - if you want to reset\n"
message = message + "/status - Check which washing machines are available\n\n"
message = message + "Look at my dp to know which number is which machine!\n"
message = message + "Your small gesture would make it more convenient for everyone in the block.\n\n"
message = message + "Happy washing!"
print(username)
if "/done" in command:
message = "Which machine is done?"
keyboard = numberkeyboard
actionWord = "done"
if "/use" in command:
message = "Which machine would you like to use?"
keyboard = numberkeyboard
actionWord = "use"
if "/status" in command:
message = manager.statusOfMachine(chat_id, lastUpdated)
if"/notify" in command:
message = "Who would you like to notify?"
keyboard = numberkeyboard
actionWord = "notify"
elif actionWord and not actionWord2:
if actionWord == "done":
machineNumber = int(command)
machInFocus = manager.getEntity(machineNumber)
if not machInFocus.isInUse():
message = "Machine is currently not in use"
else:
if(machInFocus.getUser() == chat_id):
message = "Thank you for colleting your clothes! Hope you have a great day"
machInFocus.done()
lastUpdated = datetime.datetime.now()
else:
message = "These are not your clothes!"
elif actionWord == "use":
machineNumber = int(command)
machInFocus = manager.getEntity(machineNumber)
if machInFocus.isInUse() and machInFocus.getUser() == chat_id:
message = "Your own clothes are washing! Please remember to /done first before reusing so the timer will reset :)"
elif machInFocus.isInUse():
message = "Is the machine empty and the person forgot to indicate?"
keyboard = ReplyKeyboardMarkup(keyboard=[["yes"], ["no"], ["/reset"]])
actionWord2 = "useCheck"
useCheckNumber = machineNumber
else:
message = "Remember to come back when you receive the done message"
lastUpdated = datetime.datetime.now()
machInFocus.use(chat_id, lastUpdated, False)
elif actionWord == "notify":
machineNumber = int(command)
machInFocus = manager.getEntity(machineNumber)
if not machInFocus.isInUse():
message = "Machine is currently not in use"
else:
machUser = machInFocus.getUser()
if(machUser == chat_id):
message = "This is your own clothes"
else:
telegram_bot.sendMessage(machUser, "Your clothes are done. Do collect them in the next 5 minutes as some one else may need to use it")
message = "We have notified the user, please give the user 5mins to arrive."
actionWord = ""
else:
if actionWord2 == "useCheck":
if command == "yes":
machineNumber = useCheckNumber
machInFocus = manager.getList(machineNumber)
telegram_bot.sendMessage(machInFocus.getUser(), "It seems that you are done with the machine.\n Please remember to let me know next time!", reply_markup=keyboard)
lastUpdated = datetime.datetime.now()
machInFocus.use(chat_id, lastUpdated, False)
message = "Okay, we have updated to you being the user of the machine! Remember to collect your clothes! :)"
if command == "no":
message = "These are not your clothes!"
actionWord = ""
actionWord2 = ""
keyboard = ReplyKeyboardMarkup(keyboard=[['/start', '/done'], ['/use', '/status'], ["/notify", "/reset"]])
actionWord2 = ""
telegram_bot.sendMessage(chat_id, message, reply_markup=keyboard)
else:
actionWord = ""
actionWord2 = ""
keyboard = ReplyKeyboardMarkup(keyboard=[['/start', '/done'], ['/use', '/status'], ["/notify", "/reset"]])
message = "Back to homepage!"
telegram_bot.sendMessage(chat_id, message, reply_markup=keyboard)
# print(manager.getMachList())
store.saveToStorage(manager)
storeUser.saveToStorage(userManager)
# machineStatus = [[0,0,False],[0,0, False],[0,0, False],[0,0, False],[0,0, False],[0,0,False],[0,0,False]]
# machineStatus = []
schedule.every(10).seconds.do(sendReminder, manager.getList())
testBotToken = ''
mainBotToken = ''
mainBot = telepot.Bot(mainBotToken)
testBot = telepot.Bot(testBotToken)
wAshBbot = telepot.Bot(wAshBToken)
telegram_bot = mainBot
print(telegram_bot.getMe())
MessageLoop(telegram_bot, action).run_as_thread()
print ('Up and Running')
while 1:
schedule.run_pending()
time.sleep(10)