def replies(daysago=7): daysago = int(daysago) datesearch = d.datetime.now( pytz.timezone("America/Los_Angeles")) - d.timedelta(days=daysago) undmsgs = Message.objects(direction='Incoming', datetimesent__gt=datesearch) flash( Markup( f"<h3>Replies from the last {daysago} days</h3>or since {datesearch}.<br>" )) return render_template('msgs.html', msgs=undmsgs)
def undelivered(daysago=7): daysago = int(daysago) datesearch = d.datetime.now( pytz.timezone("America/Los_Angeles")) - d.timedelta(days=daysago) undmsgs = Message.objects(status='undelivered', datetimesent__gt=datesearch) flash( Markup( f"<h3>Undelivered messages from the last {daysago} days</h3>or since {datesearch}.<br>" )) return render_template('msgs.html', msgs=undmsgs)
def msgs(daysago=7): daysago = int(daysago) # sync Twilio to Mongo # get twilio msgs from the last two weeks datesearch = d.datetime.now( pytz.timezone("America/Los_Angeles")) - d.timedelta(days=daysago) msgs = Message.objects(datetimesent__gt=datesearch) flash( Markup( f"<h3>All messages from the last {daysago} days </h3>or since {datesearch}.<br>" )) return render_template('msgs.html', msgs=msgs, daysago=daysago)
def deletemessages(): delnum = '+15107616409' client = Client(account_sid, auth_token) client.messages.get() #delete messages from twilio to the number twmsgs = client.messages.list(to=delnum) for twmsg in twmsgs: client.messages(twmsg.sid).delete() # delete msgs from twilio from the number twmsgs = client.messages.list(from_=delnum) for twmsg in twmsgs: client.messages(twmsg.sid).delete() #delete message to and from the number on mongo msgs = Message.objects(Q(to=delnum) | Q(from_=delnum)) for msg in msgs: msg.delete() flash('I think they are all deleted') return redirect(url_for('msgs'))
def addstudtomsgs(): msgs = Message.objects() for msg in msgs: if msg.to == '+19292442593': matchPhone = msg.from_ else: matchPhone = msg.to mpac = int(matchPhone[2:5]) mpf3 = int(matchPhone[5:8]) mpl4 = int(matchPhone[8:]) query1 = Q(mobileareacode=mpac) & Q(mobilefirstthree=mpf3) & Q( mobilelastfour=mpl4) query2 = Q(otherareacode=mpac) & Q(otherfirstthree=mpf3) & Q( otherlastfour=mpl4) query3 = Q(adult1mobileareacode=mpac) & Q(adult1mobilefirst3=mpf3) & Q( adult1mobilelast4=mpl4) query4 = Q(adult2mobileareacode=mpac) & Q(adult2mobilefirst3=mpf3) & Q( adult2mobilelast4=mpl4) try: stud = User.objects.get(query1 | query2 | query3 | query4) msg.update(aeries=stud.aeries) except: mpac = matchPhone[2:5] mpf3 = matchPhone[5:8] mpl4 = matchPhone[8:] aMatchPhone = f'({mpac}) {mpf3}-{mpl4}' aquery = Q(aphone=aMatchPhone) | Q(adult1phone=aMatchPhone) | Q( adult2phone=aMatchPhone) try: astud = User.objects.get_or_404(aquery) except Exception as error: flash(f'error line 323 msgs.py {error}') else: msg.update(aeries=astud) return redirect('/')
def msg(aeriesnum, tophone=None): if session['role'].lower() == 'student': flash("Only teachers can send txt messages at the moment.") return redirect(url_for('index')) currUser = User.objects.get(pk=session['currUserId']) reloadurl = f'/msg/{aeriesnum}' student = User.objects.get(aeriesid=aeriesnum) phoneNums = [] for adult in student.adults: phoneNums.append(( adult.mobile, Markup( f'Mobile for {adult.relation} <br> {adult.fname} {adult.lname}' ))) phoneNums.append((student.mobile, Markup('Student Mobile'))) student.aphone = int(re.sub("[^0-9]", "", student.aphone)) if student.aadult1phone: student.aadult1phone = int(re.sub("[^0-9]", "", student.aadult1phone)) if student.aadult2phone: student.aadult2phone = int(re.sub("[^0-9]", "", student.aadult2phone)) phoneNums.append( (student.aphone, f'Home Phone from Aeries for {student.aadults}')) phoneNums.append((student.aadult1phone, "Parent1 phone from Aeries")) phoneNums.append((student.aadult2phone, "Parent2 phone from Aeries")) form = TxtMessageForm() if tophone: form.to.data = tophone client = Client(account_sid, auth_token) if form.validate_on_submit(): if not form.to.data: flash(f"You left the 'To' field blank {form.to.data}.") return redirect(url_for('msg', aeriesnum=aeriesnum)) if not form.body.data: flash("You left the 'Message' field blank.") return redirect( url_for('msg', aeriesnum=aeriesnum, tophone=form.to.data)) sentMsg = client.messages.create( body=form.body.data, from_='+15108043552', status_callback='https://otdata.hsoakland.tech/msgstatus', to=form.to.data) # Save sentMsg to OTData newMsg = Message(twilioid=sentMsg.sid, to=sentMsg.to, from_=f'{currUser.fname} {currUser.lname}', from_user=currUser, body=sentMsg.body, datetimesent=d.datetime.utcnow(), status=sentMsg.status, direction=sentMsg.direction, student=student) newMsg.save() form.body.data = '' msgs = Message.objects(student=student).limit(20) return render_template("msg.html", form=form, student=student, msgs=msgs, reloadurl=reloadurl, phoneNums=phoneNums)
def msgreply(): if request.user_agent == None: flash( 'You are not authorized to access that page because you have no request user agent.' ) return redirect(url_for('index')) elif 'TwilioProxy' not in request.user_agent.string: flash( 'You are not authorized to access that page because you are not Twilio.' ) flash(f'You are are {request.user_agent.string}') return redirect(url_for('index')) ''' Twilio API Reply parameters NoneCombinedMultiDict([ImmutableMultiDict([]), ImmutableMultiDict([('ToCountry', 'US'), ('ToState', 'CA'), ('SmsMessageSid', 'SMd1f0db69422d0ac2c18ada135c3b8e50'), ('NumMedia', '0'), ('ToCity', 'OAKLAND'), ('FromZip', '94608'), ('SmsSid', 'SMd1f0db69422d0ac2c18ada135c3b8e50'), ('FromState', 'CA'), ('SmsStatus', 'received'), ('FromCity', 'OAKLAND'), ('Body', 'Reply to 17th'), ('FromCountry', 'US'), ('To', '+15108043552'), ('ToZip', '94605'), ('NumSegments', '1'), ('MessageSid', 'SMd1f0db69422d0ac2c18ada135c3b8e50'), ('AccountSid', 'ACffcff72d1e02d082d4ce607412b4b5b3'), ('From', '+15107616409'), ('ApiVersion', '2010-04-01')])])Reply to 17th ''' # get the phnum that sent the reply replyFrom = request.values.get('From', None) # find the last msg sent to that person initialMessage = Message.objects(to=replyFrom).first() # find the not-student that sent the msg notifyNum = '+15107616409' try: notifyNum = f"+1{initialMessage.from_user.mobile}" reply_to = initialMessage.from_user except: # TODO hardcoded! Booo! notifyNum = '+15107616409' reply_to = User.objects.get(otemail='*****@*****.**') replyMsg = Message(twilioid=request.values.get('MessageSid', None), to=request.values.get('To', None), from_=request.values.get('From', None), body=request.values.get('Body', None), datetimesent=d.datetime.utcnow(), status=request.values.get('SmsStatus', None), direction='Incoming', student=initialMessage.student, media=request.values.get('MediaUrl0', None), reply_to=reply_to) replyMsg.save() # send notification to the person being replied to client = Client(account_sid, auth_token) client.messages.create( body= f"New Reply in OTData re {initialMessage.student.fname} {initialMessage.student.lname} From: {request.values.get('From', None)} Body: {request.values.get('Body', None)}", from_='+15108043552', status_callback='https://otdata-xzxu5z4ybq-uw.a.run.app/msgstatus', to=notifyNum) # TODO Delete this code once I know it is working if notifyNum != '+15107616409': client.messages.create( body= f"New Reply in OTData to {replyMsg.reply_to.fname} {replyMsg.reply_to.lname}", from_='+15108043552', status_callback='https://otdata-xzxu5z4ybq-uw.a.run.app/msgstatus', to='+15107616409') return f'sms reply to {reply_to.fname} {reply_to.lname} received from twilio'