def get(self, bot, update): """Get the newest hetzner offers.""" session = get_session() subscriber = self.get_or_create_subscriber(session, update.message.chat_id) self.process(bot, subscriber, session=session, get_all=True) session.remove()
def process_all(self, bot, job): """Check for every subscriber.""" session = get_session() subscribers = session.query(Subscriber) \ .filter(Subscriber.active == True) \ .all() for subscriber in subscribers: self.process(bot, subscriber, session=session) session.remove()
def stop(self, bot, update): """Stop the bot.""" session = get_session() chat_id = update.message.chat_id subscriber = self.get_or_create_subscriber(session, chat_id) subscriber.active = False session.add(subscriber) session.commit() session.remove() text = "You won't receive any more offers." bot.sendMessage(chat_id=chat_id, text=text)
def start(self, bot, update): """Start the bot.""" session = get_session() chat_id = update.message.chat_id subscriber = self.get_or_create_subscriber(session, chat_id) subscriber.active = True session.add(subscriber) session.commit() session.remove() text = 'You will now receive offers. Type /help for more info.' bot.sendMessage(chat_id=chat_id, text=text)
def info(self, bot, update): """Get the newest hetzner offers.""" session = get_session() chat_id = update.message.chat_id subscriber = self.get_or_create_subscriber(session, chat_id) formatted = ' hd_count: {0}\n hd_size: {1} GB\n raid: {2}\n after_raid: {3} TB\n cpu_rating: {4}\n ram: {5} GB\n price: {6} Euro'.format( subscriber.hd_count, subscriber.hd_size, subscriber.raid, subscriber.after_raid, subscriber.cpu_rating, subscriber.ram, subscriber.price, ) session.remove() bot.sendMessage(chat_id=chat_id, text=formatted)
def set(self, bot, update): """Set query attributes.""" session = get_session() chat_id = update.message.chat_id subscriber = self.get_or_create_subscriber(session, chat_id) text = update.message.text field = text.split(' ')[1:] fields = [ 'hd_count', 'hd_size', 'raid', 'after_raid', 'cpu_rating', 'ram', 'price' ] if field[0] not in fields: bot.sendMessage( chat_id=chat_id, text='Invalid field. Type /help for more information') return if field[0] == 'raid': if field[1] not in ['raid5', 'raid6']: if field[1] == 'None': field[1] = None else: bot.sendMessage( chat_id=chat_id, text= 'Invalid value for "raid". Type /help for more information', ) else: try: field[1] = int(field[1]) except BaseException as e: bot.sendMessage( chat_id=chat_id, text='Value is not an int.', ) setattr(subscriber, field[0], field[1]) session.add(subscriber) session.commit() session.remove() subscriber = session.query(Subscriber).get(subscriber.id) self.process(bot, subscriber, session=session)