def process_chat_real(session, message): curr_exchange, data = get_session(session) # guarantees data to be a dict if curr_exchange is None: curr_exchange = 'start' set_session(session, 'start', data) exch_type = exchange_type(curr_exchange) try: return EXCHANGE_TYPES_NEXT[exch_type](session, message) except KeyError: pass mapping = keywords(curr_exchange) new_exchange = default_func(curr_exchange) cleaned_words = clean(message).lower().split() if '?' in message: cleaned_words.append('question') for mess_word in cleaned_words: if mess_word in mapping: new_exchange = mapping[mess_word] break if new_exchange: set_session(session, new_exchange, data) return get_prompt(session, new_exchange, data) return None
def tangent_exchange(session_id, message): """Determine whether we're queued or not.""" curr_exchange, data = get_session(session_id) if data.get('queued', False): return None # stay right where we are new_exchange = default_func(curr_exchange) if new_exchange is None: return None set_session(session_id, new_exchange, data) return process_chat.get_prompt(session_id, new_exchange, data)
def manual_update_key(): body = request.get_json() session_id = body.get('session') key = body.get('key') value = body.get('value') if None in (session_id, key, value): return 'Session, key and value must be provided!', 400 exchange, data = get_session(session_id) data[key] = value set_session(session_id, exchange, data) return ''
def receive_welcome_request(): if request.values.get('password', '') != SECRETS['password']: return 'Bad password!', 401 num = request.values.get('phone_num') exchange = request.values.get('exchange') if not num or not exchange: return 'phone_num and exchange must both be provided!', 400 if not has_conversed(num): message_contents = get_prompt(num, exchange, {}) send_message_wrapper(num, message_contents) set_session(num, exchange, {}) return message_contents return ''
def manual_delete_key(): body = request.get_json() session_id = body.get('session') key = body.get('key') if session_id is None or key is None: return 'Session and key must be provided!', 400 exchange, data = get_session(session_id) try: del data[key] except KeyError: pass set_session(session_id, exchange, data) return ''
def manual_set_exchange(): body = request.get_json() session_id = body.get('session') new_exch = body.get('exchange') remove_queue = body.get('de_queue', False) send_prompt = body.get('send_prompt', False) if session_id is None or new_exch is None: return 'Session and exchange must be provided!', 400 _, data = get_session(session_id) if remove_queue: data['queued'] = False if send_prompt: prompt = get_prompt(session_id, new_exch, data) send_message_wrapper(session_id, prompt) set_session(session_id, new_exch, data) return ''
def new_session_post(): session_id = request.values.get('session-id') exchange = request.values.get('exchange') if not session_id or not exchange: return render_template('new_session.html', all_exchanges=tuple( exchange_translation.all_exchanges()), error='Session ID and exchange are required!') all_digits = set(digits) id_digits = ''.join(d for d in session_id if d in all_digits) bypass = request.values.get('bypass', 'off') == 'on' if bypass: phone_num = '+' + id_digits # accept it as valid (international?) elif len(id_digits) == 10: phone_num = '+1' + id_digits elif len(id_digits) == 11 and id_digits[0] == '1': phone_num = '+' + id_digits else: return render_template('new_session.html', all_exchanges=tuple( exchange_translation.all_exchanges()), error='Bad phone number! (should be 10 digits)') send_prompt = request.values.get('send-prompt', 'on') == 'on' keys = request.values.getlist('data-key') values = request.values.getlist('data-value') user_data = dict(zip(keys, values)) try: if user_data[''] == '': # we don't want that del user_data[''] except KeyError: pass set_session(phone_num, exchange, user_data) if send_prompt: prompt = get_prompt(phone_num, exchange, user_data) send_message_wrapper(phone_num, prompt) return redirect(url_for('new_session'))
def mark_queued(session_id, status): exchange, data = get_session(session_id) data['queued'] = status set_session(session_id, exchange, data)