<commit_before>__author__ = 'mstibbs'

# External Modules
from datetime import datetime

# Internal Modules
import bb_dbconnector_factory
import bb_auditlogger
import bb_types
import config_services
import bb_notification_handler
import bb_usersettings_handler as user_settings
import blockbuster.bb_command_processor
import blockbuster.workflows as workflow
from blockbuster.messaging.bb_pushover_handler import send_push_notification
from config import *
from messaging import bb_sms_handler
from bb_logging import logger


def process_twilio_request(request):

    logger.info('################################ NEW SMS ###############################################')

    if not within_operational_period():
        logger.info("Interaction suppressed due to time restrictions.")
        return "<Response></Response>"

    # Create an instance of an smsrequest object
    smsrequest = bb_types.SMSRequestFactory().create()

    # Extract some information from the http requests
    SMSFrom = str(request.form['From'])
    SMSTo = str(request.form['To'])

    # Strip whitespace from the beginning and end of the body of the message
    SMSBody = str(request.form['Body']).rstrip().lstrip()

    # Populate the smsrequest instance with details of the received request
    smsrequest.requestormobile = SMSFrom
    smsrequest.servicenumber = SMSTo
    smsrequest.requestbody = SMSBody

    global instancename
    global location
    instancename, location = config_services.identify_service(SMSTo)
    smsrequest.instancename = instancename

    SMSList = combinesplitregistrations(SMSBody, location)
    smsrequest.requestcommandlist = SMSList

    smsservice = "Twilio"
    smsrequest.requestsmsservice = smsservice

    # Add an analytics record for the received request
    bb_dbconnector_factory.DBConnectorInterfaceFactory()\
        .create()\
        .add_analytics_record("Count", "SMS-Receive", instancename)

    # Construct a dictionary containing details for creating a message log entry
    logentry = {
        "Direction": "I <----",
        "SMSService": smsservice,
        "Command": "",
        "Originator": SMSFrom,
        "OriginatorName": bb_dbconnector_factory.DBConnectorInterfaceFactory().create().get_name_from_mobile(SMSFrom),
        "Destination": SMSTo,
        "RecipientName": instancename,
        "Details": SMSBody
    }

    # Construct a dictionary containing details for creating an audit log entry
    audit_entry = "Originator:" + SMSFrom + \
                  ";Receipient:" + SMSTo + \
                  ";Body:" + SMSBody

    # Check the incoming message to see which function has been requested
    commandelement = smsrequest.getcommandelement()

    # Define lists of aliases for some of the commands
    start_command_list = ['START']
    help_command_list = ['HELP', '?']
    move_command_list = ['MOVE', 'M']
    block_command_list = ['BLOCK', 'B']
    unblock_command_list = ['UNBLOCK', 'U']
    unregister_command_list = ['UNREGISTER', 'UR']

    # First, check if the command is a register command. If so, process the registration.
    if commandelement == "REGISTER":
        logentry['Command'] = "REGISTER"
        bb_dbconnector_factory.DBConnectorInterfaceFactory().create().add_transaction_record(logentry)
        bb_auditlogger.BBAuditLoggerFactory().create().logAudit('app', 'RCVCMD-REGISTER', audit_entry)
        logger.info("REGISTER Command Received")
        register(SMSTo, SMSFrom, SMSList, location)
        return "<Response></Response>"

    if commandelement in start_command_list:
        logentry['Command'] = "START"
        bb_dbconnector_factory.DBConnectorInterfaceFactory().create().add_transaction_record(logentry)
        bb_auditlogger.BBAuditLoggerFactory().create().logAudit('app', 'RCVCMD-START', audit_entry)
        workflow.start.send_welcome_message(smsrequest)
        return "<Response></Response>"

    if commandelement in help_command_list:
        logentry['Command'] = "HELP"
        bb_dbconnector_factory.DBConnectorInterfaceFactory().create().add_transaction_record(logentry)
        bb_auditlogger.BBAuditLoggerFactory().create().logAudit('app', 'RCVCMD-HELP', audit_entry)
        workflow.command_help.syntaxhelp(SMSTo, SMSFrom)
        return "<Response></Response>"

    # If not a registration, proceed to check that the requesting user is registered with the service.<commit_msg>Switch to using the workflow go method<commit_after>__author__ = 'mstibbs'

# External Modules
from datetime import datetime

# Internal Modules
import bb_dbconnector_factory
import bb_auditlogger
import bb_types
import config_services
import bb_notification_handler
import bb_usersettings_handler as user_settings
import blockbuster.bb_command_processor
import blockbuster.workflows as workflow
from blockbuster.messaging.bb_pushover_handler import send_push_notification
from config import *
from messaging import bb_sms_handler
from bb_logging import logger


def process_twilio_request(request):

    logger.info('################################ NEW SMS ###############################################')

    if not within_operational_period():
        logger.info("Interaction suppressed due to time restrictions.")
        return "<Response></Response>"

    # Create an instance of an smsrequest object
    smsrequest = bb_types.SMSRequestFactory().create()

    # Extract some information from the http requests
    SMSFrom = str(request.form['From'])
    SMSTo = str(request.form['To'])

    # Strip whitespace from the beginning and end of the body of the message
    SMSBody = str(request.form['Body']).rstrip().lstrip()

    # Populate the smsrequest instance with details of the received request
    smsrequest.requestormobile = SMSFrom
    smsrequest.servicenumber = SMSTo
    smsrequest.requestbody = SMSBody

    global instancename
    global location
    instancename, location = config_services.identify_service(SMSTo)
    smsrequest.instancename = instancename

    SMSList = combinesplitregistrations(SMSBody, location)
    smsrequest.requestcommandlist = SMSList

    smsservice = "Twilio"
    smsrequest.requestsmsservice = smsservice

    # Add an analytics record for the received request
    bb_dbconnector_factory.DBConnectorInterfaceFactory()\
        .create()\
        .add_analytics_record("Count", "SMS-Receive", instancename)

    # Construct a dictionary containing details for creating a message log entry
    logentry = {
        "Direction": "I <----",
        "SMSService": smsservice,
        "Command": "",
        "Originator": SMSFrom,
        "OriginatorName": bb_dbconnector_factory.DBConnectorInterfaceFactory().create().get_name_from_mobile(SMSFrom),
        "Destination": SMSTo,
        "RecipientName": instancename,
        "Details": SMSBody
    }

    # Construct a dictionary containing details for creating an audit log entry
    audit_entry = "Originator:" + SMSFrom + \
                  ";Receipient:" + SMSTo + \
                  ";Body:" + SMSBody

    # Check the incoming message to see which function has been requested
    commandelement = smsrequest.getcommandelement()

    # Define lists of aliases for some of the commands
    start_command_list = ['START']
    help_command_list = ['HELP', '?']
    move_command_list = ['MOVE', 'M']
    block_command_list = ['BLOCK', 'B']
    unblock_command_list = ['UNBLOCK', 'U']
    unregister_command_list = ['UNREGISTER', 'UR']

    # First, check if the command is a register command. If so, process the registration.
    if commandelement == "REGISTER":
        logentry['Command'] = "REGISTER"
        bb_dbconnector_factory.DBConnectorInterfaceFactory().create().add_transaction_record(logentry)
        bb_auditlogger.BBAuditLoggerFactory().create().logAudit('app', 'RCVCMD-REGISTER', audit_entry)
        logger.info("REGISTER Command Received")
        register(SMSTo, SMSFrom, SMSList, location)
        return "<Response></Response>"

    if commandelement in start_command_list:
        logentry['Command'] = "START"
        bb_dbconnector_factory.DBConnectorInterfaceFactory().create().add_transaction_record(logentry)
        bb_auditlogger.BBAuditLoggerFactory().create().logAudit('app', 'RCVCMD-START', audit_entry)
        workflow.start.send_welcome_message(smsrequest)
        return "<Response></Response>"

    if commandelement in help_command_list:
        logentry['Command'] = "HELP"
        bb_dbconnector_factory.DBConnectorInterfaceFactory().create().add_transaction_record(logentry)
        bb_auditlogger.BBAuditLoggerFactory().create().logAudit('app', 'RCVCMD-HELP', audit_entry)
        workflow.command_help.go(smsrequest)
        return "<Response></Response>"

    # If not a registration, proceed to check that the requesting user is registered with the service.
{'fraction_non_alphanumeric': 0.10808046368905558, 'fraction_numerical': 0.0, 'mean_word_length': 4.017103762827822, 'pattern_counts': {'":': 18, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1990552', 'n_tokens_mistral': 2503, 'n_tokens_neox': 2407, 'n_words': 606}