def get(self): proj_handler = ProjectsHandler(self.request, self.response) projects = ProjectsHandler.get_project_list(proj_handler) result = [] if projects: colors = ["event-important", "event-success", "event-warning", "event-info", "event-inverse", "event-special"] for proj in projects: color = colors[randint(0, len(colors)-1)] events = Event.query(ancestor=proj.key).fetch() if events: for event in events: json_event = {} json_event['id'] = proj.key.urlsafe() json_event['title'] = str(event.title) json_event['url'] = '/projects/' + proj.key.urlsafe() json_event['class'] = color ''' Calendar.js sets date in utc format only, so i have to substract millisec of 2 hours [(60*60*2 = 7200)*1000] in order to be consistent with application time''' json_event['start'] = str(int(time.mktime(event.start_time.timetuple()) * 1000) - 7200000) json_event['end'] = str(int(time.mktime(event.end_time.timetuple()) * 1000) - 7200000) result.append(json_event) result = json.dumps(result) self.render_template("calendar.html", {'events_list': result})
def post(self): urlfetch.set_default_fetch_deadline(60) body = json.loads(self.request.body) logging.info('request body:') logging.info(body) self.response.write(json.dumps(body)) update_id = body['update_id'] message = body['message'] message_id = message.get('message_id') date = message.get('date') text = message.get('text') fr = message.get('from') chat = message['chat'] chat_id = chat['id'] if not text: logging.info('no text') return def proxy_announce(msg=None): if (msg is not '' and msg is not None and msg is not ' '): resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({ 'chat_id': -35532931, 'text': msg.encode('utf-8'), 'disable_web_page_preview': 'true', })).read() def broadcast(msg=None): resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({ 'chat_id': str(chat_id), 'text': msg.encode('utf-8'), 'disable_web_page_preview': 'true' })).read() def reply(msg=None, img=None): if msg: resp = urllib2.urlopen(BASE_URL + 'sendMessage', urllib.urlencode({ 'chat_id': str(chat_id), 'text': msg.encode('utf-8'), 'disable_web_page_preview': 'true', 'reply_to_message_id': str(message_id), })).read() elif img: resp = multipart.post_multipart(BASE_URL + 'sendPhoto', [ ('chat_id', str(chat_id)), ('reply_to_message_id', str(message_id)), ], [ ('photo', 'image.jpg', img), ]) else: logging.error('No message or image specified') resp = None logging.info('send response:') logging.info(resp) if text.startswith('/'): # Bot-recognised commands if (text.lower() == '/start'): reply('Bot enabled') setEnabled(chat_id, True) elif (text.lower() == '/stop'): reply('Bot disabled') setEnabled(chat_id, False) # Help command elif (text.lower() == '/help'): help_msgs = ['/reg or /register - Registers your username into the system\n', '/users - Displays all the registered users of the system\n', '/bus - Shows you a list of bus stops and the arrival timings for the buses\n', '/events - Shows you a list of current / upcoming events in Tembusu College'] to_print_str = '' for each_msg in help_msgs: to_print_str += each_msg reply(to_print_str) ########## For registration related cases # To register a new user elif (text.lower() == '/register' or text.lower() == '/reg'): CONST_ERROR_ALR_REG = 0 CONST_OPTION_REG_SUCCESS = 1 CONST_RESPONSE = Register.signUp(fr['first_name'], fr['id']) if (CONST_RESPONSE == CONST_ERROR_ALR_REG): reply('You are already registered!') return elif (CONST_RESPONSE == CONST_OPTION_REG_SUCCESS): reply('Thank you ' + str(fr['first_name']) + ' for registering!') return else: reply('There was an error trying to register you. Please try again, or contact the admin.') return # To check for users that have already registered elif (text.lower() == '/users' or text.lower() == '/user'): list_of_users = Register.getAllUsers() reply(list_of_users) ########## End of registration related cases ########## For bus-related cases elif (text.lower() == '/bus'): bot_msg = 'Please select a bus stop:' bus_stops = ['/newtownsecsch', '/oppbuona'] Keyboard.one_time_btn_keyboard(Utils.arrSeparator(bus_stops), chat_id, message_id, bot_msg) return elif (text.lower() == '/newtownsecsch'): path = 'ltaodataservice/BusArrival?BusStopID=19051&SST=True' buses_at_stop = Bus.getBusNums(path, '19051', fr['id']) output_msg = 'Please select a bus:' Keyboard.one_time_btn_keyboard(Utils.arrSeparator(buses_at_stop), chat_id, message_id, output_msg) return elif (text.lower() == '/oppbuona'): path = 'ltaodataservice/BusArrival?BusStopID=11369&SST=True' buses_at_stop = Bus.getBusNums(path, '11369', fr['id']) output_msg = 'Please select a bus:' Keyboard.one_time_btn_keyboard(Utils.arrSeparator(buses_at_stop), chat_id, message_id, output_msg) return elif ('/bus' in text.lower()): CONST_ERROR_BUS_STOP_NAN = 0 # This error ID is if the bus stop number is NaN #CONST_ERROR_BUS_NUM_NAN = 2 # This error ID is if the bus number is NaN CONST_OPTION_GET_BUSES = 1 # This option ID is for getting a list of buses for a given bus stop CONST_OPTION_GET_BUS_TIMING = 3 # This option ID is for getting the bus arrival timing for a given bus at a given bus stop CONST_OPTION_GET_SHORTCUT = 4 # This option ID is for getting the bus timings for favourited buses option_id = Bus.busTextParser(text.lower()) if (option_id == CONST_ERROR_BUS_STOP_NAN): error_msg_reply = 'Please enter a bus stop NUMBER.' Keyboard.norm_keyboard_reply(chat_id, message_id, error_msg_reply) return # Removed because there may be bus numbers that contain alphabets, e.g. 70M #elif (option_id == CONST_ERROR_BUS_NUM_NAN): # error_msg_reply = 'Please enter a bus NUMBER.' # norm_keyboard_reply(error_msg_reply) # return elif (option_id == CONST_OPTION_GET_SHORTCUT): user_query = (User.query()).fetch() for q in user_query: if (q.user_id == fr['id']): # If this is the user we are looking for bus_stop_num = q.user_bus_stop_reply # Get the bus stop number this user is referring to if (bus_stop_num != 0): # If there is an actual bus stop number, i.e. NOT 0 bus_num = text.lower().split(' ', 1)[1] path = '/ltaodataservice/BusArrival?BusStopID=' + str(bus_stop_num) + '&ServiceNo=' + bus_num + '&SST=True' bus_arr_timing_msg = Bus.getBusTiming(path) # Get the bus timing Keyboard.norm_keyboard_reply(chat_id, message_id, bus_arr_timing_msg) q.user_bus_stop_reply = 0 # Reset the user's bus stop that they are referring to, to 0 q.put() # Update the Datastore return else: # If there is no actual bus stop number stored Keyboard.norm_keyboard_reply(chat_id, message_id, 'Tembotsu does not know which bus stop you are referring to, check your bus timing again.') return # If the code reaches this point, this means that the user cannot be found in our datastore # Prompt the user to register first before using str_reply = 'I\'m sorry but I can\'t find you in our database! Please type /reg to register before using the rest of the bot!' Keyboard.norm_keyboard_reply(chat_id, message_id, str_reply) return elif (option_id == CONST_OPTION_GET_BUSES): bus_stop_num = text.lower().split(' ')[1] path = '/ltaodataservice/BusArrival?BusStopID=' + bus_stop_num + '&SST=True' buses_at_stop = Bus.getBusNums(path, bus_stop_num, fr['id']) if (len(buses_at_stop) == 0): # If there are no buses at this bus stop str_reply = 'There are no buses for bus stop number: ' + str(bus_stop_num) Keyboard.norm_keyboard_reply(chat_id, message_id, str_reply) return else: # If there are buses at this bus stop output_msg = 'Please select a bus:' Keyboard.one_time_btn_keyboard(Utils.arrSeparator(buses_at_stop), chat_id, message_id, output_msg) return elif (option_id == CONST_OPTION_GET_BUS_TIMING): bus_stop_num = text.lower().split(' ')[1] bus_num = text.lower().split(' ')[2] path = '/ltaodataservice/BusArrival?BusStopID=' + bus_stop_num + '&ServiceNo=' + bus_num + '&SST=True' bus_arr_timing_msg = Bus.getBusTiming(path) Keyboard.norm_keyboard_reply(chat_id, message_id, bus_arr_timing_msg) return ########## End of bus-related cases ########## Start of Tembusu event-scraping cases elif (text.lower() == '/events'): str_reply = '' all_evts = (Event.query().order(Event.event_date)).fetch() # If there are any events to view if (len(all_evts) != 0): str_reply += 'These are the current / upcoming events in Tembusu College:\n\n' for q in all_evts: str_reply += (q.event_name + '\n' + q.event_date + '\n' + q.event_link + '\n\n') Keyboard.norm_keyboard_reply(chat_id, message_id, str_reply) return # Else if there are no events to view else: str_reply = 'There are currently no ongoing / upcoming events in Tembusu College.' Keyboard.norm_keyboard_reply(chat_id, message_id, str_reply) return ########## End of Tembusu event-scraping cases ########## Start of bot proxy announcement cases # If the first part of the text is '/a' elif (((text.lower()).split(" ", 1))[0] == '/a'): # If the ID belongs to Nelson if (fr['id'] == 22595307): if ((len((text.lower()).split(" ", 1))) > 1): to_announce = text.split(" ", 1)[1] proxy_announce(to_announce) return ########## End of bot proxy announcement cases else: reply('Please enter a valid command. Type /help for more information.') return