def close_spider(self, spider): student_misc = Misc.query.filter(Misc.chatID == spider.chatID).first() try: bot.send_photo( chat_id=spider.chatID, photo=open("files/{}_attendance.png".format(spider.username), 'rb'), caption='Attendance Report for {}'.format(spider.username)) if student_misc is not None and student_misc.attendance_target is not None: target = student_misc.attendance_target no_of_lectures = int(until_x(spider.chatID, target)) if no_of_lectures > 0: messageContent = "You need to attend {} lectures to meet your target of {}%".format( no_of_lectures, target) bot.sendMessage(chat_id=spider.chatID, text=messageContent) remove('files/{}_attendance.png'.format( spider.username)) #Delete saved image mp.track(spider.username, 'Attendance') except IOError: bot.sendMessage( chat_id=spider.chatID, text= 'The bot is experiencing some issues. Please try again later.') logger.warning( "Attendance screenshot failed! Check if site is blocking us or if Splash is up." ) mp.track(spider.username, 'Error', {'type': 'Site down?'}) except TelegramError as te: logger.warning("TelegramError: {}".format(str(te))) mp.track(spider.username, 'Error', { 'type': 'TelegramError', 'error': str(te) })
def attendance_target(bot, update): """Like :func:`until_eighty`, but with user specified target attendance percentage which is stored in the ``Misc`` table. If target isn't set, asks users whether they'd like to and passes control to :py:func:`select_yn` :param bot: Telegram Bot object :type bot: telegram.bot.Bot :param update: Telegram Update object :type update: telegram.update.Update :return: SELECT_YN :rtype: int """ bot.send_chat_action(chat_id=update.message.chat_id, action='typing') student_misc = Misc.query.filter( Misc.chatID == update.message.chat_id).first() if student_misc is None: new_misc_record = Misc(chatID=update.message.chat_id) db_session.add(new_misc_record) db_session.commit() username = get_user_info(update.message.chat_id)['PID'] logger.info("Created new Misc record for {}".format(username)) student_misc = Misc.query.filter( Misc.chatID == update.message.chat_id).first() target = student_misc.attendance_target if target is None: messageContent = textwrap.dedent(""" You have not set a target yet. Would you like to set it now? You can change it anytime using /edit_target """) keyboard = [['Yes'], ['No']] reply_markup = ReplyKeyboardMarkup(keyboard) bot.sendMessage(chat_id=update.message.chat_id, text=messageContent, reply_markup=reply_markup) return SELECT_YN no_of_lectures = int(until_x(update.message.chat_id, target)) if no_of_lectures < 0: messageContent = "Your attendance is already over {}%. Maybe set it higher? Use /edit_target to change it.".format( target) bot.sendMessage(chat_id=update.message.chat_id, text=messageContent) return ConversationHandler.END messageContent = "You need to attend {} lectures to meet your target of {}%".format( no_of_lectures, target) bot.sendMessage(chat_id=update.message.chat_id, text=messageContent) return ConversationHandler.END
def until_eighty(bot, update): """Calculate number of lectures you must consecutively attend before you attendance is 80% If :py:func:`misbot.mis_utils.until_x` returns a negative number, attendance is already over 80% :param bot: Telegram Bot object :type bot: telegram.bot.Bot :param update: Telegram Update object :type update: telegram.update.Update """ bot.send_chat_action(chat_id=update.message.chat_id, action='typing') no_of_lectures = int(until_x(update.message.chat_id, 80)) if no_of_lectures < 0: bot.sendMessage(chat_id=update.message.chat_id, text="Your attendance is already over 80%. Relax.") else: messageContent = "No. of lectures to attend: {}".format(no_of_lectures) bot.sendMessage(chat_id=update.message.chat_id, text=messageContent)
def until(bot, update, args): """Like :py:func:`until_eighty` but user supplies the number. :param bot: Telegram Bot object :type bot: telegram.bot.Bot :param update: Telegram Update object :type update: telegram.update.Update :param args: User supplied arguments :type args: tuple :return: None :rtype: None """ if len(args) == 0: messageContent = textwrap.dedent(""" You must specify a number after the command to use this feature. E.g: `/until 75` """) bot.sendMessage(chat_id=update.message.chat_id, text=messageContent, parse_mode='markdown') return try: figure = float(args[0]) except (ValueError, IndexError): bot.sendMessage(chat_id=update.message.chat_id, text="You must send a number between 1-99.") return if figure > 99: bot.sendMessage(chat_id=update.message.chat_id, text="You must send a number between 1-99.") return no_of_lectures = int(until_x(update.message.chat_id, figure)) if no_of_lectures < 0: bot.sendMessage( chat_id=update.message.chat_id, text="Your attendance is already over {}%. Relax.".format(figure)) else: messageContent = "No. of lectures to attend: {}".format(no_of_lectures) bot.sendMessage(chat_id=update.message.chat_id, text=messageContent)
def close_spider(self, spider): student_misc = Misc.query.filter(Misc.chatID == spider.chatID).first() try: bot.send_photo( chat_id=spider.chatID, photo=open("files/{}_attendance.png".format(spider.username), 'rb'), caption='Attendance Report for {}'.format(spider.username)) if student_misc is not None and student_misc.attendance_target is not None: target = student_misc.attendance_target no_of_lectures = int(until_x(spider.chatID, target)) if no_of_lectures > 0: messageContent = "You need to attend {} lectures to meet your target of {}%".format( no_of_lectures, target) bot.sendMessage(chat_id=spider.chatID, text=messageContent) remove('files/{}_attendance.png'.format( spider.username)) #Delete saved image except IOError: bot.sendMessage(chat_id=spider.chatID, text='There were some errors.') logger.warning( "Attendance screenshot failed! Check if site is blocking us or if Splash is up." )