def sendMessageFromiOS(request): print 'sendMessageFromiOS' try: obj = simplejson.loads(request.raw_post_data) print obj msg = obj["message"] message_text = msg["messageText"] group = Group.objects.get(pk=obj["groupID"]) recipients = extract_recipients(request.user, group, message_text) if "error" in recipients: # send error message back to sender send_message(recipients["error"], None, user, group) data = {"errorMessage": recipients["error"]} return HttpResponse(simplejson.dumps(error_code(50, data)), mimetype='application/json') recipients = recipients["recipients"] # Create Message model instance and send out message # TODO decomp this code chunk message = Message(message_text=message_text, sender=request.user, group=group) create_ios_push_notification(message) message.save() for group_link in recipients: recipient = group_link.user message_link = MessageUserLink(message=message, recipient=recipient) message_link.save() send_message(message_text, request.user, recipient, group) data = {"messageID": message.id} return error_code(0, data) except Exception, e: print e traceback.print_exc() return error_code(1)
def sendMessageFromTwilio(request): """ View that Twilio visits when a Twilio phone number receives a SMS. Broadcasts a message sent by a user to relevant members of the group. Twilio API details: https://www.twilio.com/docs/api/twiml/sms/twilio_request """ try: obj = request.POST print obj print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' sender_phone = normalize_phone_number(obj['From']) twilio_phone = normalize_phone_number(obj['To']) message_text = obj['Body'] user_profile = get_object_or_404(UserProfile, phone_number=sender_phone) user = user_profile.user user_group_link = get_object_or_404(GroupUserLink, user=user, twilio_number=twilio_phone) group = user_group_link.group recipients = extract_recipients(user, group, message_text) if "error" in recipients: # send error message back to sender send_message(recipients["error"], None, user, group) return HttpResponse(Response(), mimetype='application/xml') recipients = recipients["recipients"] # Create Message model instance and send out the message message = Message(sender=user, group=group, message_text=message_text) create_ios_push_notification(message) message.save() for group_link in recipients: recipient = group_link.user message_link = MessageUserLink(message=message, recipient=recipient) message_link.save() send_message(message, user, recipient, group) return HttpResponse(Response(), mimetype='application/xml') except Exception, e: print e traceback.print_exc() return e