def recvsms(): """if it's an unsubscribe message, delete name from db, else email text to me""" if SITE['name'] != "cmc": return render_template('404.html', site=SITE), 404 user_number = request.form['From'] the_text_content = request.form['Body'] the_response = u'<html><body>Hire me please! <a \ href="http://kburke.org/projects">Here\'s some cool stuff \ I\'m working on.</a></body></html>' if the_text_content == "u" or the_text_content == "U" or \ the_text_content == "'u'": #assume USA and strip leading plus and 1 user_number = user_number[2:] import re phone_regex = str("(\()?" + user_number[:3] + '(\))?[ -]?' + \ user_number[3:6] + "[- ]?" + user_number[6:10] + "$") #pull all #s from db connection = connect_db(DB_PATH) number_list = connection.execute("select * from phoneno").fetchall() # don't send "Unsubscribed" message twice already_sent = False # pull all matching numbers and delete them from the db for pair in number_list: number = str(pair[0]) # user could have entered their number in any one of a number of # formats if re.match(phone_regex, number): connection.execute("delete from phoneno where phoneno = :no", {"no" : number} ) msg = "Unsubscribed. Sorry to see you go - Kevin" if not already_sent: #text message user twimsg = TwilioMessage() twimsg.send_message_to_one_user(msg, user_number) already_sent = True connection.commit() connection.close() response = make_response(the_response) return response else: #want to email me the text contents recipients = ['kevin@goodmorning' + SITE['name'] + '.com'] subject = "You got a text message about Snack" msg_body = "".join(["From: ", user_number, "\n", "Message: ", the_text_content]) sender = ("Kevin Burke", "*****@*****.**") msg = Message(subject, recipients=recipients, body=msg_body, sender=sender) MAIL.send(msg) response = make_response(the_response) return response
def storephone(): """phone number posted from snack. store it in db, send confirmation msg""" if SITE['name'] != "cmc": return render_template('404.html', site=SITE), 404 elif request.method == "POST": phoneno = request.form['phone'] connection = connect_db(DB_PATH) cur = connection.execute("insert into phoneno (phoneno) values (:no)", {"no": phoneno}) cur.close() #commit the changes connection.commit() connection.close() msg = """Thanks for signing up for What's for Snack. Share with friends: http://bit.ly/cmcsnack. Unsub: reply 'u' to this number. - Kevin""" twimsg = TwilioMessage() error_if_any = twimsg.send_message_to_one_user(msg, phoneno) response = make_response(u'<html><body>'+ error_if_any + '</body></html>') return response
def main(): '''get the snack menu, construct SMS message, call send method''' snack_url = 'http://specials.cafebonappetit.com/baXML.asp?cafeid=228' opened_url = urllib2.urlopen(snack_url) snack_xml = opened_url.read() the_tree = etree.fromstring(snack_xml) the_snack = the_tree.findall(".//txtTitle") the_snack_text = "" if the_snack: the_snack_element = the_snack[0] if hasattr(the_snack_element, 'text'): the_snack_text = the_snack_element.text if not the_snack_text: the_message = "No snack tonight. Sorry :(" else: the_message = """Tonight's snack: %s. Tell your friends: http://bit.ly/cmcsnack. Unsub: reply 'u' to this number""" % the_snack_text # send the message twilio_message = TwilioMessage() twilio_message.send_message_to_all_users(the_message)