def post(self): try: ## start - init params sendType = "text" parser = Parser() reply_markup = [] text_msg = None text = None obj_res = None parse_mode = None request = json.loads(self.request.body) logging.info(request) if "message" in request.keys(): message = request["message"] else: message = request["callback_query"]["message"] text_msg = request["callback_query"]["data"] chat_id = str(message["chat"]["id"]) fromUser = message["from"] if message["chat"]["type"] == "group": firstname = fromUser["first_name"] lastname = fromUser["last_name"] else: firstname = message["chat"]["first_name"] lastname = message["chat"]["last_name"] ## end - init params ## response for when user left the chat if "left_chat_member" in message and message["left_chat_member"][ "username"] == "PickAFooodBot": text = "Thanks for using BusWhereRU. BB have a good day!" ## response for when user join chat elif (message["chat"]["type"] == "group" and "new_chat_member" in message and message["new_chat_member"]["username"] == "PickAFooodBot") or (message["text"] == "/start"): text = """HELLOOO Thank you for adding BusWhereRU bot. """ ## repsonse for user commands else: if not text_msg: text_msg = message["text"] if not text_msg: text = "Invalid command" if "/stop" in text_msg: if memcache.get("{}".format(chat_id)) == "stop": text = "Waiting for your bus stop number..." else: text = "Please tell me the bus stop number." command = "stop" memcache.set("{}".format(chat_id), command) elif "/list" in text_msg: command = "list" memcache.set("{}".format(chat_id), command) user = User.by_telegramID(chat_id).get() if user: if len(user.busStopList) == 0: text = "You did not save any bus stop." else: text = "Your saved bus stop codes:" sendType = "button" reply_markup = [ parser.parseDataAsButton(user.busStopList) ] else: text = "You did not save any bus stop." elif "/save" in text_msg: if memcache.get("{}".format(chat_id)) == "save": text = "Waiting for your bus number to save..." else: text = "Please tell me the bus stop code to save :)" command = "save" memcache.set("{}".format(chat_id), command) elif "/alarm" in text_msg: if memcache.get("{}".format(chat_id)) == "alarm1": text = "Waiting for your bus stop number to set alarm..." else: text = "1. Please tell me the bus stop code to set alarm." command = "alarm1" memcache.set("{}".format(chat_id), command) elif "/remove" in text_msg: if memcache.get("{}".format(chat_id)) == "remove1": text = "Waiting for your selection for bus number..." else: command = "remove" memcache.set("{}".format(chat_id), command) user = User.by_telegramID(chat_id).get() if user: if len(user.busStopList) == 0: text = "You did not save any bus stop." else: text = "Choose which bus stop code to remove:" sendType = "button" reply_markup = [ parser.parseDataAsButton(user.busStopList) ] else: text = "You did not save any bus stop." elif "/location" in text_msg: location_data = memcache.get("{}_location".format(chat_id)) if location_data: memcache.set('{}_data'.format(chat_id), location_data) command = "location" memcache.set("{}".format(chat_id), command) text = "Choose available buses:" sendType = "button" reply_markup = parser.parseButtonRow( parser.parseDataAsButton(location_data.keys())) else: text = "Invalid command" else: command = memcache.get("{}".format(chat_id)) # logging.info('command: ' + command) try: text_msg = int(text_msg) except: text = "Invalid bus stop/number" if command == "stop" or command == "list": contents = callDataMall(text_msg) found, text = parser.parseBusStopInfo(contents) parse_mode = "html" sendType = "button" if found: reply_markup = [ parser.parseDataAsButtonDict( {"/location": "SHOW LOCATION"}) ] ## save location data for when user click on show location location_data = parser.getLocationData(contents) memcache.set('{}_location'.format(chat_id), location_data) if command == "save": # save bus stop to user user = User.by_telegramID(chat_id).get() if user: # check if new bus stop is in the list, if not add new stop code if text_msg in user.busStopList: text = "Seems like you have already saved this bus stop code" sendType = "button" reply_markup = [ parser.parseDataAsButton(user.busStopList) ] else: ## check if bus stop code to add is valid contents = callDataMall(text_msg) if contents["Services"]: user.add_to_list(text_msg) user.put() text = "Successfully saved bus stop" sendType = "button" reply_markup = [ parser.parseDataAsButton( user.busStopList) ] else: text = "Invalid bus stop code" else: contents = callDataMall(text_msg) if contents["Services"]: ## create user if this chat did not exist before user = User(telegramID=chat_id, firstname=firstname, lastname=lastname) user.add_to_list(text_msg) user.put() text = "Successfully saved bus stop." sendType = "button" reply_markup = [ parser.parseDataAsButton(user.busStopList) ] else: text = "Invalid bus stop code" if command == "remove": try: user = User.by_telegramID(chat_id).get() if user: user.remove_from_list(text_msg) user.put() text = "Successfully removed bus stop code" + str( text_msg) + "." if len(user.busStopList) == 0: sendType = "text" text += " No bus stop available." else: sendType = "button" reply_markup = [ parser.parseDataAsButton( user.busStopList) ] else: text += "You did not save any bus stop." except Exception, e: logging.error(e) text = "Invalid bus stop code" if "alarm" in command: if command == "alarm1": ## set busStop code in cache contents = callDataMall(text_msg) if contents["Services"]: alarm_data = {'alarm1': text_msg} memcache.set('{}_data'.format(chat_id), alarm_data) command = "alarm2" memcache.set("{}".format(chat_id), command) text = "Successfully set bus stop as " + str( text_msg) + "\n" text += "2. Please tell me the time you should reach the bus stop." else: text = "Invalid bus stop code" elif command == "alarm2": parsedTime = parser.checkAlarmTimeData(text_msg) if parsedTime: alarm_data = memcache.get( '{}_data'.format(chat_id)) alarm_data["alarm2"] = parsedTime memcache.set('{}_data'.format(chat_id), alarm_data) command = "alarm3" memcache.set("{}".format(chat_id), command) text = "Successfully set time to" + str( text_msg) + "\n" text += "3. Please tell me amount of time you need to be at bus stop." else: text = "Invalid time" elif command == "alarm3": try: interval = int(text_msg) alarm_data = memcache.get( '{}_data'.format(chat_id)) parser.setAlarmTaskQueue( alarm_data, interval, chat_id) ## clear command memcache.set("{}".format(chat_id), '') text = "Successfully set buffer time to: " + str( text_msg) except Exception, e: text = "Invalid minutes" if command == "location": location_data = memcache.get( "{}_location".format(chat_id)) sendType = "location" text = "location" obj_res = location_data[str(text_msg)]["location"]