def add_notification(): user = request.args.get('user') pokemon = request.args.get('pokemon') if user is not None and pokemon is not None: notification = Notifications(user, pokemon) notif = notification.insert_into_db() return jsonify(success=0, notification=notif) return jsonify(success=1, error='check args')
def get_notification(): user = request.args.get('user') if user is not None: return jsonify(success=0, notifications=Notifications.get_users_notifs( float(user))) return jsonify(success=1, error='check args')
def toggle_comment_like(request, sse, comment_id): comment = db_session.query(EventComments).filter_by(id=comment_id).first() if not comment: return jsonify(error=True, message='comment not found') like = db_session.query(CommentLikes) \ .filter_by(comment_id = comment_id) \ .filter_by(owner_id = user_session['account_id']) \ .first() if like: db_session.delete(like) check_notification = db_session.query(Notifications) \ .filter(Notifications.action == ACTION_TYPES['COMMENT_LIKE']) \ .filter(Notifications.target_type == TARGET_TYPES['COMMENT']) \ .filter(Notifications.target_id == like.comment_id) \ .filter(Notifications.from_id == user_session['account_id']) \ .filter(Notifications.account_id == like.comment_rel.owner_id) \ .first() if check_notification: db_session.delete(check_notification) db_session.commit() return jsonify(message='unliked', liked=False) else: like = CommentLikes(comment_id=comment_id, owner_id=user_session['account_id']) db_session.add(like) db_session.commit() if like.comment_rel.owner_id != user_session['account_id']: you = db_session.query(Accounts).filter_by( id=user_session['account_id']).one() message = you.username + ' liked your comment: ' + like.comment_rel.text new_notification = Notifications( action=ACTION_TYPES['COMMENT_LIKE'], target_type=TARGET_TYPES['COMMENT'], target_id=like.comment_id, from_id=user_session['account_id'], account_id=like.comment_rel.owner_id, message=message, link='/event/' + str(like.comment_rel.event_rel.id)) db_session.add(new_notification) db_session.commit() sse.publish( { "message": message, "for_id": like.comment_rel.owner_id }, type='action') return jsonify(message='liked', liked=True)
def intent_targetword_handler(clova_request): cek_response = None notify_message = None user_id = None try: print("targetWordIntent request") user_id = clova_request.user_id print(f'CEK userId: {user_id}') aobj = AccessTokens.query.filter_by(user_id=user_id).one_or_none() target = clova_request.slots_dict["target"] notify_message, cek_response = make_response_tweet_summarize( target=target, num=1, token=aobj) #raise LineBotApiError(status_code=400, error=BaseError("debug")) # DEBUG line_bot_api.push_message(user_id, TextSendMessage(text=notify_message)) return cek_response except LineBotApiError as e: nobj = Notifications(user_id=user_id, message=notify_message) db.session.add(nobj) db.session.commit() print(str(e)) return cek_response except Exception as e: print(str(e)) return clova.response("ごめんなさい。知らない単語です。")
def intent_next_handler(clova_request): cek_response = None notify_message = None user_id = None try: print("nextIntent request") user_id = clova_request.user_id print(f'CEK userId: {user_id}') aobj = AccessTokens.query.filter_by(user_id=user_id).one_or_none() target = clova_request.session_attributes["target"] num = int(clova_request.session_attributes["num"]) + 1 notify_message, cek_response = make_response_tweet_summarize( target=target, num=num, token=aobj) line_bot_api.push_message(user_id, TextSendMessage(text=notify_message)) return cek_response except LineBotApiError as e: nobj = Notifications(user_id=user_id, message=notify_message) db.session.add(nobj) db.session.commit() print(str(e)) return cek_response except Exception as e: print(str(e)) return clova.response("私に話しかけるときは、なになにについて教えて、と話しかけてください。")
def finish(self): # set last update self.channel.last_update = datetime.date.today() self._save_channel() new_bots = Bot.select_new_bots() if not self.silent and len(new_bots) > 0: self.notify_admin("Sending notifications to subscribers...") subscribers = Notifications.select().where(Notifications.enabled == True) notification_count = 0 for sub in subscribers: try: util.send_md_message(self.bot, sub.chat_id, messages.BOTLIST_UPDATE_NOTIFICATION.format( n_bots=len(new_bots), new_bots=Bot.get_new_bots_markdown())) notification_count += 1 sub.last_notification = datetime.date.today() sub.save() except TelegramError: pass self.sent['notifications'] = "Notifications sent to {} users.".format( notification_count) changes_made = len(self.sent) > 1 or len(self.sent['category']) > 0 if changes_made: text = util.success('{}{}'.format('BotList updated successfully:\n\n', mdformat.results_list(self.sent))) else: text = mdformat.none_action("No changes were necessary.") log.info(self.sent) self.bot.formatter.send_or_edit(self.chat_id, text, to_edit=self.message_id)
def test(request): if request.method == 'POST': if request.POST.get('twilioNumber'): twilioUser = request.POST.get('twilioNumber') print twilioUser number = Notifications(number=str(twilioUser)) number.save() return render(request, 'app/page.html') else: username = request.POST.get('username') repo = request.POST.get('repo') service = request.POST.get('service') data = issues.batchTasks(username, repo, service) client.messages.create( to="+13473282978", from_="+13473259195", body="David, you have to present at Hack battle soon!", ) # Store in database if service == 'github': for datum in data: print datum if 'due' in datum: entry = Entries(due=datum['due'], title=datum['title'], username=datum['username']) entry.save() else: entry = Entries(due='', title=datum['title'], username=datum['username']) entry.save() if service == 'bitbucket': for datum in data: entry = BitbucketEntries(due='', title=datum['title'], username=datum['username']) entry.save() return HttpResponse(json.dumps(data)); else: print 'Not a post request?' return render(request, 'app/page.html')
def create_event_comment(request, sse, event_id): event = db_session.query(Events).filter_by(id=event_id).first() if not event: return jsonify(error=True, message='event not found') data = json.loads(request.data) if not data: return jsonify(error=True, message='request body is empty, check headers/data') if 'text' not in data: return jsonify(error=True, message='no text key/value pair in request body') text = str(data['text']).encode() new_comment = EventComments(event_id=event_id, owner_id=user_session['account_id'], text=text) db_session.add(new_comment) if event.host_id != user_session['account_id']: you = db_session.query(Accounts).filter_by( id=user_session['account_id']).one() message = you.username + ' commented on your event(' + event.title[: 10] + '...' + '): ' + text[: 10] + '...' new_notification = Notifications(action=ACTION_TYPES['NEW_COMMENT'], target_type=TARGET_TYPES['EVENT'], target_id=event.id, from_id=user_session['account_id'], account_id=event.host_id, message=message, link='/event/' + str(event.id)) db_session.add(new_notification) sse.publish({ "message": message, "for_id": event.host_id }, type='action') db_session.commit() return jsonify(message='event comment created', comment=new_comment.serialize)
def set_notifications(bot, update, value: bool): cid = update.effective_chat.id try: notifications = Notifications.get(Notifications.chat_id == cid) except Notifications.DoesNotExist: notifications = Notifications(chat_id=cid) notifications.enabled = value notifications.save() Statistic.of(update, ('enabled' if value else 'disabled') + ' notifications for their group {}'.format(cid)) msg = util.success("Nice! Notifications enabled." ) if value else "Ok, notifications disabled." msg += '\nYou can always adjust this setting with the /subscribe command.' bot.formatter.send_or_edit(cid, msg, to_edit=util.mid_from_update(update)) return ConversationHandler.END
def accept_booking_request(request, sse, event_id, sender_id): try: # check given data your_id = user_session['account_id'] if your_id == sender_id: response = '''sender_id is same as current session\'s id. The sender cannot accept/decline a request, only withdraw a request they sent''' return jsonify(error=True, message=response) event = db_session.query(Events).filter_by(id=event_id).first() if event == None: return jsonify(error=True, message='no event found by id: ' + str(event_id)) if event.host_id != your_id and event.host_id != sender_id: return jsonify(error=True, message='none of the two accounts own this event') booking_request = db_session.query(EventRequests) \ .filter(EventRequests.event_id == event_id) \ .filter(EventRequests.sender_id == sender_id) \ .filter(EventRequests.receiver_id == your_id) \ .first() if not booking_request: response = '''booking request does not exist. maybe it was withdrawn by sender''' return jsonify(error=True, message=response, booking_request=False) if user_session['account_type'] == 'ARTIST': # VENUE requested you new_performer = EventPerformers(event_id=event_id, performer_id=your_id) if user_session['account_type'] == 'VENUE': # ARTIST requested you new_performer = EventPerformers(event_id=event_id, performer_id=sender_id) db_session.delete(booking_request) db_session.add(new_performer) if your_id != sender_id: you = db_session.query(Accounts).filter_by(id=your_id).one() text = you.username + ' accepted your request to perform at event: ' + event.title new_notification = Notifications( action=ACTION_TYPES['BOOKED'], target_type=TARGET_TYPES['ACCOUNT'], target_id=sender_id, from_id=your_id, account_id=sender_id, message=text, link='/event/' + str(event.id)) db_session.add(new_notification) sse.publish({"message": text, "for_id": sender_id}, type='action') # commit everything: booking request and notification db_session.commit() return jsonify(message='request accepted!', booking=new_performer.serialize) except Exception as err: print(err) return jsonify(error=True, errorMessage=str(err), message='error processing...')
def send_booking_request(request, sse, event_id, receiver_id): try: # check given data your_id = user_session['account_id'] if receiver_id == your_id: return jsonify( error=True, message= 'forbidden: provided receiver_id is equal to current account_id' ) event = db_session.query(Events).filter_by(id=event_id).first() if event == None: return jsonify(error=True, message='no event found by id: ' + str(event_id)) if event.host_id != your_id and event.host_id != receiver_id: return jsonify(error=True, message='none of the two accounts own this event') if user_session['account_type'] == 'ARTIST': booking = db_session.query(EventPerformers) \ .filter(EventPerformers.event_id == event_id) \ .filter(EventPerformers.performer_id == your_id) \ .first() else: booking = db_session.query(EventPerformers) \ .filter(EventPerformers.event_id == event_id) \ .filter(EventPerformers.performer_id == receiver_id) \ .first() if booking: return jsonify(error=True, message='booking exists', booked=True, booking=booking.serialize) # check if booking request and notification already exists booking_request = db_session.query(EventRequests) \ .filter(EventRequests.event_id == event_id) \ .filter( (EventRequests.sender_id == your_id) | (EventRequests.receiver_id == your_id) ) \ .filter( (EventRequests.sender_id == receiver_id) | (EventRequests.receiver_id == receiver_id) ) \ .first() if booking_request: response = '''Booking request already exists.''' return jsonify(error=True, message=response, booking_request_exists=True, booking_request=booking_request.serialize) check_notification = db_session.query(Notifications) \ .filter(Notifications.action == ACTION_TYPES['CANCEL_BOOKING_REQUEST']) \ .filter(Notifications.target_type == TARGET_TYPES['EVENT']) \ .filter(Notifications.target_id == event_id) \ .filter(Notifications.from_id == your_id) \ .filter(Notifications.account_id == receiver_id) \ .first() if check_notification: db_session.delete(check_notification) # create booking request new_booking_request = EventRequests(sender_id=your_id, receiver_id=receiver_id, event_id=event_id) # create notification / push event if receiver_id != your_id: you = db_session.query(Accounts).filter_by(id=your_id).one() account = db_session.query(Accounts).filter_by( id=receiver_id).one() if you.type == 'ARTIST' and account.type == 'VENUE': text = you.username + ' wants to perform at your event: ' + event.title new_booking_request.message = you.username + ' wants to perform at your event' if you.type == 'VENUE' and account.type == 'ARTIST': text = you.username + ' wants you to perform at an event: ' + event.title new_booking_request.message = you.username + ' wants you to perform at an event' new_notification = Notifications( action=ACTION_TYPES['REQUEST_BOOKING'], target_type=TARGET_TYPES['EVENT'], target_id=event.id, from_id=your_id, account_id=receiver_id, message=text, link='/event/' + str(event.id)) db_session.add(new_notification) db_session.add(new_booking_request) sse.publish({ "message": text, "for_id": receiver_id }, type='action') # commit everything: booking request and notification db_session.commit() return jsonify(message='booking request sent!', booking_request_exists=True, new_booking_request=new_booking_request.serialize) except Exception as err: print(err) return jsonify(error=True, errorMessage=str(err), message='error processing...')
def send_account_message(request, sse, account_id): try: your_id = user_session['account_id'] if account_id == your_id: return jsonify( error=True, message='account_id provided is same as current user.') data = json.loads(request.data) if not data: return jsonify(error=True, message='request body is empty, check headers/data') if 'message' not in data: return jsonify(error=True, message='no message key/value pair in request body') conversation = db_session.query(Conversations) \ .filter( (Conversations.account_A_id == your_id) | (Conversations.account_B_id == your_id) ) \ .filter( (Conversations.account_A_id == account_id) | (Conversations.account_B_id == account_id) ) \ .first() if conversation == None: conversation = Conversations(account_A_id=your_id, account_B_id=account_id) db_session.add(conversation) db_session.commit() conversation.last_updated = func.now() db_session.add(conversation) message = str(data['message']).encode() conversation_messge = ConversationMessages( conversation_id=conversation.id, owner_id=your_id, message=message) db_session.add(conversation_messge) if account_id != your_id: you = db_session.query(Accounts).filter_by(id=your_id).one() text = you.username + ' sent you a message' new_notification = Notifications( action=ACTION_TYPES['NEW_MESSAGE'], target_type=TARGET_TYPES['ACCOUNT'], target_id=account_id, from_id=your_id, account_id=account_id, message=text, link='/accounts/' + str(you.username)) db_session.add(new_notification) sse.publish({"message": text, "for_id": account_id}, type='action') db_session.commit() sse.publish( { "message": text, "conversation_message": conversation_messge.serialize }, type='message') return jsonify(message='message sent', new_message=conversation_messge.serialize) except Exception as err: print(err) return jsonify(error=True, errorMessage=str(err), message='error processing...')
def toggle_account_follow(request, sse, account_id): account = db_session.query(Accounts).filter_by(id=account_id).first() if not account: return jsonify(error=True, message='account not found') if account_id == user_session['account_id']: return jsonify( error=True, message= 'provided account_id is same as session account\'s id: accounts cannot follow themselves.' ) follow = db_session.query(Follows) \ .filter_by(account_id = user_session['account_id']) \ .filter_by(follows_id = account_id) \ .first() if follow: db_session.delete(follow) check_notification = db_session.query(Notifications) \ .filter(Notifications.action == ACTION_TYPES['ACCOUNT_FOLLOW']) \ .filter(Notifications.target_type == TARGET_TYPES['ACCOUNT']) \ .filter(Notifications.target_id == account_id) \ .filter(Notifications.from_id == user_session['account_id']) \ .filter(Notifications.account_id == account_id) \ .first() if check_notification: db_session.delete(check_notification) db_session.commit() return jsonify(message='unfollowed', following=False) else: follow = Follows(account_id=user_session['account_id'], follows_id=account_id) db_session.add(follow) if account_id != user_session['account_id']: you = db_session.query(Accounts).filter_by( id=user_session['account_id']).one() message = you.username + ' started following you' new_notification = Notifications( action=ACTION_TYPES['ACCOUNT_FOLLOW'], target_type=TARGET_TYPES['ACCOUNT'], target_id=account_id, from_id=user_session['account_id'], account_id=account_id, message=message) db_session.add(new_notification) sse.publish({ "message": message, "for_id": account_id }, type='action') db_session.commit() return jsonify(message='followed', following=True)
def toggle_event_attending(request, sse, event_id): if user_session['account_type'] != 'USER': return jsonify(error=True, message='current account is not of type: USER') event = db_session.query(Events).filter_by(id=event_id).first() if not event: return jsonify(error=True, message='event not found') attending = db_session.query(EventAttendees) \ .filter_by(event_id = event_id) \ .filter_by(account_id = user_session['account_id']) \ .first() if attending: db_session.delete(attending) check_notification = db_session.query(Notifications) \ .filter(Notifications.action == ACTION_TYPES['EVENT_ATTENDING']) \ .filter(Notifications.target_type == TARGET_TYPES['EVENT']) \ .filter(Notifications.target_id == attending.event_id) \ .filter(Notifications.from_id == user_session['account_id']) \ .filter(Notifications.account_id == attending.event_rel.host_id) \ .first() if check_notification: db_session.delete(check_notification) db_session.commit() return jsonify(message='not attending', attending=False) else: attending = EventAttendees(event_id=event_id, account_id=user_session['account_id']) db_session.add(attending) db_session.commit() if attending.event_rel.host_id != user_session['account_id']: you = db_session.query(Accounts).filter_by( id=user_session['account_id']).one() message = you.username + ' is attending your event: ' + attending.event_rel.title new_notification = Notifications( action=ACTION_TYPES['EVENT_ATTENDING'], target_type=TARGET_TYPES['EVENT'], target_id=attending.event_id, from_id=user_session['account_id'], account_id=attending.event_rel.host_id, message=message, link='/event/' + str(event_id)) db_session.add(new_notification) db_session.commit() sse.publish( { "message": message, "for_id": attending.event_rel.host_id }, type='action') return jsonify(message='attending', attending=True)