Ejemplo n.º 1
0
def config():
    update = request.args.get('update')

    config = Config()
    existing_configs = Config.query().fetch()
    if existing_configs:
        config = existing_configs[0]

    if update == 'true':
        admin_username = request.args.get('admin_username')
        admin_apikey = request.args.get('admin_apikey')
        twilio_account_sid = request.args.get('twilio_account_sid')
        twilio_auth_token = request.args.get('twilio_auth_token')
        twilio_phone_number = request.args.get('twilio_phone_number')

        if admin_username:
            config.admin_username = admin_username
        if admin_apikey:
            config.admin_apikey = admin_apikey
        if twilio_account_sid:
            config.twilio_account_sid = twilio_account_sid
        if twilio_auth_token:
            config.twilio_auth_token = twilio_auth_token
        if twilio_phone_number:
            config.twilio_phone_number = twilio_phone_number

        config.put()

    return config.toJson()
Ejemplo n.º 2
0
def incoming_twilio_sms():
    sms = SmsRequest(id=SmsRequest.id(),
                     from_number=request.form.get('From'),
                     to_number=request.form.get('To'),
                     body=request.form.get('Body'),
                     twilio_message_id=request.form.get('MessageSid'),
                     from_city=request.form.get('FromCity'),
                     from_state=request.form.get('FromState'),
                     from_zip=request.form.get('FromZip'),
                     from_country=request.form.get('FromCountry'),
                     to_city=request.form.get('ToCity'),
                     to_state=request.form.get('ToState'),
                     to_zip=request.form.get('ToZip'),
                     to_country=request.form.get('ToCountry'))

    if not sms.valid:
        app.logger.error(request)
        abort(400, 'invalid request')

    # store all sms for auditing
    sms.put()

    # load application data associated with the sms
    user = User.query(User.phone_number == sms.from_number).fetch()
    if not user:
        abort(
            400, 'The phone number {} does not belong to a user'.format(
                sms.from_number))

    sms.user = user[0]

    response_twiml = twiml.Response()
    response_message = None

    # dispatch sms request
    try:
        response_message = dispatcher.dispatch(sms)
    except (NoRouteError, MultipleRouteError):
        response_message = _('Unknown command, valid format below:\n'
                             'PLANT [qty] [type]\n'
                             'HARVEST [qty] [type]\n'
                             'SELL [qty] [type]\n'
                             'BROADCAST [msg]')

    if response_message:
        config = Config.query().get()
        response_twiml.sms(to=sms.from_number,
                           sender=config.twilio_phone_number,
                           msg=response_message)

    # update sms processed state
    sms.processed = True
    sms.ts_processed = datetime.now()
    sms.put()

    return str(response_twiml)
Ejemplo n.º 3
0
    def inner(*args, **kwargs):
        config = Config.query().fetch()[0]
        auth = request.authorization

        if not auth \
            or auth.username != config.admin_username \
                or auth.password != config.admin_apikey:
                    abort(400, "unauthorized access")

        return func(*args, **kwargs)
    def inner(*args, **kwargs):
        config = Config.query().fetch()[0]
        auth = request.authorization

        if not auth \
            or auth.username != config.admin_username \
                or auth.password != config.admin_apikey:
            abort(400, "unauthorized access")

        return func(*args, **kwargs)
Ejemplo n.º 5
0
def incoming_twilio_sms():
    sms = SmsRequest(id=SmsRequest.id(),
                     from_number=request.form.get('From'),
                     to_number=request.form.get('To'),
                     body=request.form.get('Body'),
                     twilio_message_id=request.form.get('MessageSid'),
                     from_city=request.form.get('FromCity'),
                     from_state=request.form.get('FromState'),
                     from_zip=request.form.get('FromZip'),
                     from_country=request.form.get('FromCountry'),
                     to_city=request.form.get('ToCity'),
                     to_state=request.form.get('ToState'),
                     to_zip=request.form.get('ToZip'),
                     to_country=request.form.get('ToCountry'))

    if not sms.valid:
        app.logger.error(request)
        abort(400, 'invalid request')

    # store all sms for auditing
    sms.put()

    # load application data associated with the sms
    user = User.query(User.phone_number == sms.from_number).fetch()
    if not user:
        abort(400, 'The phone number {} does not belong to a user'.format(sms.from_number))

    sms.user = user[0]

    response_twiml = twiml.Response()
    response_message = None

    # dispatch sms request
    try:
        response_message = dispatcher.dispatch(sms)
    except (NoRouteError, MultipleRouteError):
        response_message = _('Unknown command, valid format below:\n'
                             'PLANT [qty] [type]\n'
                             'HARVEST [qty] [type]\n'
                             'SELL [qty] [type]\n'
                             'BROADCAST [msg]')

    if response_message:
        config = Config.query().get()
        response_twiml.sms(to=sms.from_number,
                           sender=config.twilio_phone_number,
                           msg=response_message)

    # update sms processed state
    sms.processed = True
    sms.ts_processed = datetime.now()
    sms.put()

    return str(response_twiml)
def broadcast_sms():
    config = Config.query().get()
    data = request.form.get("task")

    d = json.loads(data)
    phone_numbers = d["phone_numbers"]
    message = d["message"]

    client = TwilioRestClient(config.twilio_account_sid, config.twilio_auth_token)

    for phone_number in phone_numbers:
        client.messages.create(body=message, to=phone_number, from_=config.twilio_phone_number)

    return {"farmers_sent": len(phone_numbers), "body": message}
Ejemplo n.º 7
0
def incoming_twilio_sms():
    sms = SmsRequest(
        id=SmsRequest.id(),
        from_number=request.form.get("From"),
        to_number=request.form.get("To"),
        body=request.form.get("Body"),
        twilio_message_id=request.form.get("MessageSid"),
        from_city=request.form.get("FromCity"),
        from_state=request.form.get("FromState"),
        from_zip=request.form.get("FromZip"),
        from_country=request.form.get("FromCountry"),
        to_city=request.form.get("ToCity"),
        to_state=request.form.get("ToState"),
        to_zip=request.form.get("ToZip"),
        to_country=request.form.get("ToCountry"),
    )

    if not sms.valid:
        app.logger.error(request)
        abort(400, "invalid request")

    # store all sms for auditing
    sms.put()

    # load application data associated with the sms
    user = User.query(User.phone_number == sms.from_number).fetch()
    if not user:
        abort(400, "The phone number {} does not belong to a user".format(sms.from_number))

    sms.user = user[0]

    response_twiml = twiml.Response()
    response_message = None

    # dispatch sms request
    try:
        response_message = dispatcher.dispatch(sms)
    except (NoRouteError, MultipleRouteError):
        response_message = _("Unknown command")

    if response_message:
        config = Config.query().get()
        response_twiml.sms(to=sms.from_number, sender=config.twilio_phone_number, msg=response_message)

    # update sms processed state
    sms.processed = True
    sms.ts_processed = datetime.now()
    sms.put()

    return str(response_twiml)
    def test_update_config(self):
        res = self.config(update='true',
                          admin_username='******',
                          admin_apikey='123',
                          twilio_account_sid='sid',
                          twilio_auth_token='token',
                          twilio_phone_number='123')
        data = json.loads(res.data)

        self.assertEqual(200, res.status_code)
        self.assertEqual('kat', data['admin_username'])
        self.assertEqual('123', data['admin_apikey'])
        self.assertEqual('sid', data['twilio_account_sid'])
        self.assertEqual('token', data['twilio_auth_token'])
        self.assertEqual('123', data['twilio_phone_number'])

        self.assertEqual(1, len(Config.query().fetch()))
def broadcast_sms():
    config = Config.query().get()
    data = request.form.get('task')

    d = json.loads(data)
    phone_numbers = d['phone_numbers']
    message = d['message']

    client = TwilioRestClient(
        config.twilio_account_sid,
        config.twilio_auth_token)

    for phone_number in phone_numbers:
        client.messages.create(
            body=message,
            to=phone_number,
            from_=config.twilio_phone_number)

    return {
        'farmers_sent': len(phone_numbers),
        'body': message
    }