def checkStar(chat_id, busStopNo): starKey = makeStarKey(chat_id) initDbList(starKey) matchingIndexes = [ i for (i, val) in enumerate(getDbObj(starKey)) if val.split("\n")[0] == busStopNo ] return matchingIndexes
def removeFromQueue(chat_id, req): qKey = makeQueueKey(chat_id) initDbList(qKey) matchingIndexes = [ i for (i, val) in enumerate(getDbObj(qKey)) if val == req ] if len(matchingIndexes) > 0: popFromDbList(qKey, matchingIndexes[0])
def checkQueueUponStart(): for key in db.scan_iter("*" + KEY_QUEUE): key = key.decode("utf-8") currQ = getDbObj(key) if len(currQ) > 0: chat_id = key.replace(KEY_QUEUE, "") sendMsg( chat_id, 'The server restarted while you had pending requests. You will not receive updates for your pending requests below, so please resend them:\n' + '\n'.join(['- `{}`'.format(item) for item in currQ])) db.delete(key)
def getSavedNag(chat_id): key = makeNagKey(chat_id) keyInfo = getDbObj(key) if keyInfo is not None: interval, maxCount = keyInfo success = True else: sendMsg(chat_id, 'Can\'t retrieve last nag interval and repeats') interval, maxCount = -1, 1 success = False return success, interval, maxCount
def saveHistory(chat_id, text): textAction = re.sub('\n.*', '', text) histKey = makeHistKey(chat_id) initDbList(histKey) matchingIndexes = [ i for (i, val) in enumerate(getDbObj(histKey)) if re.sub('\n.*', '', val) == textAction ] for i in matchingIndexes: popFromDbList(histKey, i) addToDbList(histKey, textAction + '\n' + getNowString()) trimDbList(histKey, 5)
def getSavedService(chat_id): key = makeServKey(chat_id) keyInfo = getDbObj(key) if keyInfo is not None: busStopNo, routeNo = keyInfo success = True else: sendMsg( chat_id, 'Can\'t retrieve last successfully queried bus stop and route number' ) busStopNo, routeNo = '', '' success = False return success, busStopNo, routeNo
def showFav(chat_id): favKey = makeFavKey(chat_id) initDbList(favKey) currFavs = getDbObj(favKey) if len(currFavs) == 0: reply_markup = telegram.ReplyKeyboardRemove() sendMsg(chat_id, "You have no favourite commands", reply_markup=reply_markup) else: reply_markup = telegram.ReplyKeyboardMarkup( keyboard=[[item] for item in currFavs], resize_keyboard=True, one_time_keyboard=True) sendMsg(chat_id, "Here are your favourite commands:", reply_markup=reply_markup)
def showStar(chat_id): starKey = makeStarKey(chat_id) initDbList(starKey) currStars = getDbObj(starKey) if len(currStars) == 0: reply_markup = telegram.ReplyKeyboardRemove() sendMsg(chat_id, "You have no starred bus stops", reply_markup=reply_markup) else: reply_markup = telegram.ReplyKeyboardMarkup( keyboard=[["/info {}".format(item)] for item in currStars], resize_keyboard=True, one_time_keyboard=True) sendMsg(chat_id, "Here are your starred bus stops:", reply_markup=reply_markup)
def showHist(chat_id): histKey = makeHistKey(chat_id) initDbList(histKey) history = getDbObj(histKey) if len(history) == 0: reply_markup = telegram.ReplyKeyboardRemove() sendMsg(chat_id, "You have no recent commands", reply_markup=reply_markup) else: reply_markup = telegram.ReplyKeyboardMarkup( keyboard=[[item] for item in history], resize_keyboard=True, one_time_keyboard=True) sendMsg(chat_id, "Here are your recent commands:", reply_markup=reply_markup)
def editFav(chat_id, text): textList = [item.strip() for item in text.split('|')] if (len(textList) == 2): command, action = textList command = command.lower() command = re.sub('^/', '', command) else: sendMsg(chat_id, helpText) return favKey = makeFavKey(chat_id) initDbList(favKey) matchingIndexes = [ i for (i, val) in enumerate(getDbObj(favKey)) if val == action ] if command == 'save': if len(matchingIndexes) == 0: addToDbList(favKey, action) reply_markup = telegram.ReplyKeyboardRemove() sendMsg(chat_id, "Command saved to your favourites!", reply_markup=reply_markup) else: sendMsg(chat_id, 'You have already saved this command') elif command == 'delete': if len(matchingIndexes) == 0: sendMsg(chat_id, 'No matching commands found in your favourites') else: for i in matchingIndexes: popFromDbList(favKey, i) reply_markup = telegram.ReplyKeyboardRemove() sendMsg(chat_id, "Command removed from your favourites!", reply_markup=reply_markup) else: sendMsg(chat_id, helpText)