def send_turn(phone):
    ''' manually send a turn '''
    turn_data = request.get_json()
    try:
        player = models.find_player(phone)
    except NoResultFound:
        return failure('no player found with that phone number')

    player = player.set_contacted(True)

    # send a turn
    queue = []
    for text in turn_data['text']:
        logging.info('sending message: %s', text)
        sms = TWILIO.send(text, '+%s' % player.phone)
        queue.append(sms)
        time.sleep(DELAY)

        models.add_message(player, turn_data, sms)

    if 'options' in turn_data and len(turn_data['options']):
        options_text = GAME.format_options(turn_data, player.name)
        logging.info('sending message: %s', options_text)
        sms = TWILIO.send(options_text, '+%s' % player.phone)
        queue.append(sms)
        time.sleep(DELAY)

        models.add_message(player, turn_data, sms)

    return success(queue)
def respond():
    ''' receives a reply from twilio '''
    sms = request.values.to_dict()
    phone = sms['From'].replace('+', '')

    try:
        player = models.find_player(phone)
    except NoResultFound:
        # contact from an unknown number
        player = models.add_player(None, phone)

        turn_data = {'text': ['Pardon me, but what name do you go by?'], 'uid': None}
        player.set_pending_turn(turn_data)
        return success(player.pending_turn)
    else:
        player.set_show(True)

        # TODO: this is maybe the wrong way to get this
        previous_turn = player.pending_turn

        turn = GAME.process_response(previous_turn, sms['Body'], player.name)
        player.set_pending_turn(turn)

        models.add_message(player, {'text': sms['Body']}, sms, incoming=True)

        return success(turn)
Exemple #3
0
def new_message(data):
    """Sent by a client when the user entered a new message.
    The message is sent to both people in the chat."""
    message = data["msg"]
    print("New received message bro! was {}".format(message), file=sys.stdout)
    username = current_user().username
    user_id = current_user().id
    chat_id = active_chats[user_id]
    # Safety check
    if None in [message, user_id, chat_id
                ] or len(message) == 0 or len(message) > 500:
        return False
    chat = models.get_chat(chat_id)
    # Check that user is valid participant in chat
    if username != chat.user1_name and username != chat.user2_name:
        return False
    # Get the 'other' user in the chat
    other_userid = chat.user1_id
    other_username = chat.user1_name
    if user_id == chat.user1_id:
        other_userid = chat.user2_id
        other_username = chat.user2_name
    # Insert message into db
    msg = models.add_message(message, user_id, username, other_userid,
                             other_username, chat_id)
    # Safety check
    if msg is None:
        return False
    # Update the chat's last message time
    models.update_chat_last_message_time(chat_id, msg.dt)
    # Send message back to client
    emit(
        "message",
        {
            "sender": msg.sender_username,
            "receiver": msg.receiver_username,
            "msg": msg.text,
            "id": msg.id,
            "dt": msg.dt.isoformat(),
        },
        room=chat_id,
    )
Exemple #4
0
def new_message(data):
    message = data["msg"]
    user_id = session["user"]["id"]
    username = session["user"]["username"]
    chat_id = session["chat_id"]
    # Safety check
    if None in [message, user_id, chat_id
                ] or len(message) == 0 or len(message) > 500:
        return False
    chat = models.get_chat(chat_id)

    if username != chat.user1_name and username != chat.user2_name:
        return False

    other_userid = chat.user1_id
    other_username = chat.user1_name
    if user_id == chat.user1_id:
        other_userid = chat.user2_id
        other_username = chat.user2_name

    msg = models.add_message(message, user_id, username, other_userid,
                             other_username, chat_id)

    if msg is None:
        return False

    models.update_chat_last_message_time(chat_id, msg.dt)

    emit(
        "message",
        {
            "sender": msg.sender_username,
            "receiver": msg.receiver_username,
            "msg": msg.text,
            "id": msg.id,
            "dt": msg.dt.isoformat(),
        },
        room=chat_id,
    )
Exemple #5
0
def new_message(data):
    """Sent by a client when the user entered a new message.
    The message is sent to both people in the chat."""
    message = data['msg']
    user_id = session['user']['id']
    username = session['user']['username']
    chat_id = session['chat_id']
    # Safety check
    if None in [message, user_id, chat_id
                ] or len(message) == 0 or len(message) > 500:
        return False
    chat = models.get_chat(chat_id)
    # Check that user is valid participant in chat
    if username != chat.user1_name and username != chat.user2_name:
        return False
    # Get the 'other' user in the chat
    other_userid = chat.user1_id
    other_username = chat.user1_name
    if user_id == chat.user1_id:
        other_userid = chat.user2_id
        other_username = chat.user2_name
    # Insert message into DB
    msg = models.add_message(message, user_id, username, other_userid,
                             other_username, chat_id)
    # Safety check
    if msg is None:
        return False
    # Update the chat's last message time
    models.update_chat_last_message_time(chat_id, msg.dt)
    # Send message back to client
    emit('message', {
        'sender': msg.sender_username,
        'receiver': msg.receiver_username,
        'msg': msg.text,
        'id': msg.id,
        'dt': msg.dt.isoformat()
    },
         room=chat_id)
Exemple #6
0
import sys

sys.path.append(".")
from models import does_nickname_exist, add_message

if len(sys.argv) < 2:
    print("usage: add_profile.py NICKNAME", file=sys.stderr)
    sys.exit(1)
nickname = sys.argv[1]

if does_nickname_exist(nickname):
    print("nickname {} already exists".format(nickname))
    sys.exit(1)

for line in sys.stdin.readlines():
    add_message(nickname, line)
import sys
import json

sys.path.append('.')
from models import add_message

if len(sys.argv) < 2:
    print('no file given', file=sys.stderr)
    sys.exit(1)
history_file = sys.argv[1]

with open(history_file, 'r') as fp:
    history = json.load(fp)

for nickname, timed_messages in history.items():
    print('adding %s messages for %s' % (len(timed_messages), nickname))
    for time, message in timed_messages:
        add_message(nickname, message, time)
Exemple #8
0
 def on_message(self, username, message):
     add_message(username, message)