def add(message): args = message.text.split() if not is_url(args[1]): bot.send_message(message.chat.id, 'url is invalid') return with db_transaction(session): session.add(UserURL(chat_id=message.chat.id, url=args[1])) bot.send_message(message.chat.id, 'successful') logger.info("for chat_id = {0} added url = {1}".format( message.chat.id, args[1]))
def delete(message): args = message.text.split() model = session.query(UserURL).filter(UserURL.url == args[1]).first() if model is None: bot.send_message(message.chat.id, 'no such url') return with db_transaction(session): session.delete(model) bot.send_message(message.chat.id, 'successful') logger.info("for chat_id = {0} deleted url = {1}".format( message.chat.id, args[1]))
def register(message): args = message.text.split() model = session.query(User).filter(User.chat_id == message.chat.id).first() if model is not None: bot.send_message(message.chat.id, 'already registered') return model = session.query(UserToken).filter(UserToken.token == args[1]).first() if model is None: bot.send_message(message.chat.id, 'no such token') return with db_transaction(session): session.delete(model) session.add(User(chat_id=message.chat.id)) bot.send_message(message.chat.id, 'successful') logger.info("new user was registered with chat_id = {0}".format( message.chat.id))
def generate_token(message): token = str(uuid.uuid4()) with db_transaction(session): session.add(UserToken(chat_id=message.chat.id, token=token)) bot.send_message(message.chat.id, token)
logging.config.dictConfig(LOG_CONFIG) logger = logging.getLogger(__name__) if __name__ == '__main__': logger.info("starting...") models = session.query(UserURL).all() for model in models: response = requests.get(url=model.url, headers=HEADERS) logger.info("checking url = {0} for chat_id = {1}".format( model.url, model.chat_id)) m = md5() m.update(response.text.encode('utf-8')) if m.hexdigest() == model.hash: continue if model.hash is not None: logger.info("for chat_id = {0} url = {1} was changed".format( model.chat_id, model.url)) bot.send_message(model.chat_id, "url = {0} was changed".format(model.url)) model.hash = m.hexdigest() with db_transaction(session): session.add(model) logger.info('terminating...')