def projects(self, user: User): projects = ' '.join( ['#' + project['name'] for project in user.api.projects.all()]) if projects: user.send_message('Your projects: ' + projects) else: user.send_message('You don\'t have any projects!')
def labels(self, user: User): labels = ' '.join( ['@' + label['name'] for label in user.api.labels.all()]) if labels: user.send_message('Your labels: ' + labels) else: user.send_message('You don\'t have any labels!')
def wrap(self, _, update, *args, **kwargs): assert isinstance(User.query, Query) assert isinstance(update.message, Message) tguser = update.message.from_user assert isinstance(tguser, TgUser) user = User.query.filter(User.tg_id == tguser.id).one_or_none() now = datetime.now() if not user: user = User( tg_id=tguser.id, first_name=tguser.first_name, last_name=tguser.last_name or '', username=tguser.username, created_at=now, last_active_at=now, ) db.session.add(user) db.session.commit() else: user.first_name = tguser.first_name user.last_name = tguser.last_name or '' user.username = tguser.username user.last_active_at = now user.is_active = True user.update = update user.message = update.message try: func(self, user, *args, **kwargs) except Flow: pass db.session.commit()
def base_welcome(self, user: User): projects = ' '.join( ['#' + project['name'] for project in user.api.projects.all()]) labels = ' '.join( ['@' + label['name'] for label in user.api.labels.all()]) text = 'You were succesfully authorized!' if projects or labels: text += '\n' if projects: text += '\nProjects: ' + projects if labels: text += '\nLabels: ' + labels text += '\n\nNow you can send me a task that you want to save for later...' user.send_message(text)
def any_text(self, user: User): item = user.api.quick.add(user.message.text) if not item: user.send_message('Could not create task') return labels = item['labels'] if labels: labels = [ '@' + label.data['name'] for label in user.api.labels.all() if label.data['id'] in labels ] answer = 'Task added:\n{} {} {}'.format(item['content'], ' '.join(labels), item['date_string'] or '') user.send_message(answer)
def wrap(self, user: User): user.init_api() if not user.auth: assert isinstance(self, MyBot), self user.state = str(uuid4()) db.session.commit() with app.app_context(): url = url_for('todoist.need_auth', state=user.state, _external=True) user.send_message( 'You need to authorize: {}\n'.format(url) + 'We promise, that we will not share you private information with anyone, or look at it by ourselves.\n' 'You can check it open source code of this bot: {}'.format( app.config['PROJECT_REPOSITORY_LINK'])) raise Stop func(self, user)
def wrap(self, user: User): while True: if not user.message or not user.message.text or not str( user.message.text).startswith('/start '): break param = str(user.message.text).split(' ', 1)[1] if not param.startswith('error_'): break with app.app_context(): if not user.state: user.state = str(uuid4()) url = url_for('todoist.need_auth', state=user.state, _external=True) if param == 'error_access_denied': user.send_message( 'If you want to use bot you have to grant us access to your Todoist account: {}\n' .format(url) + 'We care about your privacy!') elif param == 'error_invalid_scope': user.send_message('Shit happens...\n' 'Please, try again: {}\n'.format(url)) raise Stop break func(self, user)
def notification(self, user: User, item: Item): text = '{} {}'.format(emoji.DOUBLE_RED_EXCLAMATION, item.data['content']) return user.send_message(text)
def error(self, user: User, error): user.send_message('Error occurred') logging.warning('Error occurred: {}'.format(error))
def any_other(self, user: User): user.send_message('This type of content is not supported.')
def stop(self, user: User): user.send_message( 'We will not send you anything again until you send a message me.') user.is_active = False
def start(self, user: User): user.send_message( 'You are authorized! Everything is okey!\n' 'Now you can create new task just by writing it to me...')